09- 条件查询器 Wrapper
@Test
public void testWrapper2() {
//查询 name=Tom 的用户
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("name","Tom");
//查询一个数据 selectOne,若查询出多个会报错
//Expected one result (or null) to be returned by selectOne(), but found: *
//若出现多个结果使用 list 或 map
User user = userMapper.selectOne(wrapper);//查询一个数据,若出现多个结果使用 list 或 map
System.out.println(user);
}
测试三
@Test
public void testWrapper3() {
//查询 age 在 10-20 之间的用户
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.between("age", 10, 25);//区间
Integer count = userMapper.selectCount(wrapper);//输出查询的数量 selectCount
System.out.p 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 rintln(count);
}
测试四
@Test
public void testWrapper4() {
//模糊查询
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper
.notLike("name","e") //不包含
.likeRight("email","t");//t%
List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);
maps.forEach(System.out::println);
}
测试五
@Test
public void testWrapper5() {
//模糊查询
// SELECT id,name,age,email,version,deleted,create_time,update_time
//FROM user
//WHERE deleted=0 AND id IN
//(select id from user where id<5)
QueryWrapper<User> wrapper = new QueryWrapper<>();
//id 在子查询中查出来
wrapper.inSql("id","select id from user where id<5");
List<Object> objects = userMapper.selectObjs(wrapper);
objects.forEach(System.out::println);
评论