目录
MyBatis-Plus 常用方法
一. mapper 层,继承了 BaseMapper<>
I. 增(insert)
// 插入一条数据. T:表示任意实体类型; entity:表示实体类对象
int insert(T entity)
II. 删(delete)
// 根据主键 ID 删除
int deleteById(Serializable id)
// 根据 map 定义字段的条件删除,即 columnMap 自定义的条件
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
// 根据实体类定义的条件删除对象 wrapper:表示实体对象封装的操作类,可以为 null
int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
// 根据 ID 进行批量删除 idList:表示主键 ID 集合,不能为 null
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
III. 改(update)
/**
* 根据 updateWrapper 条件修改实体类对象
*
* entity:这个是 set 的条件,可以为 null
* updateWrapper:实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
*/
int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);
// 根据 ID 修改实体类对象
int updateById(@Param(Constants.ENTITY) T entity);
IV. 查(select)
// 根据主键 ID 查询数据
T selectById(Serializable id);
// 根据实体类封装的对象,查询一条记录
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 ID 批量查询
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 entity 条件,查询全部记录
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 columnMap 定义的条件查询
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
// 根据 Wrapper 条件,查询全部记录
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询所有记录,但只保留第一个字段的值
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询记录的总条数
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 entity 条件,查询全部记录(并分页)
IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录(并分页)
IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
二. service 层,继承了 IService<>
I. 增(save)
// 调用 BaseMapper 的 insert 方法,用于添加一条记录
boolean save(T entity);
// 批量插入数据. entityList:表示实体对象集合 batchSize:表示一次批量插入的数量,默认为 1000
boolean saveBatch(Collection<T> entityList);
boolean saveBatch(Collection<T> entityList, int batchSize);
**注意:**在批量插入的场景中,需要在 spring.datasource.url
中设置开启批量输入 ----- &rewriteBatchedStatements=true
,不然批量插入不生效。
II. 删(remove)
// 调用 BaseMapper 的 delete 方法,根据实体类定义的条件,删除记录
boolean remove(Wrapper<T> queryWrapper);
// 调用 BaseMapper 的 deleteById 方法,根据 ID 删除数据
boolean removeById(Serializable id);
// 调用 BaseMapper 的 deleteByMap 方法,根据 columnMap 定义的字段,删除记录
boolean removeByMap(Map<String, Object> columnMap);
// 调用 BaseMapper 的 deleteBatchIds 方法,根据 ID 批量删除
boolean removeByIds(Collection<? extends Serializable> idList);
III. 改或增(saveOrUpdate)
// 实体类 entity 中,id 若存在,则修改;id 不存在,则新增数据。 新增还是修改取决于你有没有传 id
boolean saveOrUpdate(T entity);
// 根据 updateWrapper 尝试更新,否继续执行 saveOrUpdate 方法
boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper);
// 批量修改插入并修改数据. entityList:表示实体对象集合 batchSize:表示一次批量插入的数量,默认为 1000
boolean saveOrUpdateBatch(Collection<T> entityList);
boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
IV. 查(get、list)
// 调用 BaseMapper 的 selectById 方法,根据 ID 返回数据
default T getById(Serializable id);
// 根据 Wrapper 返回一条记录,返回值为实体类类型
default T getOne(Wrapper<T> queryWrapper);
// 根据 Wrapper 返回一条记录,返回值 map
Map<String, Object> getMap(Wrapper<T> queryWrapper);
// 返回所有数据
List<T> list();
// 调用 BaseMapper 的 selectList 方法,查询所有数据,返回 entit 集合
List<T> list(Wrapper<T> queryWrapper);
// 调用 BaseMapper 的 selectBatchIds 方法,根据 ID 批量查询
Collection<T> listByIds(Collection<? extends Serializable> idList);
// 调用 BaseMapper 的 selectById 方法,根据 columnMap 条件查询所有记录,返回 map 集合
Collection<T> listByMap(Map<String, Object> columnMap);
// 调用 BaseMapper 的 selectMaps 方法,查询所有列表
List<Map<String, Object>> listMaps();
// 这个跟上面的也是一样的,只是添加了一个查询条件
List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper);
// 查询全部记录. 这个方法还有几个带参的方法,使用场景不多,这里就不列出来了
List<Object> listObjs();
// 查询总记录数
int count();
// 根据 Wrapper 条件,查询总记录数
int count(Wrapper<T> queryWrapper);
// 无条件分页查询
IPage<T> page(IPage<T> page);
// 调用 BaseMapper 的 selectPage 方法,按 Wrapper 条件分页查询
IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);
// 无条件分页查询
IPage<Map<String, Object>> pageMaps(IPage<T> page);
// 调用 BaseMapper 的 selectMapsPage 方法,按 Wrapper 条件分页查询
IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);
三. 条件构造器
AbstractWrapper
QueryWrapper
(LambdaQueryWrapper
) 和UpdateWrapper
(LambdaUpdateWrapper
) 的父类
用于生成 sql 的 where 条件, entity 属性也用于生成 sql 的 where 条件
I. 比较大小
// 等价于 =
// 例: eq("name", "Peng") ---> name = 'Peng'
eq(R column, Object val)
// 等价于 !=
// 例: ne("name", "Peng") ---> name != 'Peng'
ne(R column, Object val)
// 等价于 >
// 例: gt("name", "Peng") ---> name > 'Peng'
gt(R column, Object val)
// 等价于 >=
// 例: ge("name", "Peng") ---> name >= 'Peng'
ge(R column, Object val)
// 等价于 <
// 例: lt("name", "Peng") ---> name < 'Peng'
lt(R column, Object val)
// 等价于 <=
// 例: le("name", "Peng") ---> name <= 'Peng'
le(R column, Object val)
II. 范围
// 等价于 between a and b
// 例: between("age", 1, 18) ---> age between 1 and 18
between(R column, Object val1, Object val2)
// 等价于 not between a and b
// 例: notBetween("age", 1, 18) ---> age not between 1 and 18
notBetween(R column, Object val1, Object val2)
// 等价于 字段 IN (v0, v1, ...)
// 例: in("age",{1,2,3}) ---> age in (1,2,3)
in(R column, Object... values)
// 等价于 字段 NOT IN (v0, v1, ...)
// 例: notIn("age",{1,2,3}) ---> age not in (1,2,3)
notIn(R column, Object... values)
// 等价于 字段 IN (sql 语句)
// inSql("id", "select id from table where id < 3") ---> id in (select id from table where id < 3)
inSql(R column, Object... values)
// 等价于 字段 NOT IN (sql 语句)
// 例: notInSql("age", "1,2,3,4,5,6") ---> age not in (1,2,3,4,5,6)
notInSql(R column, Object... values)
III. 模糊匹配
// 等价于 LIKE '%值%'
// like("name", "王") ---> name like '%王%'
like(R column, Object val)
// 等价于 NOT LIKE '%值%'
// 例: notLike("name", "王") ---> name not like '%王%'
notLike(R column, Object val)
// 等价于 LIKE '%值'
// 例: likeLeft("name", "王") ---> name like '%王'
likeLeft(R column, Object val)
// 等价于 LIKE '值%'
// 例: likeRight("name", "王") ---> name like '王%'
likeRight(R column, Object val)
IV. 空值比较
// 等价于 IS NULL
// 例: isNull("name") ---> name is null
isNull(R column)
// 等价于 IS NOT NULL
// 例: isNotNull("name") ---> name is not null
isNotNull(R column)
V. 分组排序
// 等价于 GROUP BY 字段, ...
// 例: groupBy("id", "name") ---> group by id,name
groupBy(R... columns)
// 等价于 ORDER BY 字段, ... ASC
// 例: orderByAsc("id", "name") ---> order by id ASC,name ASC
orderByAsc(R... columns)
// 等价于 ORDER BY 字段, ... DESC
// 例: orderByDesc("id", "name") ---> order by id DESC,name DESC
orderByDesc(R... columns)
// 等价于 HAVING ( sql语句 )
// 例: having("sum(age) > {0}", 11) ---> having sum(age) > 11
having(String sqlHaving, Object... params)
VI. 拼接、嵌套
// 等价于 a or b。 or 的拼接
// 例:eq("id",1).or().eq("name","老王") ---> id = 1 or name = '老王'
or()
// 等价于 or(a or/and b)。 or的嵌套
// 例: or(i -> i.eq("name", "李白").ne("status", "活着")) ---> or (name = '李白' and status <> '活着')
// <> 表示 不等于
or(Consumer<Param> consumer)
// 等价于 and(a or/and b)。 and 的嵌套
// 例: and(i -> i.eq("name", "李白").ne("status", "活着")) ---> and (name = '李白' and status <> '活着')
and(Consumer<Param> consumer)
// 等价于 (a or/and b)。 普通嵌套
// 例: nested(i -> i.eq("name", "李白").ne("status", "活着")) ---> (name = '李白' and status <> '活着')
nested(Consumer<Param> consumer)
// 拼接sql(若不使用 params 参数,可能存在 sql 注入),
// 例: apply("date_format(dateColumn,'%Y-%m-%d') = {0}", "2008-08-08") ---> date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")
apply(String applySql, Object... params)
// 无视优化规则直接拼接到 sql 的最后,可能存若在 sql 注入。
last(String lastSql)
// 拼接 exists (sql语句)
// 例: exists("select id from table where age = 1") ---> exists (select id from table where age = 1)
exists(String existsSql)
// 拼接 not Exists (sql语句)
// 例: notExists("select id from table where age = 1")--->not exists (select id from table where age = 1)
notExists(String notExistsSql)
VII. QueryWrapper 条件
// 用于定义需要返回的字段
// 例: select("id", "name", "age") ---> select id, name, age
select(String... sqlSelect)
// Lambda 表达式,过滤需要的字段
select(Predicate<TableFieldInfo> predicate)
// 返回一个 LambdaQueryWrapper
lambda()
VIII. UpdateWrapper 条件
// 用于设置 set 字段值
// 例: set("name", null) ---> set name = null
set(String column, Object val)
// 用于设置 set 字段值
// 例: setSql("name = '老李头'") ---> set name = '老李头'
etSql(String sql)
// 返回一个 LambdaUpdateWrapper
lambda()