https://mp.baomidou.com/==>快速开始
1. Mybatis-Plus依赖
1 | <dependency> |
2. 数据库设计
1 | drop table if exists user; |
3. lombok
3.1. Maven依赖
1 | <!-- 简化实体类 get/set方法--> |
intelliJ IDEA设置
3.2. @Data
添加在实体类上
1 | package com.runaccpeted.mybatisplus.entity; |
3.3. get/set自动生成
4. application.properties
1 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver |
5. extends BaseMapper< Object>
1 | package com.runaccpeted.mybatisplus.mapper; |
5.1. BaseMapper 已定义方法
1 | public interface BaseMapper<T> extends Mapper<T> { |
6. 添加@MapperScan
1 |
|
7. CRUD测试
1 | <dependency> |
Test.java
1 | package com.runaccpeted.mybatisplus; |
7.1. 分布式id生成策略
mybatis-plus自生成的id是一串数字 1233005715855114242
即默认采用的是snowflake算法
1 |
|
7.1.1. 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方法) |
分布式全局唯一ID 长整型类型(please use ASSIGN_ID ) |
|
32位UUID字符串(please use ASSIGN_UUID ) |
|
分布式全局唯一ID 字符串类型(please use ASSIGN_ID ) |
7.2. 增加
1 | int insert(T entity); |
7.3. 更新
1 | // 根据 whereEntity 条件,更新记录 |
7.4. 查询
1 | // 根据 ID 查询 |
测试
1 | // 查询所有 |
7.5. 删除
1 | // 根据 entity 条件,删除记录 |
8. 自动填充-更新时间
8.1. @TableField(fill = FieldFill.INSERT)
注解填充字段
1 | //添加数据时加值 |
8.2. 实现类 MyMetaObjectHandler
1 | package com.runaccpeted.mybatisplus.handler; |
8.3. 测试
1 | package com.runaccpeted.mybatisplus; |
8.4. 输出
1 | ==> Preparing: INSERT INTO user ( id, name, age, email, create_time, update_time ) VALUES ( ?, ?, ?, ?, ?, ? ) |
9. 乐观锁
丢失更新
当多个线程运行时,通过比较当前数据版本号和数据库里记录版本号是否一样,一致才提交事务,并把版本号+1。其他线程提交时,版本号不同则不能提交事务。
9.1. 表中添加版本号列
1 | alter table user add column version int default 1; |
9.2. 插件
1 | package com.runaccpeted.mybatisplus.config; |
9.3. 添加属性@Version
1 |
|
9.4. 注意⚠️
支持的数据类型只有:int,Integer,long,Long,Date,Timestamp,LocalDateTime
仅支持 updateById(id) 与 update(entity, wrapper) 方法
自动填充版本号
1 | package com.runaccpeted.mybatisplus.handler; |
9.5. 测试乐观锁
1 |
|
9.6. 输出
1 | ==> Preparing: SELECT id,name,age,email,create_time,update_time,version FROM user WHERE id=? |
10. 分页实现
10.1. 注册PaginationInterceptor Bean
1 | package com.runaccpeted.mybatisplus.config; |
10.2. Page(long current, long size) - selectPage()
1 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
10.3. 输出
1 | ==> Preparing: SELECT COUNT(1) FROM user |
11. 逻辑删除
不同于物理删除,仍存在于数据库,状态改为被删除状态
11.1. 表中添加删除状态字段
1 | alter table user add column deleted boolean default 0; |
表中没有boolean属性
11.2. 实体类中添加属性@TableLogic
1 |
|
11.3. 自动填充状态
1 |
|
11.4. 配置application.properties
1 | # 逻辑已删除值(默认为 1) |
11.5. 删除测试
1 |
|
11.6. 输出
1 | ==> Preparing: UPDATE user SET deleted=1 WHERE id=? AND deleted=0 |
并没有删除这条记录
11.7. 查询测试
1 |
|
11.8. 输出
1 | ==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 |
输出的是deleted=0 的记录
12. 条件构造器Wrapper-QueryWrapper
https://mp.baomidou.com/guide/wrapper.html#alleq
12.1. gt(“age”,80) = age>80
1 |
|
12.1.1. 输出
1 | ==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND (age > ?) |