简单的查询如下:
//单个id查询 @Test public void testSelectDemo() { User user = this.userMapper.selectById(1l); System.out.println(user); } //多个id查询 @Test public void testSelectDemo1() { List users = this.userMapper.selectBatchIds(Arrays.asList(1l, 2l, 3l)); System.out.println(users); } //根据条件查询 @Test public void testSelectDemo2() { Map map = new HashMap<String, Object>(); map.put("name", "独孤求败"); map.put("age" , 19); this.userMapper.selectByMap(map); }
分页查询的操作步骤是:
1.配置分页插件(在启动类或者怕配置类中,推荐配置类)
// 在新版mp中已过时 // @Bean // public PaginationInterceptor paginationInterceptor() { // return new PaginationInterceptor(); // } //分页插件 @Bean public MybatisPlusInterceptor paginationInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2)); return interceptor; }
2.new一个page对象,传入当前页、每页显示记录数。调用mp中的分页方法。
//分页查询 @Test public void testPage() { Page <User> page = new Page<>(1, 3); // mp底层会把分页查询到的结果封装到page中 this.userMapper.selectPage(page, null); System.out.println("页数:" + page.getPages()); System.out.println("当前页:" + page.getCurrent()); System.out.println("当前数据的List集合:" + page.getRecords()); System.out.println("每页记录数:" + page.getSize()); System.out.println("记录总数:" + page.getTotal()); System.out.println("是否有下一页:" + page.hasNext()); System.out.println("是否有上一页:" + page.hasPrevious()); }