Java知识点总结:想看的可以从这里进入
目录
3.2、IService接口
BaseMapper 是用在Mapper中,而IService是在Service层使用的封装接口,它进一步封装 CRUD 。为了和BaseMapper 中方法进行区分,它采用了不同的前缀:
- get 查询单行
- remove 删除
- list 查询集合
- page 分页
- save新增
- update修改
IService还有一个实现的类ServiceImpl,在使用使用时分别对应service接口和impl实现类。
publicinterfaceUserServiceextendsIService<User>{}
@ServicepublicclassUserServiceImplextendsServiceImpl<UserMapper,User>implementsUserService{}
3.2.1、新增
- 新增一条记录
@ResourceprivateUserService userService;@TestpublicvoidtestServcie(){User user =newUser(); user.setUsername("service增加"); user.setPassword("12321");boolean save = userService.save(user);System.out.println("是否成功:"+save);}
- 批量操作
@ResourceprivateUserService userService;@TestpublicvoidtestServcie(){List<User> users =newArrayList<>();User user1 =newUser("批量增加1","123"); users.add(user1);User user2 =newUser("批量增加2","123"); users.add(user2);User user3 =newUser("批量增加3","123"); users.add(user3);User user4 =newUser("批量增加4","123"); users.add(user4);User user5 =newUser("批量增加5","123"); users.add(user5);boolean save = userService.saveBatch(users);System.out.println("是否成功:"+save);}
- 设置批次数量
publicvoidtestServcie(){List<User> users =newArrayList<>();User user1 =newUser("指定数量批量增加6","123"); users.add(user1);User user2 =newUser("指定数量批量增加7","123"); users.add(user2);User user3 =newUser("指定数量批量增加8","123"); users.add(user3);User user4 =newUser("指定数量批量增加9","123"); users.add(user4);User user5 =newUser("指定数量批量增加10","123"); users.add(user5);boolean save = userService.saveBatch(users,2);System.out.println("是否成功:"+save);}
3.2.2、查询
1、单行查询
- 根据id查询
@TestpublicvoidtestServcie(){User user = userService.getById(1);System.out.println(user);}
2、多行查询
- 根据ID批量查询
@TestpublicvoidtestServcie(){List<Integer> list =Arrays.asList(1,2,3);List<User> users = userService.listByIds(list); users.forEach(System.out::println);}
- 查询所有
@TestpublicvoidtestServcie(){//返回listList<User> list = userService.list();System.out.println(list);//返回mapList<Map<String,Object>> maps = userService.listMaps();System.out.println(maps);List<Object> objects = userService.listObjs();System.out.println(objects);}
3.2.3、删除
- 根据id删除
@TestpublicvoidtestServcie(){boolean b = userService.removeById(21);}
- 根据实体的id删除
@TestpublicvoidtestServcie(){User user = userService.getById(22);boolean b = userService.removeById(user);}
- 批量删除
@TestpublicvoidtestServcie(){List<Integer> list =Arrays.asList(23,24,25);boolean b = userService.removeByIds(list);}
- 根据Map条件删除
@TestpublicvoidtestServcie(){Map<String,Object> map =newHashMap<>(); map.put("username","批量增加5"); map.put("password","123");boolean b = userService.removeByMap(map);}
3.2.4、修改
- 根据ID修改
@TestpublicvoidtestServcie(){User user = userService.getById(27); user.setUsername("修改1"); user.setPassword("213123");boolean b = userService.updateById(user);}
- 批量修改
@TestpublicvoidtestServcie(){List<Integer> list =Arrays.asList(28,29,30);List<User> users = userService.listByIds(list); users.forEach(user ->{ user.setUsername("批量修改");});boolean b = userService.updateBatchById(users);}
3.2.5、修改或更新
3.2.6、分页
在Mybatis-plus中提供了有关分页的接口和实现类 IPage 和 Page
publicclassPage<T>implementsIPage<T>{privatestaticfinallong serialVersionUID =8545996863226528798L;//用来存放查询出来的数据protectedList<T> records =Collections.emptyList();//返回的数据总数protectedlong total =0;// 每页显示条数,默认 10protectedlong size =10;//当前页,默认1protectedlong current =1;// 排序字段信息@SetterprotectedList<OrderItem> orders =newArrayList<>();//自动优化 COUNT SQLprotectedboolean optimizeCountSql =true;// 是否进行 count 查询protectedboolean searchCount =true;publicPage(){}/**
* 有参构造函数
* @param current 当前页
* @param size 每页显示条数
*/publicPage(long current,long size){this(current, size,0);}publicPage(long current,long size,long total){this(current, size, total,true);}publicPage(long current,long size,boolean searchCount){this(current, size,0, searchCount);}publicPage(long current,long size,long total,boolean searchCount){if(current >1){this.current = current;}this.size = size;this.total = total;this.searchCount = searchCount;}//是否存在上一页publicbooleanhasPrevious(){returnthis.current >1;}//是否存在下一页publicbooleanhasNext(){returnthis.current <this.getPages();}..........}
- 配置- 使用Spring时,在Spring的配置文件中先配置mybatis-plus内置的分页插件
<!-- 配置mybatis-plus的分页拦截器--><beanid="paginationInnerInterceptor"class="com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor"/><!-- 配置mybatis-plus的分页拦截器插件 --><beanid="mybatisPlusInterceptor"class="com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor"><propertyname="interceptors"><list><refbean="paginationInnerInterceptor"/></list></property></bean><!-- 将插件加入到MybatisSqlSessionFactoryBean 中--><beanclass="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean"><!--加载连接池--><propertyname="dataSource"ref="dataSource"/><!-- 设置MyBatis配置文件的路径(可以不设置) --><propertyname="configLocation"value="classpath:mybatis-config.xml"/><!--加载映射文件路径--><propertyname="mapperLocations"value="classpath:mapping/*.xml"/><!--分页插件生效 --><propertyname="plugins"><array><refbean="mybatisPlusInterceptor"/></array></property></bean>
- 使用SpringBoot时配置时,在配置类中配置@BeanpublicMybatisPlusInterceptormybatisPlusInterceptor(){MybatisPlusInterceptor interceptor =newMybatisPlusInterceptor(); interceptor.addInnerInterceptor(newPaginationInnerInterceptor(DbType.MYSQL));return interceptor;}
- 分页查询数据
@TestpublicvoidtestServcie(){Page<User> page = userService.page(newPage<>(1,5));System.out.println("总数据:"+page.getTotal());List<User> users = page.getRecords(); users.forEach(System.out::println);}
3.2.7、查询记录数
@TestpublicvoidtestServcie(){long count = userService.count();System.out.println(count);}
版权归原作者 辰 羽 所有, 如有侵权,请联系我们删除。