0
点赞
收藏
分享

微信扫一扫

mybatis-plus之简单查询操作、分页查询

爱读书的歌者 2022-04-25 阅读 53

查询

1.查询单个数据

	//测试查询
	@Test
	public void testSelect(){
		User user = userMapper.selectById(1L);
		System.out.println(user);
	}

2.批量查询

@Test
	public void testSelect(){
		//单个数据查询
		//User user = userMapper.selectById(1L);
		//System.out.println(user);
		//测试批量查询
		List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
		users.forEach(System.out::println);
	}

3. 测试条件查询

//测试条件查询 map
	@Test
	public void testselectmap(){
		HashMap<String, Object> map1 = new HashMap<>();
		//自定义查询
		map1.put("name","左小妹");
		List<User> users = userMapper.selectByMap(map1);
		users.forEach(System.out::println);
	}

 

//测试条件查询 map
	@Test
	public void testselectmap(){
		HashMap<String, Object> map1 = new HashMap<>();
		//自定义查询
		map1.put("name","左小妹");
		map1.put("age",18);
		List<User> users = userMapper.selectByMap(map1);
		users.forEach(System.out::println);
	}

 分页查询

1. 原始的limit分页查询

2.第三方插件(pagehelper)

3.MP中内置了分页插件

(1)配置拦截器

 

 //分页插件
    @Bean
    public PaginationInnerInterceptor paginationInnerInterceptor(){
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        return paginationInnerInterceptor;
    }

(2)直接使用Page对象即可

//测试分页插件
	@Test
	public void testPage(){
		Page<User> page = new Page<>(2,3);//当前页、页面大小
		userMapper.selectPage(page,null);
		page.getRecords().forEach(System.out::println);
	}
举报

相关推荐

0 条评论