Mybatis Plus CRUD 接口
本文最后更新于:2024年9月8日 晚上
Mybatis Plus CRUD 接口
Service CRUD接口
说明
通用 Service CRUD 封装IService接口,进一步封装 CRUD 采用 get 查询单行
remove 删除
list 查询集合
page 分页
前缀命名方式区分 Mapper
层避免混淆
泛型 T
为任意实体对象。
建议如果存在自定义通用 Service 方法的可能,请创建自己的 IBaseService
继承 Mybatis-Plus
提供的父类。
对象 Wrapper
为条件构造器。
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 entity, Wrapper<T> updateWrapper) ;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
Chain
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 CRUD 接口
说明
通用 CRUD 封装BaseMapper接口,为 Mybatis-Plus
启动时自动解析实体表关系映射转换为 Mybatis
内部对象注入容器。
泛型 T
为任意实体对象。
参数 Serializable
为任意类型主键 Mybatis-Plus
不推荐使用复合主键约定每一张表都有自己的唯一 id
主键。
对象 Wrapper
为条件构造器。
insert
实例
1 2 3 4 5 6 7 8 9 @Test public void testInsert () { User user = new User(); user.setName("test" ); user.setAge(3 ); user.setEmail("123456@qq.com" ); int result = userMapper.insert(user); System.out.println(result); System.out.println(user); }
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 对象
实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @Test public void testDeleteById () { userMapper.deleteById(1240620674645544965L ); }@Test public void testDeleteBatchId () { userMapper.deleteBatchIds(Arrays.asList(1240620674645544961L ,124062067464554496 2L )); }@Test public void testDeleteMap () { HashMap<String, Object> map = new HashMap<>(); map.put("name" ,"test" ); userMapper.deleteByMap(map); }
update
1 2 3 4 int update (@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper) ;int updateById (@Param(Constants.ENTITY) T entity) ;
类型
参数名
描述
T
entity
实体对象(set 条件值,可为 null)
Wrapper
updateWrapper
实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
实例
1 2 3 4 5 6 7 8 9 10 11 @Test public void testUpdate () { User user = new User(); user.setId(6L ); user.setName("test" ); user.setAge(18 ); int i = userMapper.updateById(user); System.out.println(i); }
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)
实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 @Test public void testSelectById () { User user = userMapper.selectById(1L ); System.out.println(user); } @Test public void testSelectByBatchId () { List<User> users = userMapper.selectBatchIds(Arrays.asList(1 , 2 , 3 )); users.forEach(System.out::println); } @Test public void testSelectByBatchIds () { HashMap<String, Object> map = new HashMap<>(); map.put("name" ,"test" ); map.put("age" ,3 ); List<User> users = userMapper.selectByMap(map); users.forEach(System.out::println); }