MyBatis-Plus (简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
官方文档
1. 常见注解
MyBatis-Plus通过扫描实体类,并基于反射来获取实体类信息作为数据库表信息(基础BaseMapper时添加泛型)
1 2 3
| public interface UserMapper extends BaseMapper<User> { }
|
1 2 3 4 5 6 7 8 9 10 11
| public class User { private Long id; private String username; private String password; private String phone; private String info; private Integer status; private Integer balance; private LocalDateTime createTime; private LocalDateTime updateTime; }
|
映射规则:
- 类名驼峰下划线作为表明
- 名为id的字段作为表的主键
- 变量名驼峰下换线作为表的字段名
mp通过反射获取到数据库表信息后就可以帮我们生成SQL语句
MyBatis-Plus中比较常用的几个注解:
- @TableName:指定数据库表名称
- @TableId:指定数据库表中的主键
- 可以设置主键生成类型,通过在注解中添加type属性实现,type属性的值封装在IdType中
- AUTO:自增
- INPUT :通过set方法自行输入
- ASSIGN_ID:分配ID,通过接口IdentifierGenerator的方法nextId来生成ID,采用雪花算法
- @TableFiled:指定数据库表中的普通字段信息
- 使用场景:
- 成员变量名与数据库字段名不一致
- 成员变量名以is开头并且是布尔值
- 成员变量名与数据库攻击值冲突
- 成员变量不是数据库字段
1 2 3 4 5 6 7 8 9 10 11 12 13
| @TableName("tb_user") public class User { @TableId(Value="u_id", type="IdType.AUTO") private Long id; @TableFileId("username") private String Name; @TableFileId("is_married") private Boolean isMarried; @TableFiledId("`order`") private Integer order; @TableFIledId(exist = false) private String address; }
|
2. 常见配置
1 2 3 4 5 6 7 8 9 10
| mybatis-plus: mapper-locations: classpath:mapping/*.xml type-aliases-package: com.rainng.coursesystem.model configuration: map-underscore-to-camel-case: true cache-enabled: false global-config: db-config: id-type: assign_id update-strategy: not_null
|
这里除了type-aliases-package,其它的都不需要配置,默认值即可(上面的值就是默认值),除非有需要
3. CRUD接口
官方文档:CRUD 接口 | MyBatis-Plus (baomidou.com)
Service层
Save
1 2 3 4 5 6
| boolean save(T entity);
boolean saveBatch(Collection<T> entityList);
boolean saveBatch(Collection<T> entityList, int batchSize);
|
参数说明
| 类型 |
参数名 |
描述 |
| T |
entity |
实体对象 |
| Collection |
entityList |
实体对象集合 |
| int |
batchSize |
插入批次数量 |
SaveOrUpdate
1 2 3 4 5 6 7 8
| boolean saveOrUpdate(T entity);
boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper);
boolean saveOrUpdateBatch(Collection<T> entityList);
boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
|
参数说明
| 类型 |
参数名 |
描述 |
| T |
entity |
实体对象 |
| Wrapper |
updateWrapper |
实体对象封装操作类 UpdateWrapper |
| Collection |
entityList |
实体对象集合 |
| int |
batchSize |
插入批次数量 |
Remove
1 2 3 4 5 6 7 8
| boolean remove(Wrapper<T> queryWrapper);
boolean removeById(Serializable id);
boolean removeByMap(Map<String, Object> columnMap);
boolean removeByIds(Collection<? extends Serializable> idList);
|
参数说明
| 类型 |
参数名 |
描述 |
| Wrapper |
queryWrapper |
实体包装类 QueryWrapper |
| Serializable |
id |
主键 ID |
| Map<String, Object> |
columnMap |
表字段 map 对象 |
| Collection<? extends Serializable> |
idList |
主键 ID 列表 |
Update
1 2 3 4 5 6 7 8 9 10
| boolean update(Wrapper<T> updateWrapper);
boolean update(T updateEntity, Wrapper<T> whereWrapper);
boolean updateById(T entity);
boolean updateBatchById(Collection<T> entityList);
boolean updateBatchById(Collection<T> entityList, int batchSize);
|
参数说明
| 类型 |
参数名 |
描述 |
| Wrapper |
updateWrapper |
实体对象封装操作类 UpdateWrapper |
| T |
entity |
实体对象 |
| Collection |
entityList |
实体对象集合 |
| int |
batchSize |
更新批次数量 |
Get
1 2 3 4 5 6 7 8 9 10
| T getById(Serializable id);
T getOne(Wrapper<T> queryWrapper);
T getOne(Wrapper<T> queryWrapper, boolean throwEx);
Map<String, Object> getMap(Wrapper<T> queryWrapper);
<V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
|
参数说明
| 类型 |
参数名 |
描述 |
| Serializable |
id |
主键 ID |
| Wrapper |
queryWrapper |
实体对象封装操作类 QueryWrapper |
| boolean |
throwEx |
有多个 result 是否抛出异常 |
| T |
entity |
实体对象 |
| Function<? super Object, V> |
mapper |
转换函数 |
List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| List<T> list();
List<T> list(Wrapper<T> queryWrapper);
Collection<T> listByIds(Collection<? extends Serializable> idList);
Collection<T> listByMap(Map<String, Object> columnMap);
List<Map<String, Object>> listMaps();
List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper);
List<Object> listObjs();
<V> List<V> listObjs(Function<? super Object, V> mapper);
List<Object> listObjs(Wrapper<T> queryWrapper);
<V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
|
参数说明
| 类型 |
参数名 |
描述 |
| Wrapper |
queryWrapper |
实体对象封装操作类 QueryWrapper |
| Collection<? extends Serializable> |
idList |
主键 ID 列表 |
| Map<String, Object> |
columnMap |
表字段 map 对象 |
| Function<? super Object, V> |
mapper |
转换函数 |
Page
1 2 3 4 5 6 7 8
| IPage<T> page(IPage<T> page);
IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);
IPage<Map<String, Object>> pageMaps(IPage<T> page);
IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);
|
参数说明
| 类型 |
参数名 |
描述 |
| IPage |
page |
翻页对象 |
| Wrapper |
queryWrapper |
实体对象封装操作类 QueryWrapper |
Count
1 2 3 4
| int count();
int count(Wrapper<T> queryWrapper);
|
参数说明
| 类型 |
参数名 |
描述 |
| Wrapper |
queryWrapper |
实体对象封装操作类 QueryWrapper |
query
1 2 3 4 5 6 7 8
| QueryChainWrapper<T> query();
LambdaQueryChainWrapper<T> lambdaQuery();
query().eq("column", value).one(); lambdaQuery().eq(Entity::getId, value).list();
|
update
1 2 3 4 5 6 7 8
| UpdateChainWrapper<T> update();
LambdaUpdateChainWrapper<T> lambdaUpdate();
update().eq("column", value).remove(); lambdaUpdate().eq(Entity::getId, value).update(entity);
|
Mapper层
Insert
参数说明
Delete
1 2 3 4 5 6 7 8
| int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
int deleteById(Serializable id);
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
|
参数说明
| 类型 |
参数名 |
描述 |
| Wrapper |
wrapper |
实体对象封装操作类(可以为 null) |
| Collection<? extends Serializable> |
idList |
主键 ID 列表(不能为 null 以及 empty) |
| Serializable |
id |
主键 ID |
| Map<String, Object> |
columnMap |
表字段 map 对象 |
Update
1 2 3 4
| int update(@Param(Constants.ENTITY) T updateEntity, @Param(Constants.WRAPPER) Wrapper<T> whereWrapper);
int updateById(@Param(Constants.ENTITY) T entity);
|
使用提示:
在调用updateById方法前,需要在T entity(对应的实体类)中的主键属性上加上@TableId注解。
参数说明
| 类型 |
参数名 |
描述 |
| T |
entity |
实体对象 (set 条件值,可为 null) |
| Wrapper |
updateWrapper |
实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) |
Select
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| T selectById(Serializable id);
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
|
参数说明
| 类型 |
参数名 |
描述 |
| Serializable |
id |
主键 ID |
| Wrapper |
queryWrapper |
实体对象封装操作类(可以为 null) |
| Collection<? extends Serializable> |
idList |
主键 ID 列表(不能为 null 以及 empty) |
| Map<String, Object> |
columnMap |
表字段 map 对象 |
| IPage |
page |
分页查询条件(可以为 RowBounds.DEFAULT) |
4. 条件构造器
官方文档:条件构造器 | MyBatis-Plus (baomidou.com)
allEq
全部eq(或个别isNull)
1 2 3
| allEq(Map<R, V> params) allEq(Map<R, V> params, boolean null2IsNull) allEq(boolean condition, Map<R, V> params, boolean null2IsNull)
|
参数null2IsNull: 为true则在map的value为null时调用isNull方法,为false时则忽略value为null的
例:
1 2
| allEq({id:1,name:"老王",age:null}) ---> id = 1 and name = '老王' and age is null allEq({id:1,name:"老王",age:null}, false) ---> id = 1 and name = '老王'
|
eq
等于=
例如:
1
| eq("name", "老王")--->name = '老王'
|
ne
不等于<>
1 2
| ne(R column, Object val) ne(boolean condition, R column, Object val)
|
例如:
1
| ne("name", "老王")--->name <> '老王'
|
gt
大于
例如:
1
| gt("age", 18)--->age > 18
|
ge
大于等于>=
例如:``` java
1
| ge("age", 18)`--->`age >= 18
|
lt
小于<
例如:
1
| lt("age", 18)--->age < 18
|
le
小于等于<=
例如:
1
| le("age", 18)--->age <= 18
|
between
在什么之间
例如
1
| between("age", 18, 30)--->age between 18 and 30
|
notBetween
不在什么之间
例如:
1
| notBetween("age", 18, 30)--->age not between 18 and 30
|
like
模糊查询
例如:
1
| like("name", "王")--->name like '%王%'
|
notLike
例如:
1
| notLike("name", "王")--->name not like '%王%'
|
likeLeft
例如:
1
| likeLeft("name", "王")--->name like '%王'
|
likeRight
例如:
1
| likeRight("name", "王")--->name like '王%'
|
notLikeLeft
例如:
1
| notLikeLeft("name", "王")--->name not like '%王'
|