service接口
通用 Service CRUD 封装IService (opens new window)接口,进一步封装 CRUD 采用 get 查询单行 remove 删除 list 查询集合 page 分页 前缀命名方式区分 Mapper 层避免混淆,
1.Save 插入
参数说明
类型参数名描述Tentity实体对象CollectionentityList实体对象集合intbatchSize插入批次数量
1 2 3 4 5 6 7 8 9 10 | // 插入一条记录(选择字段,策略插入) boolean save(T entity); // 插入(批量) boolean saveBatch(Collection entityList); // 插入(批量) boolean saveBatch(Collection entityList, int batchSize); /不存在则插入 否在更新 boolean saveOrUpdate(T entity); // 批量修改插入 boolean saveOrUpdateBatch(Collection entityList); |
测试:
1.1单个添加测试
1 2 3 4 5 6 7 8 9 10 | /** * 单个添加测试 */ @Test public void save(){ Department department= new Department(); department.setName( "牛逼" ); department.setRemark( "xxx" ); boolean save = departmentService.save(department); } |
1.2 批量添加测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** * 批量添加测试 */ @Test public void saveBatch(){ Department department1= new Department(); department1.setName( "牛逼1" ); department1.setRemark( "xxx1" ); Department department2= new Department(); department2.setName( "牛逼2" ); department2.setRemark( "xxx2" ); Department department3= new Department(); department3.setName( "牛逼3" ); department3.setRemark( "xxx3" ); boolean b = departmentService.saveBatch(Arrays.asList(department1, department2, department3)); } |
1.3批量添加或者更新
判断ID是否存在,如果ID不存在执行新增,如果ID存在先执行查询语句,查询结果为空新增,否则修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /** * 批量添加或者更新 */ @Test public void saveOrUpdateBatch() { User user1 = new User(); user1.setName( "兔子" ); user1.setAge( 18 ); User user2 = new User(); user2.setId(1088250446457389060L); user2.setName( "乌龟" ); user2.setAge( 18 ); List users = Arrays.asList(user1, user2); boolean b = userService.saveOrUpdateBatch(users); System.out.println(b);} |
2.Remove 删除
1 2 3 4 5 6 7 8 | // 根据 entity 条件,删除记录 boolean remove(Wrapper queryWrapper); // 根据 ID 删除 boolean removeById(Serializable id); // 根据 columnMap 条件,删除记录 boolean removeByMap(Map columnMap); // 删除(根据ID 批量删除) boolean removeByIds(Collection extends Serializable> idList); |
2.1.通过id删除
1 2 3 4 5 6 7 | /** * 通过id删除 */ @Test public void removeById(){ boolean b = departmentService.removeById( 9 ); } |
2.2根据 entity 条件,删除记录
1 2 3 4 5 6 7 8 9 10 | @Test public void test1() { User user= new User(); user.setAge( 5 ); user.setName( "猫" ); QueryWrapper queryWrapper= new QueryWrapper(); queryWrapper.setEntity(user); boolean count=testService.remove(queryWrapper); System.out.println(count); //true } |
2.3根据 columnMap 条件,删除记录
1 2 3 4 5 6 7 8 9 | @Test public void test2() { HashMap map = new HashMap(); //自定义查询条件,下面的值全部满足才能成功 map.put( "name" , "狗" ); map.put( "age" , 5 ); boolean b = testService.removeByMap(map); System.out.println(b); // true } |
2.4批量删除
1 2 3 4 5 6 7 8 | @Test public void test3() { List list= new ArrayList(); list.add( "1495949722160246786" ); list.add( "1495950143763296258" ); boolean b = testService.removeByIds(list); System.out.println(b); } |
3.Update修改
1 2 3 4 5 6 | // 根据 whereWrapper 条件,更新记录 boolean update(T updateEntity, Wrapper whereWrapper); // 根据 ID 选择修改 boolean updateById(T entity); // 根据ID 批量更新 boolean updateBatchById(Collection entityList); |
3.1 根据 ID 选择修改
1 2 3 4 5 6 7 8 9 | @Test public void updateById(){ Department department= new Department(); department.setId( 9 ); department.setName( "牛逼2" ); department.setRemark( "xxx2" ); boolean save = departmentService.updateById(department); System.out.println(save); } |
3.2通过实体+UpdateWrapper做修改
1 2 3 4 5 6 7 8 9 | @Test public void test4() { UpdateWrapper userUpdateWrapper= new UpdateWrapper(); userUpdateWrapper.eq( "email" , "1314@qq.com" ); User user= new User(); user.setAge( 25 ); user.setName( "李白" ); testService.update(user,userUpdateWrapper); } |
4.Get 查询一条记录
1 2 3 4 5 6 | // 根据 ID 查询 T getById(Serializable id); // 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1") T getOne(Wrapper queryWrapper); // 根据 Wrapper,查询一条记录 Map getMap(Wrapper queryWrapper); |
4.1根据id查询记录
根据 主键 ID 返回数据
1 2 3 4 5 | @Test public void test5() { System.out.println(testService.getById( "1495950143763296266" )); //User(id=1495950143763296266, age=25, name=张飞, email=1314@qq.com, deleted=0, createTime=Tue Feb 22 14:38:22 CST 2022, updateTime=Sun Apr 24 17:06:12 CST 2022) } |
4.2根据 Wrapper,查询一条记录
返回一条记录(实体类保存)
1 2 3 4 5 6 7 | @Test public void test6() { QueryWrapper queryWrapper= new QueryWrapper(); queryWrapper.eq( "name" , "张飞" ); // queryWrapper.last("LIMIT 1") //如果是多个会抛出异 常加上限制条件 System.out.println(testService.getOne(queryWrapper)); } |
4.3返回一条记录(map 保存)
1 2 3 4 5 6 7 8 | @Test public void test7() { QueryWrapper queryWrapper= new QueryWrapper(); queryWrapper.ne( "age" , 5 ) .eq( "email" , "1314@qq.com" ); Map map=testService.getMap(queryWrapper); System.out.println(map); } |
5.获取条数
5.1获取所有数据条数 也就是条件为null
1 2 3 4 | @Test public void test8() { System.out.println(testService.count( null )); // 21 } |
5.2 根据条件返回记录数
1 2 3 4 5 6 | @Test public void test9() { QueryWrapper queryWrapper= new QueryWrapper(); queryWrapper.eq( "name" , "张飞" ); System.out.println(testService.count(queryWrapper)); // 7 } |
6.返回多条记录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | // 查询所有 List list(); // 查询列表 List list(Wrapper queryWrapper); // 查询(根据ID 批量查询) Collection listByIds(Collection extends Serializable> idList); // 查询(根据 columnMap 条件) Collection listByMap(Map columnMap); // 查询所有列表 List<map>> listMaps(); // 查询列表 List<map>> listMaps(Wrapper queryWrapper); // 查询全部记录 List<object data-origwidth= "" data-origheight= "" style= "width: 473px;" > listObjs(); // 查询全部记录 List listObjs(Function super Object, V> mapper); // 根据 Wrapper 条件,查询全部记录 List<object data-origwidth= "" data-origheight= "" style= "width: 473px;" > listObjs(Wrapper queryWrapper); // 根据 Wrapper 条件,查询全部记录 List listObjs(Wrapper queryWrapper, Function super Object, V> mapper);</object></object></map></map> |
6.1无条件查询所有数据
1 2 3 4 | @Test public void test10() { System.out.println(testService.list( null )); } |
结果:
[User(id=1495690228859723778, age=5, name=猫, email=2424496907@qq.com, deleted=0, createTime=null, updateTime=null), User(id=1495933979356090370, age=25, name=猫, email=2424496907@qq.com, deleted=0, createTime=null, updateTime=null), User(id=1495934171253850114, age=25, name=猫飞, email=1314@qq.com, deleted=0, createTime=null, updateTime=Sun Apr 24 17:06:12 CST 2022), User(id=1495950143763296262, age=38, name=王k松, email=2423435907@qq.com, deleted=0, createTime=Tue Feb 22 13:44:24 CST 2022, updateTime=Tue Feb 22 13:44:24 CST 2022), User(id=1495950143763296263, age=25, name=刘备, email=1314@qq.com, deleted=0, createTime=Tue createTime=Thu Feb 24 14:58:07 CST 2022, updateTime=Thu Feb 24 14:58:07 CST 2022)]
6.2按条件查询数据
1 2 3 4 5 6 7 | @Test public void test11() { QueryWrapper queryWrapper= new QueryWrapper(); queryWrapper.eq( "name" , "张飞" ) .ge( "age" , 25 ); System.out.println(testService.list(queryWrapper)); } |
结果:
[User(id=1495935752888176642, age=32, name=张飞, email=1314@qq.com, deleted=0, createTime=null, updateTime=Sun Apr 24 17:06:12 CST 2022), User(id=1495937693445828610, age=39, name=张飞, email=1314@qq.com, deleted=0, createTime=null, updateTime=Sun Apr 24 17:06:12 CST 2022), User(id=1495950143763296265, age=45, name=张飞, email=1314@qq.com, deleted=0, createTime=Tue Feb 22 14:33:53 CST 2022, updateTime=Sun Apr 24 17:06:12 CST 2022), User(id=1495950143763296266, age=25, name=张飞, email=1314@qq.com, deleted=0, createTime=Tue Feb 22 14:38:22 CST 2022, updateTime=Sun Apr 24 17:06:12 CST 2022), User(id=1495950143763296268, age=50, name=张飞, email=1314@qq.com, deleted=0, createTime=Tue Feb 22 14:57:17 CST 2022, updateTime=Sun Apr 24 17:06:12 CST 2022), User(id=1495950143763296269, age=25, name=张飞, email=1314@qq.com, deleted=0, createTime=Tue Feb 22 14:59:50 CST 2022, updateTime=Sun Apr 24 17:06:12 CST 2022)]
6.3 返回为map集合
调用 BaseMapper 的 selectMaps 方法,查询所有记录(返回 map 集合)。
1 2 3 4 5 6 7 | @Test public void test12() { QueryWrapper queryWrapper= new QueryWrapper(); queryWrapper.eq( "name" , "张飞" ) .ge( "age" , 25 ); System.out.println(testService.listMaps(queryWrapper)); } |
结果:
[{update_time=2022-04-24T17:06:12, deleted=0, name=张飞, id=1495935752888176642, age=32, email=1314@qq.com}, {update_time=2022-04-24T17:06:12, deleted=0, name=张飞, id=1495937693445828610, age=39, email=1314@qq.com}, {update_time=2022-04-24T17:06:12, deleted=0, create_time=2022-02-22T14:33:53, name=张飞, id=1495950143763296265, age=45, email=1314@qq.com}, {update_time=2022-04-24T17:06:12, deleted=0, create_time=2022-02-22T14:38:22, name=张飞, id=1495950143763296266, age=25, email=1314@qq.com}, {update_time=2022-04-24T17:06:12, deleted=0, create_time=2022-02-22T14:57:17, name=张飞, id=1495950143763296268, age=50, email=1314@qq.com}, {update_time=2022-04-24T17:06:12, deleted=0, create_time=2022-02-22T14:59:50, name=张飞, id=1495950143763296269, age=25, email=1314@qq.com}]
-
get
用于返回一条记录。 -
list
用于返回多条记录。 -
count
用于返回记录总数。 -
page
用于分页查询。
【添加数据】
1 2 | default boolean save(T entity); // 调用 BaseMapper 的 insert 方法,用于添加一条数据。 boolean saveBatch(Collection entityList, int batchSize); // 批量插入数据 |
注:
-
entityList
表示实体对象集合 -
batchSize
表示一次批量插入的数据量,默认为 1000
【添加或修改数据:(增或改)】
1 2 3 | boolean saveOrUpdate(T entity); // id 若存在,则修改, id 不存在则新增数据 default boolean saveOrUpdate(T entity, Wrapper updateWrapper); // 先根据条件尝试更新,然后再执行 saveOrUpdate 操作 boolean saveOrUpdateBatch(Collection entityList, int batchSize); // 批量插入并修改数据 |
【删除数据:(删)】
1 2 3 4 | default boolean removeById(Serializable id); // 调用 BaseMapper 的 deleteById 方法,根据 id 删除数据。 default boolean removeByMap(Map columnMap); // 调用 BaseMapper 的 deleteByMap 方法,根据 map 定义字段的条件删除 default boolean remove(Wrapper queryWrapper); // 调用 BaseMapper 的 delete 方法,根据实体类定义的 条件删除对象。 default boolean removeByIds(Collection extends Serializable> idList); // 用 BaseMapper 的 deleteBatchIds 方法, 进行批量删除。 |
【修改数据:(改)】
1 2 3 | default boolean updateById(T entity); // 调用 BaseMapper 的 updateById 方法,根据 ID 选择修改。 default boolean update(T entity, Wrapper updateWrapper); // 调用 BaseMapper 的 update 方法,根据 updateWrapper 条件修改实体对象。 boolean updateBatchById(Collection entityList, int batchSize); // 批量更新数据 |
【查找数据:(查)】
1 2 3 4 5 6 7 8 9 10 11 12 | default T getById(Serializable id); // 调用 BaseMapper 的 selectById 方法,根据 主键 ID 返回数据。 default List listByIds(Collection extends Serializable> idList); // 调用 BaseMapper 的 selectBatchIds 方法,批量查询数据。 default List listByMap(Map columnMap); // 调用 BaseMapper 的 selectByMap 方法,根据表字段条件查询 default T getOne(Wrapper queryWrapper); // 返回一条记录(实体类保存)。 Map getMap(Wrapper queryWrapper); // 返回一条记录(map 保存)。 default int count(Wrapper queryWrapper); // 根据条件返回 记录数。 default List list(); // 返回所有数据。 default List list(Wrapper queryWrapper); // 调用 BaseMapper 的 selectList 方法,查询所有记录(返回 entity 集合)。 default List<map>> listMaps(Wrapper queryWrapper); // 调用 BaseMapper 的 selectMaps 方法,查询所有记录(返回 map 集合)。 default List<object data-origwidth= "" data-origheight= "" style= "width: 679px;" > listObjs(); // 返回全部记录,但只返回第一个字段的值。 default > E page(E page, Wrapper queryWrapper); // 调用 BaseMapper 的 selectPage 方法,分页查询 default >> E pageMaps(E page, Wrapper queryWrapper); // 调用 BaseMapper 的 selectMapsPage 方法,分页查询</object></map> |
【链式调用:】
1 2 3 4 | default QueryChainWrapper query(); // 普通链式查询 default LambdaQueryChainWrapper lambdaQuery(); // 支持 Lambda 表达式的修改 default UpdateChainWrapper update(); // 普通链式修改 default LambdaUpdateChainWrapper lambdaUpdate(); // 支持 Lambda 表达式的修改 |
注:
-
query
表示查询 -
update
表示修改 -
Lambda
表示内部支持 Lambda 写法。
形如:
1 2 3 4 | query().eq( "column" , value).one(); lambdaQuery().eq(Entity::getId, value).list(); update().eq( "column" , value).remove(); lambdaUpdate().eq(Entity::getId, value).update(entity); |
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持IT俱乐部。