MyBatis-Plus
什么是MyBatis-Plus
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
MyBatis-Plus实现curd
-
创建数据库及相关数据库表
-
创建springboot工程并引入MP依赖和相关依赖
<!--mybatis-plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>Latest Version</version> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
-
配置数据库相关信息
在application.properties文件中添加以下配置# mysql数据库连接 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/mptest?characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=root # mybatis日志 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
-
编写核心代码
创建包 entity 编写实体类 User :User.java@Data public class User { private Long id; private String phone; private String name; private Integer age; }
创建包 mapper 编写 Mapper 接口:UserMapper.java
@Repository public interface UserMapper extends BaseMapper<User> { }
在启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:
@SpringBootApplication @MapperScan("com.xiaonvshenyo.mptest.mapper") public class MptestApplication { public static void main(String[] args) { SpringApplication.run(MptestApplication.class, args); } }
-
在测试类中进行测试
@SpringBootTest public class SampleTest { @Autowired private UserMapper userMapper; @Test public void findAll() { List<User> userList = userMapper.selectList(null); System.out.println(userList); } @Test public void insertOne() { User user = new User(); user.setName("张三"); user.setPhone("12345678910"); user.setAge(25); int count = userMapper.insert(user); //返回影响行数 System.out.println(count); } @Test public void updateOne() { User user = new User(); user.setId(100000L); user.setName("李四"); int count = userMapper.updateById(user); //返回影响行数 System.out.println(count); } @Test public void deleteOne() { int count = userMapper.deleteById(100000L); //返回影响行数 System.out.println(count); } }
MyBatis-Plus自动填充
- 添加表字段create_time、update_time,更新User实体类
- 为进行自动填充的属性添加注解
FieldFill.DEFAULT 默认不处理
FieldFill.INSERT 插入填充字段
FieldFill.UPDATE 更新填充字段
FieldFill.INSERT_UPDATE 插入和更新填充字段@TableField(fill = FieldFill.INSERT) private Date createTime; @TableField(fill = FieldFill.INSERT_UPDATE) private Date updateTime;
- 创建实现类 MyMetaObjectHandler
@Component public class MyMetaObjectHandler implements MetaObjectHandler { //MP执行添加操作时,该方法执行 @Override public void insertFill(MetaObject metaObject) { this.setFieldValByName("createTime", new Date(), metaObject); this.setFieldValByName("updateTime", new Date(), metaObject); } //MP执行修改操作时,该方法执行 @Override public void updateFill(MetaObject metaObject) { this.setFieldValByName("updateTime", new Date(), metaObject); } }
- 重新执行上一节测试方法,查看结果
MyBatis-Plus逻辑删除
- 在表中添加字段 delete,用作逻辑删除标志,每次删除时修改标志位
默认未设置情况:0表示没有删除,1表示已删除 - 在实体类字段上添加注解@TableLogic
@TableLogic private Integer delete;
MyBatis-Plus分页查询
- 配置分页插件
创建包 config 添加配置类:MpConfig.java@Configuration @MapperScan("com.xiaonvshenyo.mptest.mapper") public class MpConfig { /** * 分页插件 */ @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } }
- 编写分页代码
@Test public void getPage() { Page<User> page = new Page<>(1,3); Page<User> userPage= userMapper.selectPage(page, null); //返回对象得到分页的所有数据 List<User> records = userPage.getRecords();//查询数据集合 long pages = userPage.getPages();//总页数 long current = userPage.getCurrent();//当前页 long total = userPage.getTotal();//总记录数 boolean hasNext = userPage.hasNext();//下一页 boolean hasPrevious = userPage.hasPrevious();//上一页 }
MyBatis-Plus条件构造器
- 常用条件构造器:QueryWrapper
- 常用方法:
方法 含义 举例 eq 等于 = 例: eq(“name”, “老王”) ====> name = ‘老王’ ne 不等于 <> 例: ne(“name”, “老王”) ====> name <> ‘老王’ ge 大于等于 >= 例: ge(“age”, 18) ====> age >= 18 le 小于等于 <= 例: le(“age”, 18) ====> age <= 18 gt 大于 > 例: gt(“age”, 18) ====> age > 18 lt 小于 < 例: lt(“age”, 18) ====> age < 18 … … … - 示例:
@Test public void wapperTest() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.isNull("name") .ge("age", 12) .isNotNull("phone"); List<User> userList = userMapper.selectList(queryWrapper); System.out.println(userList); }
详细操作请参考开发文档