3.mybatis-plus-service
1.通用service接口方法
前缀 : 查询单行
get
,删除remove
,查询集合list
,分页page
1.1.Save
// 插入一条记录(选择字段,策略插入)
boolean save(T entity);// 插入(批量)
boolean saveBatch(Collection<T> entityList);// 插入(批量)
boolean saveBatch(Collection<T> entityList, int batchSize);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
T | entity | 实体对象 |
Collection | entityList | 实体对象集合 |
int | batchSize | 插入批次数量 |
1.2.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);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
T | entity | 实体对象 |
Wrapper | updateWrapper | 实体对象封装操作类 UpdateWrapper |
Collection | entityList | 实体对象集合 |
int | batchSize | 插入批次数量 |
1.3.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);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
Wrapper | queryWrapper | 实体包装类 QueryWrapper |
Serializable | id | 主键 ID |
Map<String, Object> | columnMap | 表字段 map 对象 |
Collection<? extends Serializable> | idList | 主键 ID 列表 |
1.4.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);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
Wrapper | updateWrapper | 实体对象封装操作类 UpdateWrapper |
T | entity | 实体对象 |
Collection | entityList | 实体对象集合 |
int | batchSize | 更新批次数量 |
1.5. 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);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
Serializable | id | 主键 ID |
Wrapper | queryWrapper | 实体对象封装操作类 QueryWrapper |
boolean | throwEx | 有多个 result 是否抛出异常 |
T | entity | 实体对象 |
1.6.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);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
Wrapper | queryWrapper | 实体对象封装操作类 QueryWrapper |
Collection<? extends Serializable> | idList | 主键 ID 列表 |
Map<String, Object> | columnMap | 表字段 map 对象 |
Function<? super Object, V> | mapper | 转换函数 |
1.7.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);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
IPage | page | 翻页对象 |
Wrapper | queryWrapper | 实体对象封装操作类 QueryWrapper |
1.8.Count
// 查询总记录数
int count();@Testpublic void count(){long count = userService.count();System.out.println("count = " + count);}// 根据 Wrapper 条件,查询总记录数
int count(Wrapper<T> queryWrapper);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
Wrapper | queryWrapper | 实体对象封装操作类 QueryWrapper |
1.9.query
// 链式查询 普通
QueryChainWrapper<T> query();// 链式查询 lambda 式。注意:不支持 Kotlin
LambdaQueryChainWrapper<T> lambdaQuery();// 示例:
query().eq("column", value).one();
lambdaQuery().eq(Entity::getId, value).list();
1.10.update
// 链式更改 普通
UpdateChainWrapper<T> update();// 链式更改 lambda 式。注意:不支持 Kotlin
LambdaUpdateChainWrapper<T> lambdaUpdate();// 示例:
update().eq("column", value).remove();
lambdaUpdate().eq(Entity::getId, value).update(entity);
0.0.测试
@Resourceprivate UserServiceImpl userService;/* 总记录数*/@Testpublic void count(){long count = userService.count();System.out.println("count = " + count);}/* 添加*/@Testpublic void save(){User user = new User();user.setName("王五");user.setAge(20);user.setEmail("wangwu@qq.com");boolean result = userService.save(user);System.out.println("result = " + result);}/* 批量添加*/@Testpublic void saveBatch(){ArrayList<User> users = new ArrayList<>();for (int i = 1; i <= 2; i++) {User user = new User();user.setName("user"+i);user.setAge(20+i);user.setEmail("user"+i+"@qq.com");users.add(user);}boolean result = userService.saveBatch(users);System.out.println("result = " + result);}/* 根据id删除*/@Testpublic void removeById(){boolean result = userService.removeById(1643544155738775554L);System.out.println("result = " + result);}/* 根据多个id批量删除*/@Testpublic void removeByIds(){List<Long> ids = Arrays.asList(1643543113559080962L, 1643544155512283137L);boolean result = userService.removeByIds(ids);System.out.println("result = " + result);}/* 更新*/@Testpublic void updateById(){User user = new User();user.setId(5L);user.setName("赵六");user.setAge(28);user.setEmail("zhaoliu@qq.com");boolean result = userService.updateById(user);System.out.println("result = " + result);}/* 批量更新*/@Testpublic void updateBatchById(){ArrayList<User> users = new ArrayList<>();User user1 = new User();user1.setId(4L);user1.setName("赵六");user1.setAge(29);user1.setEmail("zhaoliu@qq.com");User user2 = new User();user2.setId(5L);user2.setName("朱八");user2.setAge(27);user2.setEmail("zhuba@qq.com");users.add(user1);users.add(user2);boolean result = userService.updateBatchById(users);System.out.println("result = " + result);}/* 根据id查询*/@Testpublic void getById(){User user = userService.getById(1L);System.out.println("user = " + user);}/* 查询所有*/@Testpublic void list(){List<User> users = userService.list();users.forEach(System.out::println);}@Testpublic void listMaps(){List<Map<String, Object>> users = userService.listMaps();users.forEach(System.out::println);}/* 根据多个id查询*/@Testpublic void listByIds(){List<Long> ids = Arrays.asList(1L, 2L, 3L);List<User> users = userService.listByIds(ids);users.forEach(System.out::println);}/* 根据map集合中的条件查询*/@Testpublic void listByMap(){Map<String, Object> map = new HashMap<>();map.put("age",28);List<User> users = userService.listByMap(map);users.forEach(System.out::println);}
2.自定义service接口方法
自定义service接口方法
需要继承Mybatis-Plus
提供的IService<T>
自定义serviceImpl
需要继承Mybatis-Plus
提供的ServiceImpl<TMaapper, T>
并 继承自定义service接口方法
2.1.Service
# UserService.java
public interface UserService extends IService<User> {
}# UserServiceImpl.java
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}