mybatisPlus介绍
1.MybatisPlus介绍
Mybatis-Plus(简称 MP)是一个MyBatis(opens new window)的增强工具,在Mybatis的基础上只做增强不做改变,为简化开发、提高效率而生。
2.特性
- 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑。
- 损耗小:启动即会自动注入基本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查询。
- 内置性能分析插件:可输出SQL语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询。
- 内置全局拦截插件:提供全表delete、update操作只能分析阻断,也可自定义拦截规则,预防误操作。
3.案例介绍
1.创建SpringBoot项目
2.引入依赖
注意这里只引入了Mybatisplus依赖,其余SQL依赖需要自己添加。
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.10.1</version>
</dependency>
3.编码配置
新增StudentPO实体类
@Data
@TableName("student") // 指定表名
public class StudentPO {
private Long id;
private String name;
private Integer age;
private String email;
}
新增StudentMapper接口
@Mapper // 指定为Mapper接口,并继承BaseMapper,指定类型为StudentPO
public interface StudentMapper extends BaseMapper<StudentPO> {
}
Application启动类配置
@SpringBootApplication // 声明为SpringBoot应用启动类
@MapperScan("com.test.mybatisplustest.mapper") // Mapper接口扫描包
public class MyBatsisPlusStyudyApplication {
public static void main(String[] args) {
SpringApplication.run(MyBatsisPlusStyudyApplication.class, args);
}
}
配置application.yml
# 配置数据源
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatisplustest?useSSL=false&useUnicode=true&characterEncoding=utf8
username: root
password: 123456
# 开启日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
测试
这里测试就不加Service业务层了,直接测试。
@SpringBootTest // 声明为SpringBoot测试类
@Slf4j // 打印日志
public class TestController {
@Autowired
private StudentMapper studentMapper;
@Test
public void testQueryStudentList(){
List<StudentPO> studentList = studentMapper.selectList(null);
log.info("studentList========>"+studentList);
}
}
测试结果
案例总结
- MyBatis-Plus的案例变得更加简洁,更加方便。
- Mapper层中,我们只需要继承 BaseMapper<StudentPO>,然后使用泛型指定StudentPO的类型即可。
- StudentPO实体类种,通过@TableName("student") 绑定数据库库表。
- 测试使用时,直接调用BaseMapper中提供的方法,就可以快速的实现CRUD了。
4.MybatisPlus注解
1.@TableName
- 表名注解,标识实体类对应的表
- 使用位置:实体类
@Data
@TableName("student")
public class StudentPO {
private Long id;
private String name;
private Integer age;
private String email;
}
属性 | 类型 | 必须指定 | 默认值 | 描述 |
---|---|---|---|---|
value | String | 否 | "" | 表名 |
schema | String | 否 | "" | schema |
keepGlobalPrefix | boolean | 否 | false | 是否保持使用全局的 tablePrefix 的值(当全局 tablePrefix 生效时) |
resultMap | String | 否 | "" | xml中 resultMap 的 id (用于满足特定类型的实体类对象绑定) |
autoResultMap | boolean | 否 | false | 是否自动构建 resultMap 并使用(如果设置了resultMap 则不会进行resultMap 的自动构建与注入) |
excludeProperty | String[] | 否 | {} | 需要排除的属性名 @since 3.3.1 |
案例配置
# application.yml
mybatis-plus:
global-config:
db-config:
table-prefix: test. # 配置表前缀
@Data
@TableName(value="student",schema="mybatisplustest", keepGlobalPrefix=true,
excludeProperty = {"age"})
public class StudentPO {
private Long id;
private String name;
private Integer age;
private String email;
}
- 指定schema为mybatisplustest
- keepGlobalPrefix=true时,执行SQL,会自动加上test。
- excludeProperty={"age"}时,会忽略age属性,不会进行age字段查询,常用于非当前表数据,注意:excludeProperty={"age"}时,不能使用@TableField("age") 注解。
2.@TableId
- 描述:主键注解
- 使用位置:实体类主键字段
@Data
@TableName("student")
public class StudentPO {
@TableId
private Long id;
private String name;
private Integer age;
private String email;
}
属性 | 类型 | 必须指定 | 默认值 | 描述 |
---|---|---|---|---|
value | String | 否 | "" | 主键字段名 |
type | Enum | 否 | IdType.NONE | 指定主键类型 |
IdType
主键类型可以设置为如下:
值 | 描述 |
---|---|
AUTO | 数据库ID自增 |
NONE | 无状态,该类型为未设置主键类型(注解里等于跟随全局,全局里约等于INPUT) |
INPUT | insert前自行set主键值 |
ASSIGN_ID | 分配ID(主键类型为Number(Long 和 Integer) 或 String )(since 3.3.0),使用接口IdentifierGenerator的方法nextId(默认实现类为DefaultIdentifierGenerator雪花算法) |
ASSIGN_UUID | 分配UUID,主键类型为 String(since 3.3.0),使用接口IdentifierGenerator的方法nextUUID(默认default 方法) |
案例说明
@Data
@TableName("student")
public class StudentPO {
@TableId(value = "id",type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
}
- IdType.AUTO:数据库字段需要设置自增,否则会报错。
- IdType.INPUT:新增的数据必须设置id,否则会报错。
- IdType.ASSIGN_ID:注意数据类型为Number(Long 和 Integer)或String),生成结果:1746460971291541505(Long)
- IdType.ASSIGN_UUID:主键类型为String,生成结果是:74d2f31e333483df1f72222e98728900(String)
3..@TableField
描述:字段注解(非主键)
@Data
@TableName("student")
public class StudentPO {
private Long id;
@TableField("name")
private String name;
private Integer age;
private String email;
}
属性 | 类型 | 必须指定 | 默认值 | 描述 |
---|---|---|---|---|
value | String | 否 | "" | 数据库字段名 |
exist | boolean | 否 | true | 是否为数据库表字段 |
condition | String | 否 | "" | 字段 where 实体查询比较条件,有值设置则按设置的值为准,没有则为默认全局的 %s=#{%s},参考(opens new window) |
update | String | 否 | "" | 字段 update set 部分注入,例如: 当在version字段上注解update="%s+1" 表示更新时会 set version=version+1 (该属性优先级高于 el 属性) |
insertStrategy | Enum | 否 | FieldStrategy.DEFAULT | 举例: NOT_NULL |
updateStrategy | Enum | 否 | FieldStrategy.DEFAULT | 举例: IGNORED |
whereStrategy | Enum | 否 | FieldStrategy.DEFAULT | 举例: NOT_EMPTY |
fill | Enum | 否 | FieldFill.DEFAULT | 字段自动填充策略 |
select | boolean | 否 | true | 是否进行select 查询 |
keepGlobalFormat | boolean | 否 | false | 是否保持使用全局的 format 进行处理 |
jdbcType | jdbcType | 否 | JdbcType.UNDEFINED | JDBC类型(该默认值不代表会按照该值生效) |
typeHandler | Class<? extends TypeHandler> | 否 | UnknownTypeHandler.class | 类型处理器 (该默认值不代表会按照该值生效) |
numericScale | String | 否 | "" | 指定小数点后保留的位数 |
案例说明
@Data
@TableName(value = "student",excludeProperty = {"age"})
public class StudentPO {
@TableId(value = "id",type = IdType.ASSIGN_UUID)
private Long id;
@TableField(value = "name",condition = SqlCondition.LIKE)
private String name;
private Integer age;
@TableField(exist = false)
private String email;
}
// 测试
@Test
public void testQueryStudentList2(){
StudentPO studentPO = new StudentPO();
studentPO.setName("33");
QueryWrapper<StudentPO> queryWrapper = new QueryWrapper<StudentPO>().setEntity(studentPO);
studentMapper.selectList(queryWrapper).forEach(System.out::println);
}
测试结果
- exist效果和excludeProperty一样,建议用exist
- condition 常用在setEntity中生效
FieldStrategy
值 | 描述 |
ALWAYS | 总是加入SQL,无论字段值是否为NULL |
NOT——NULL | 非NULL判断 |
NOT_EMPTY | 非空判断(只对字符串类型字段,其他类型字段依然为非NULL 判断) |
DEFAULT | 追随全局配置 |
NEVER | 不加入SQL |
FieldFill
值 | 描述 |
DEFAULT | 默认不处理 |
INSERT | 插入时填充字段 |
UPDATE | 更新时填充字段 |
INSERT_UPDATE | 插入和更新时填充字段 |
4.@Version
描述:乐观锁注解、标记@Version 在字段上
@Data
@TableName(value = "student")
public class StudentPO {
private Long id;
private String name;
private Integer age;
private String email;
@Version
private Integer version;
}
- 乐观锁是一种并发控制策略,通过在数据表中添加一个版本号字段来实现。每次更新数据时,会将版本号加1。当多个用户同时对同一条数据进行修改时,只有一个用户能够成功更新数据,其他用户需要重新读取数据并重新进行修改。这样可以保证数据的一致性。
- 因此,乐观锁字段对于实现乐观锁机制是必要的。如果没有这个字段,就无法实现乐观锁的机制,也就无法保证并发更新时的数据一致性。
5.@EnumValue
描述:普通枚举类注解(注解在枚举字段上)
单值枚举,可以省略@EnumValue注解,会自动对应上
// 枚举类
public enum SexEnum {
MALE,
FEMALE,
UNKNOWN
}
// 实体类
@Data
@TableName(value = "student")
public class StudentPO {
@TableId(value = "id",type = IdType.ASSIGN_UUID)
private Long id;
private String name;
private Integer age;
private String email;
private SexEnum sex;
private String version;
}
键值类枚举
在需要存储数据库的属性上添加@EnumValue注解,在需要前端展示的属性上添加@JsonValue注解。
@AllArgsConstructor
@Getter
public enum SexEnum {
MAN(1, "男"),
WOMAN(2, "女");
@EnumValue
private Integer key;
@JsonValue
private String display;
}
6.TableLogic
描述:表字段逻辑处理注解(逻辑删除)
属性 | 类型 | 必须指定 | 默认值 | 描述 |
---|---|---|---|---|
value | String | 否 | "" | 逻辑未删除值 |
delval | String | 否 | "" | 逻辑删除值 |
// 实体类
@Data
@TableName(value = "student")
public class StudentPO {
@TableField("is_delete")
@TableLogic(value = "0",delval = "1")
private Integer isDelete;
}
7.@KeySequence
- 描述:序列主键策略 oracle
- 属性:value dbType
属性 | 类型 | 必须指定 | 默认值 | 描述 |
---|---|---|---|---|
value | String | 否 | "" | 序列名 |
dbType | Enum | 否 | DbType.OTHER | 数据库类型,未配置默认使用IKeyGenerator实现,如存在多个实现,必须明确指定。 |
@KeySequence(value="seq_user",clazz=String.class) //value为数据库中生成的序列名,class指主键属性类型
public class User {
@TableId(type=IdType.INPUT) //注意主键类型要指定为Input
private String id;
}
Oracle中配合序列使用
-- 创建序列
CREATE SEQUENCE seq_user
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 10
NOCYCLE
ORDER;
-- seq_user是序列的名称。
-- START WITH指定序列的起始值。
-- INCREMENT BY指定序列的增量值。
-- MINVALUE和MAXVALUE分别指定序列的最小值和最大值。这两个选项是可选的。
-- CACHE指定预分配的序列值的数量,用于提高性能。这个选项也是可选的。
-- ORDER表示生成的序列值是有序的,NOORDER表示生成的序列值是无序的。
-- CYCLE表示当序列达到最大值或最小值时,它会回滚到起始值。
-- NOCYCLE表示当序列达到最大值或最小值时,它将停止生成新的值。
-- 测试序列
SELECT seq_user.NEXTVAL FROM dual;
8.@Interceptorlgnore
@InterceptorIgnore 用于指定某个方法或类不进行拦截器处理的注解。当你希望某些方法或类不执行拦截器逻辑时,可以使用这个注解。
value 值为 1 | yes | on 视为忽略,例如 @interceptorIgnore(tenanLine = "1")
value 值为 0 | false | off | 空值不变,视为正常执行
属性名 | 类型 | 默认值 | 描述 |
---|---|---|---|
tenanLine | String | "" | 行级租户 |
dynamicTableName | String | "" | 动态表名 |
blockAttack | String | "" | 攻击SQL阻断解析器,防止全表更新与删除 |
illegalSql | String | "" | 垃圾SQL拦截 |
- 可以直接标注在方法上或类上。
- 如果标注在方法上,则该方法不会执行任何拦截器逻辑。
- 如果标注在类上,则该类及其所有方法都不会执行拦截器逻辑。
@InterceptorIgnore
public void testMethod() {
// 这个方法不会被任何拦截器处理
}
@InterceptorIgnore
public class TestClass {
public void testMethod() {
// 这个类及其所有方法都不会被任何拦截器处理
}
}
9,@OrderBy
描述:内置SQL默认指定排序,优先级低于 wrapper 条件查询
属性 | 类型 | 必须指定 | 默认值 | 描述 |
---|---|---|---|---|
asc | boolean | 否 | true | 是否升序查询 |
sort | short | 否 | Short.MAX_VALUE | 数字越小越靠前 |
默认倒序排序
@Data
@TableName(value = "student")
public class StudentPO {
@OrderBy(sort = 1)
@TableId(value = "id",type = IdType.ASSIGN_UUID)
private Long id;
@OrderBy(asc = true,sort = 2)
@TableField(value = "name",condition = SqlCondition.LIKE)
private String name;
private Integer age;
@TableField(exist = false)
private String email;
private SexEnum sex;
@Version
private String version;
@TableField("is_delete")
@TableLogic(value = "0",delval = "1")
private Integer isDelete;
}
// 输出:SELECT id,name,sex,version,is_delete FROM student WHERE is_delete=0 ORDER BY id DESC,name ASC