1.Mybatis-plus简介
MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
2.Mybatis-plus特性
- 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
- 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作
- 强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求
- 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错
- 支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题
- 支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作
- 支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )
- 内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用
- 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询
- 分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库
- 内置性能分析插件:可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询
- 内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作
3.如何使用Mybatis-plus
在一个Springboot工程中添加Mybatis-plus的相关依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
在Dao层Mapper接口 继承BaseMapper<>类
4.crud
4.1新增insert
entity:
package com.cbb.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
/**
* <p>
*
* </p>
*
* @author jobob
* @since 2021-06-21
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
@TableId(type = IdType.AUTO)
private Integer id;
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private Integer age;
/**
* 邮箱
*/
private String email;
@TableLogic
private Integer deleted;
}
使用
@Test
void insertUser() {
User user = new User(null,"李四",18,"19.com",0);
int insert = mapper.insert(user);
System.out.println(insert);
}
4.2删除delete
(1)逻辑删除
说明:
只对自动注入的sql起效:
- 插入: 不作限制
- 查找: 追加where条件过滤掉已删除数据,且使用 wrapper.entity 生成的where条件会忽略该字段
- 更新: 追加where条件防止更新到已删除数据,且使用 wrapper.entity 生成的where条件会忽略该字段
- 删除: 转变为 更新
例如:
- 删除:
update user set deleted=1 where id = 1 and deleted=0
- 查找:
select id,name,deleted from user where deleted=0
字段类型支持说明:
- 支持所有数据类型(推荐使用
Integer
,Boolean
,LocalDateTime
)- 如果数据库字段使用
datetime
,逻辑未删除值和已删除值支持配置为字符串null
,另一个值支持配置为函数来获取值如now()
附录:
- 逻辑删除是为了方便数据恢复和保护数据本身价值等等的一种方案,但实际就是删除。
- 如果你需要频繁查出来看就不应使用逻辑删除,而是以一个状态去表示。
①在表中新增一个表示 状态的字段deleted(字段名自定义)
②在实体类中对应的字段 添加@TableLogic注解
测试:
@Test
void deleteByid(){
int i = mapper.deleteById(6);
System.out.println(i);
}
4.3修改update
自动填充
①在实体类添加注释
②设置配置类
@Configuration
public class MyConfig implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.strictInsertFill(metaObject, "inserttime", LocalDateTime.class, LocalDateTime.now());
}
@Override
public void updateFill(MetaObject metaObject) {
this.strictUpdateFill(metaObject, "updatetime", LocalDateTime.class, LocalDateTime.now());
}
}
③测试
@Test
void updateUser(){
User user = new User("李四test02", 18, "19.com");
UpdateWrapper<User> wrapper = new UpdateWrapper<>();
wrapper.eq("id",5);
mapper.update(user,wrapper);
}
@Test
void insertUser() {
User user = new User("李四test02",18,"19.com");
int insert = mapper.insert(user);
System.out.println(insert);
}
4.4条件查询
@Test
void selectCondition(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.between("age",18,22);
wrapper.or();
wrapper.like("name","王");
wrapper.orderByDesc("age");
// wrapper.select("count(*)");
// wrapper.groupBy("u_name");
List<User> users = mapper.selectList(null);
System.out.println(users);
}
4.5分页查询
①配置分页插件
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
return interceptor;
}
②使用
@Test
public void selectPage(){
/**
* Page:当前页码 每页显示的条数
*/
Page<User> page=new Page<>(1,4);
Page<User> selectPage = mapper.selectPage(page, null);
System.out.println("当前的总页码:"+selectPage.getPages());
System.out.println("总条数:"+selectPage.getTotal());
System.out.println("当前页的记录:"+selectPage.getRecords());
}
entity:
package com.cbb.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
/**
* <p>
*
* </p>
*
* @author cbb
* @since 2021-06-21
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
@TableId(type = IdType.AUTO)
private Integer id;
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private Integer age;
/**
* 邮箱
*/
private String email;
/**
* 逻辑删除
*/
@TableLogic
private Integer deleted;
/**
* 新增时间
*/
@TableField(fill = FieldFill.INSERT)
private LocalDateTime inserttime;
public User(String name, Integer age, String email) {
this.name = name;
this.age = age;
this.email = email;
}
/**
* 修改时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updatetime;
}