MyBatis-Plus(简称MP)是一个MyBatis的增强工具。
自动实现Mapper CRUD操作,极致提高数据库开发效率。
MP在MyBatis的基础上只做增强不做改变。
pom引入mybatis-plus依赖
打开pom.xml文件引入依赖
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plusartifactId>
<version>3.3.2version>
dependency>
Spring XML更改配置SqlSessionFactory实现类
打开applicationContext.xml修改SqlSessionFactory实现类
<bean id="sessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mappers/*.xml"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
bean>
mybatis-config.xml增加MP分页插件
打开mybatis-config.xml添加分页插件配置
<plugins>
<plugin interceptor="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor">plugin>
plugins>
@TableName - 将实体类与表名映射
@TableId - 说明对应属性是表的主键
@TableField - 设置属性与列名的对应关系
如果字段名与属性名相同或者符合驼峰命名转换规则,则TableField可省略

创建实体类,@TableName/@Tableld/@TableField实现映射
在com.ql.reader.entity包下创建实体类Test.java
package com.ql.reader.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
@TableName("test")//说明实体对应哪一张表
public class Test {
@TableId(type = IdType.AUTO)
@TableField("id")//说明属性对应哪个字段
private Integer id;
@TableField("content")
private String content;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
创建Mapper接口继承BaseMapper,创建Mapper XML
打开com.ql.reader.mapper包下的TestMapper.java修改内容为
package com.ql.reader.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ql.reader.entity.Test;
public interface TestMapper extends BaseMapper<Test> {
public void insertSample();
}
同时修改src/main/resources/mappers目录下的test.xml文件
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ql.reader.mapper.TestMapper">
<insert id="insertSample">
insert into test(content) values ('测试内容')
insert>
mapper>
开发时注入Mapper对象,通过内置API实现CRUD操作
在src/test/java/com/ql/reader目录下创建测试类MyBatisPlusTest.java
package com.ql.reader;
import com.ql.reader.entity.Test;
import com.ql.reader.mapper.TestMapper;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class MyBatisPlusTest {
@Resource
private TestMapper testMapper;
@org.junit.Test
public void testInsert(){
Test test = new Test();
test.setContent("MyBatis Plus测试");
testMapper.insert(test);
}
@org.junit.Test
public void testUpdate(){
Test test = testMapper.selectById(24);
test.setContent("MyBatis Plus测试1");
testMapper.updateById(test);
}
@org.junit.Test
public void testDelete(){
testMapper.deleteById(24);
}
@org.junit.Test
public void testSelect(){
QueryWrapper<Test> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id", 23);
queryWrapper.gt("id", 20);
List<Test> list = testMapper.selectList(queryWrapper);
System.out.println(list.get(0));
}
}
依次去运行测试方法,运行成功。





打开Navicat连接数据库,创建数据表
图书表book

会员表member

评论表evaluation

分类表category

阅读状态表member_read_state

后台管理员用户表user
