@Testpublic void test1(){ // 查询name不为空,email不为空,age大于18的用户 QueryWrapper wrapper = new QueryWrapper(); wrapper.isNotNull("name") .isNotNull("email") .ge("age", 18); List users = userMapper.selectList(wrapper); users.forEach(System.out::println);}
2、selectOne
查询 name=Billie 的用户
如果查询结果有多个,会报错
@Test public void test2(){ // 查询 name=Billie 的用户 QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("name","Billie"); User user = userMapper.selectOne(wrapper); System.out.println(user); }
3、selectCount
根据 Wrapper 条件,查询总记录数
@Testpublic void test3(){ QueryWrapper wrapper = new QueryWrapper<>(); // 查询年龄在20到30之间;between 包含20和30 wrapper.between("age", 20, 30); Long count = userMapper.selectCount(wrapper); System.out.println(count);}
4、模糊查询
Compare 接口
like = LIKE '%值%'notLike = NOT LIKE '%值%'likeLeft = LIKE '%值'likeRight = LIKE '值%'
@Testpublic void test4(){ QueryWrapper wrapper = new QueryWrapper<>(); // 名字中包含 a,不包含 e,向左侧匹配 y wrapper.like("name", "a") .notLike("name", "e") .likeLeft("name", "y"); List
5、子查询 inSql
@Testpublic void test5(){ QueryWrapper wrapper = new QueryWrapper<>(); // id 存在于子查询中 wrapper.inSql("id", "select id from user where id < 5"); List
6、排序
@Testpublic void test6(){ QueryWrapper wrapper = new QueryWrapper<>(); // id 小于8 wrapper.lt("id",8); // 按名字升序排序 wrapper.orderByAsc("name"); List users = userMapper.selectList(wrapper); users.forEach(System.out::println);}