Java知识点总结:想看的可以从这里进入
BaseMapper 是用在Mapper中,而IService是在Service层使用的封装接口,它进一步封装 CRUD 。为了和BaseMapper 中方法进行区分,它采用了不同的前缀:
IService还有一个实现的类ServiceImpl,在使用使用时分别对应service接口和impl实现类。
public interface UserService extends IService{ }
@Service public class UserServiceImpl extends ServiceImplimplements UserService { }
新增一条记录
@Resource private UserService userService; @Test public void testServcie(){ User user = new User(); user.setUsername("service增加"); user.setPassword("12321"); boolean save = userService.save(user); System.out.println("是否成功:"+save); }
批量操作
@Resource private UserService userService; @Test public void testServcie(){ Listusers = new ArrayList<>(); User user1 = new User("批量增加1","123"); users.add(user1); User user2 = new User("批量增加2","123"); users.add(user2); User user3 = new User("批量增加3","123"); users.add(user3); User user4 = new User("批量增加4","123"); users.add(user4); User user5 = new User("批量增加5","123"); users.add(user5); boolean save = userService.saveBatch(users); System.out.println("是否成功:"+save); }
设置批次数量
public void testServcie(){ Listusers = new ArrayList<>(); User user1 = new User("指定数量批量增加6","123"); users.add(user1); User user2 = new User("指定数量批量增加7","123"); users.add(user2); User user3 = new User("指定数量批量增加8","123"); users.add(user3); User user4 = new User("指定数量批量增加9","123"); users.add(user4); User user5 = new User("指定数量批量增加10","123"); users.add(user5); boolean save = userService.saveBatch(users,2); System.out.println("是否成功:"+save); }
根据id查询
@Test public void testServcie(){ User user = userService.getById(1); System.out.println(user); }
根据ID批量查询
@Test public void testServcie(){ Listlist = Arrays.asList(1, 2, 3); List users = userService.listByIds(list); users.forEach(System.out::println); }
查询所有
@Test public void testServcie(){ //返回list Listlist = userService.list(); System.out.println(list); //返回map List
根据id删除
@Test public void testServcie(){ boolean b = userService.removeById(21); }
根据实体的id删除
@Test public void testServcie(){ User user = userService.getById(22); boolean b = userService.removeById(user); }
批量删除
@Test public void testServcie(){ Listlist = Arrays.asList(23, 24, 25); boolean b = userService.removeByIds(list); }
根据Map条件删除
@Test public void testServcie(){ Mapmap = new HashMap<>(); map.put("username","批量增加5"); map.put("password","123"); boolean b = userService.removeByMap(map); }
根据ID修改
@Test public void testServcie(){ User user = userService.getById(27); user.setUsername("修改1"); user.setPassword("213123"); boolean b = userService.updateById(user); }
批量修改
@Test public void testServcie(){ Listlist = Arrays.asList(28, 29, 30); List users = userService.listByIds(list); users.forEach(user -> { user.setUsername("批量修改"); }); boolean b = userService.updateBatchById(users); }
在Mybatis-plus中提供了有关分页的接口和实现类 IPage 和 Page
public class Pageimplements IPage { private static final long serialVersionUID = 8545996863226528798L; //用来存放查询出来的数据 protected List records = Collections.emptyList(); //返回的数据总数 protected long total = 0; // 每页显示条数,默认 10 protected long size = 10; //当前页,默认1 protected long current = 1; // 排序字段信息 @Setter protected List orders = new ArrayList<>(); //自动优化 COUNT SQL protected boolean optimizeCountSql = true; // 是否进行 count 查询 protected boolean searchCount = true; public Page() { } /** * 有参构造函数 * @param current 当前页 * @param size 每页显示条数 */ public Page(long current, long size) { this(current, size, 0); } public Page(long current, long size, long total) { this(current, size, total, true); } public Page(long current, long size, boolean searchCount) { this(current, size, 0, searchCount); } public Page(long current, long size, long total, boolean searchCount) { if (current > 1) { this.current = current; } this.size = size; this.total = total; this.searchCount = searchCount; } //是否存在上一页 public boolean hasPrevious() { return this.current > 1; } //是否存在下一页 public boolean hasNext() { return this.current < this.getPages(); } .......... }
配置
使用Spring时,在Spring的配置文件中先配置mybatis-plus内置的分页插件
使用SpringBoot时配置时,在配置类中配置
@Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; }
分页查询数据
@Test public void testServcie(){ Pagepage = userService.page(new Page<>(1,5)); System.out.println("总数据:"+page.getTotal()); List users = page.getRecords(); users.forEach(System.out::println); }
@Test public void testServcie(){ long count = userService.count(); System.out.println(count); }