CRUD interface
Service CRUD interface
illustrate:
- General Service CRUD package IService
- Interface, further encapsulates CRUD and adopts
get 查询单行
remove 删除
list 查询集合
page 分页
prefix naming method to distinguishMapper
layers to avoid confusion. - Generics
T
are any entity object - It is recommended that if there is a possibility to customize the general Service method, please create your
IBaseService
ownMybatis-Plus
base class inherited from the provided - object
Wrapper
is a conditional initializer
Save
// 插入一条记录(选择字段,策略插入)
boolean save(T entity);
// 插入(批量)
boolean saveBatch(Collection<T> entityList);
// 插入(批量)
boolean saveBatch(Collection<T> entityList, int batchSize);
Parameter Description
type | parameter name | describe |
---|---|---|
T | entity | entity object |
Collection<T> | entityList | collection of entity objects |
int | batchSize | Insert batch quantity |
SaveOrUpdate
// TableId 注解存在更新记录,否插入一条记录
boolean saveOrUpdate(T entity);
// 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法
boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper);
// 批量修改插入
boolean saveOrUpdateBatch(Collection<T> entityList);
// 批量修改插入
boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
Parameter Description
type | parameter name | describe |
---|---|---|
T | entity | entity object |
Wrapper<T> | updateWrapper | Entity object encapsulation operation class UpdateWrapper |
Collection<T> | entityList | collection of entity objects |
int | batchSize | Insert batch quantity |
Remove
// 根据 entity 条件,删除记录
boolean remove(Wrapper<T> queryWrapper);
// 根据 ID 删除
boolean removeById(Serializable id);
// 根据 columnMap 条件,删除记录
boolean removeByMap(Map<String, Object> columnMap);
// 删除(根据ID 批量删除)
boolean removeByIds(Collection<? extends Serializable> idList);
Parameter Description
type | parameter name | describe |
---|---|---|
Wrapper<T> | queryWrapper | Entity wrapping class QueryWrapper |
Serializable | id | primary key ID |
Map<String, Object> | columnMap | table field map object |
Collection<? extends Serializable> | idList | List of primary key IDs |
Update
// 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
boolean update(Wrapper<T> updateWrapper);
// 根据 whereWrapper 条件,更新记录
boolean update(T updateEntity, Wrapper<T> whereWrapper);
// 根据 ID 选择修改
boolean updateById(T entity);
// 根据ID 批量更新
boolean updateBatchById(Collection<T> entityList);
// 根据ID 批量更新
boolean updateBatchById(Collection<T> entityList, int batchSize);
Parameter Description
type | parameter name | describe |
---|---|---|
Wrapper<T> | updateWrapper | Entity object encapsulation operation class UpdateWrapper |
T | entity | entity object |
Collection<T> | entityList | collection of entity objects |
int | batchSize | Update batch quantity |
Get
// 根据 ID 查询
T getById(Serializable id);
// 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")
T getOne(Wrapper<T> queryWrapper);
// 根据 Wrapper,查询一条记录
T getOne(Wrapper<T> queryWrapper, boolean throwEx);
// 根据 Wrapper,查询一条记录
Map<String, Object> getMap(Wrapper<T> queryWrapper);
// 根据 Wrapper,查询一条记录
<V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
Parameter Description
type | parameter name | describe |
---|---|---|
Serializable | id | primary key ID |
Wrapper<T> | queryWrapper | Entity object encapsulation operation class QueryWrapper |
boolean | throwEx | If there are multiple results, whether an exception is thrown |
T | entity | entity object |
Function<? super Object, V> | mapper | conversion function |
List
// 查询所有
List<T> list();
// 查询列表
List<T> list(Wrapper<T> queryWrapper);
// 查询(根据ID 批量查询)
Collection<T> listByIds(Collection<? extends Serializable> idList);
// 查询(根据 columnMap 条件)
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);
// 根据 Wrapper 条件,查询全部记录
List<Object> listObjs(Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录
<V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
Parameter Description
type | parameter name | describe |
---|---|---|
Wrapper<T> | queryWrapper | Entity object encapsulation operation class QueryWrapper |
Collection<? extends Serializable> | idList | List of primary key IDs |
Map<String, Object> | columnMap | table field map object |
Function<? super Object, V> | mapper | conversion function |
Page
// 无条件分页查询
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);
Parameter Description
type | parameter name | describe |
---|---|---|
IPage<T> | page | page turning object |
Wrapper<T> | queryWrapper | Entity object encapsulation operation class QueryWrapper |
Count
// 查询总记录数
int count();
// 根据 Wrapper 条件,查询总记录数
int count(Wrapper<T> queryWrapper);
Parameter Description
type | parameter name | describe |
---|---|---|
Wrapper<T> | queryWrapper | Entity object encapsulation operation class QueryWrapper |
Chain
query
// 链式查询 普通
QueryChainWrapper<T> query();
// 链式查询 lambda 式。注意:不支持 Kotlin
LambdaQueryChainWrapper<T> lambdaQuery();
// 示例:
query().eq("column", value).one();
lambdaQuery().eq(Entity::getId, value).list();
update
// 链式更改 普通
UpdateChainWrapper<T> update();
// 链式更改 lambda 式。注意:不支持 Kotlin
LambdaUpdateChainWrapper<T> lambdaUpdate();
// 示例:
update().eq("column", value).remove();
lambdaUpdate().eq(Entity::getId, value).update(entity);
Mapper CRUD interface
illustrate:
- General CRUD package BaseMapper
- Interface, which automatically resolves the entity table relationship mapping at
Mybatis-Plus
startup and converts it intoMybatis
an internal object injection container - Generics
T
are any entity object - 参数
Serializable
为任意类型主键Mybatis-Plus
不推荐使用复合主键约定每一张表都有自己的唯一id
主键 - 对象
Wrapper
为 条件构造器
Insert
// 插入一条记录
int insert(T entity);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
T | entity | 实体对象 |
Delete
// 根据 entity 条件,删除记录
int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
// 删除(根据ID 批量删除)
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 ID 删除
int deleteById(Serializable id);
// 根据 columnMap 条件,删除记录
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
Wrapper<T> | wrapper | 实体对象封装操作类(可以为 null) |
Collection<? extends Serializable> | idList | 主键 ID 列表(不能为 null 以及 empty) |
Serializable | id | 主键 ID |
Map<String, Object> | columnMap | 表字段 map 对象 |
Update
// 根据 whereWrapper 条件,更新记录
int update(@Param(Constants.ENTITY) T updateEntity, @Param(Constants.WRAPPER) Wrapper<T> whereWrapper);
// 根据 ID 修改
int updateById(@Param(Constants.ENTITY) T entity);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
T | entity | 实体对象 (set 条件值,可为 null) |
Wrapper<T> | updateWrapper | 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) |
Select
// 根据 ID 查询
T selectById(Serializable id);
// 根据 entity 条件,查询一条记录
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);
// 根据 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);
// 根据 Wrapper 条件,查询总记录数
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
Serializable | id | 主键 ID |
Wrapper<T> | queryWrapper | 实体对象封装操作类(可以为 null) |
Collection<? extends Serializable> | idList | 主键 ID 列表(不能为 null 以及 empty) |
Map<String, Object> | columnMap | 表字段 map 对象 |
IPage<T> | page | 分页查询条件(可以为 RowBounds.DEFAULT) |
mapper 层 选装件
说明:
选装件位于 com.baomidou.mybatisplus.extension.injector.methods
包下 需要配合Sql 注入器
使用。
AlwaysUpdateSomeColumnById
int alwaysUpdateSomeColumnById(T entity);
insertBatchSomeColumn
int insertBatchSomeColumn(List<T> entityList);
logicDeleteByIdWithFill
int logicDeleteByIdWithFill(T entity);
ActiveRecord 模式
说明:
- 实体类只需继承 Model 类即可进行强大的 CRUD 操作
- 需要项目中已注入对应实体的BaseMapper
操作步骤:
class User extends Model<User>{
// fields...
}
- 调用
CRUD
方法(演示部分api,仅供参考)
User user = new User();
user.insert();
user.selectAll();
user.updateById();
user.deleteById();
// ...
SimpleQuery 工具类
说明:
- 对
selectList
查询后的结果用Stream
流进行了一些封装,使其可以返回一些指定结果,简洁了api的调用 - 需要项目中已注入对应实体的BaseMapper
- 对于下方参数
peeks
,其类型为Consumer...
,可一直往后叠加操作例如:List<Long> ids = SimpleQuery.list(Wrappers.lambdaQuery(), Entity::getId, System.out::println, user -> userNames.add(user.getName()));
keyMap
// 查询表内记录,封装返回为Map<属性,实体>
Map<A, E> keyMap(LambdaQueryWrapper<E> wrapper, SFunction<E, A> sFunction, Consumer<E>... peeks);
// 查询表内记录,封装返回为Map<属性,实体>,考虑了并行流的情况
Map<A, E> keyMap(LambdaQueryWrapper<E> wrapper, SFunction<E, A> sFunction, boolean isParallel, Consumer<E>... peeks);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
E | entity | 实体对象 |
A | attribute | 实体属性类型,也是map中key的类型 |
LambdaQueryWrapper<E> | wrapper | 支持lambda的条件构造器 |
SFunction<E, A> | sFunction | 实体中属性的getter,用于封装后map中作为key的条件 |
boolean | isParallel | 为true时底层使用并行流执行 |
Consumer<E>... | peeks | 可叠加的后续操作 |
map
// 查询表内记录,封装返回为Map<属性,属性>
Map<A, P> map(LambdaQueryWrapper<E> wrapper, SFunction<E, A> keyFunc, SFunction<E, P> valueFunc, Consumer<E>... peeks);
// 查询表内记录,封装返回为Map<属性,属性>,考虑了并行流的情况
Map<A, P> map(LambdaQueryWrapper<E> wrapper, SFunction<E, A> keyFunc, SFunction<E, P> valueFunc, boolean isParallel, Consumer<E>... peeks);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
E | entity | 实体对象 |
A | attribute | 实体属性类型,也是map中key的类型 |
P | attribute | 实体属性类型,也是map中value的类型 |
LambdaQueryWrapper<E> | wrapper | 支持lambda的条件构造器 |
SFunction<E, A> | keyFunc | 封装后map中作为key的条件 |
SFunction<E, P> | valueFunc | 封装后map中作为value的条件 |
boolean | isParallel | 为true时底层使用并行流执行 |
Consumer<E>... | peeks | 可叠加的后续操作 |
group
// 查询表内记录,封装返回为Map<属性,List<实体>>
Map<K, List<T>> group(LambdaQueryWrapper<T> wrapper, SFunction<T, A> sFunction, Consumer<T>... peeks);
// 查询表内记录,封装返回为Map<属性,List<实体>>,考虑了并行流的情况
Map<K, List<T>> group(LambdaQueryWrapper<T> wrapper, SFunction<T, K> sFunction, boolean isParallel, Consumer<T>... peeks);
// 查询表内记录,封装返回为Map<属性,分组后对集合进行的下游收集器>
M group(LambdaQueryWrapper<T> wrapper, SFunction<T, K> sFunction, Collector<? super T, A, D> downstream, Consumer<T>... peeks);
// 查询表内记录,封装返回为Map<属性,分组后对集合进行的下游收集器>,考虑了并行流的情况
M group(LambdaQueryWrapper<T> wrapper, SFunction<T, K> sFunction, Collector<? super T, A, D> downstream, boolean isParallel, Consumer<T>... peeks);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
T | entity | 实体对象 |
K | attribute | 实体属性类型,也是map中key的类型 |
D | - | 下游收集器返回类型,也是map中value的类型 |
A | - | 下游操作中间类型 |
M | - | 最终结束返回的Map<K, D> |
LambdaQueryWrapper<E> | wrapper | 支持lambda的条件构造器 |
SFunction<E, A> | sFunction | 分组依据,封装后map中作为key的条件 |
Collector<T, A, D> | downstream | 下游收集器 |
boolean | isParallel | 为true时底层使用并行流执行 |
Consumer<T>... | peeks | 可叠加的后续操作 |
list
// 查询表内记录,封装返回为List<属性>
List<A> list(LambdaQueryWrapper<E> wrapper, SFunction<E, A> sFunction, Consumer<E>... peeks);
// 查询表内记录,封装返回为List<属性>,考虑了并行流的情况
List<A> list(LambdaQueryWrapper<E> wrapper, SFunction<E, A> sFunction, boolean isParallel, Consumer<E>... peeks);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
E | entity | 实体对象 |
A | attribute | 实体属性类型,也是list中元素的类型 |
LambdaQueryWrapper<E> | wrapper | 支持lambda的条件构造器 |
SFunction<E, A> | sFunction | 封装后list中的元素 |
boolean | isParallel | 为true时底层使用并行流执行 |
Consumer<E>... | peeks | 可叠加的后续操作 |