Wrapper条件构造器
测试一:查询name不为空 ,并且邮箱不为空,年龄大于等于12的
@Test
void contextLoads(){
// 查询name不为空 ,并且邮箱不为空,年龄大于等于12的
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.isNotNull("name")
.isNotNull("email")
.ge("age",12);
userMapper.selectList(queryWrapper).forEach(System.out::println);
}
结果:
测试二:查询一个数据
@Test
void selectOne(){
// 查询姓名是Jack 的
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name","Jack");
User user = userMapper.selectOne(queryWrapper);
System.out.println(user);
}
测试三:查询一个数据
@Test
void test1(){
// 查询年龄是二十到三十岁之间的
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.between("age",20,30); // 区间
Integer integer = userMapper.selectCount(queryWrapper);// 查询结果树
System.out.println(integer);
}
测试三:模糊查询
@Test
void test2(){
// 模糊查询
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
// 左查询 %e (百分号在左边就是左查询)
// 右查询 e% (百分号在右边就是右查询)
queryWrapper
.notLike("name","e")
.likeRight("email","t");// 区间
List<Map<String, Object>> maps = userMapper.selectMaps(queryWrapper);// 查询结果树
maps.forEach(System.out::println);
}
测试四:链接查询,内查询
@Test
void test3(){
// 链接查询,内查询
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper
.inSql("id","select id from user where id < 3");
List<Object> objects = userMapper.selectObjs(queryWrapper);
objects.forEach(System.out::println);
}
测试五:排序
@Test
void test4(){
// 排序
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByAsc("age");
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
网友评论