条件构造器
1. 测试1
@Test
void contextLoads() {
//查询 name 不为空, 并且邮箱不为空, 年龄大于等于12
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.isNotNull("name")
.isNotNull("email")
.ge("age", 12);
List<User> users = userMapper.selectList(wrapper);
for (User user : users) {
System.out.println(user);
}
}
2. 测试2
@Test
void test2() {
// 查询名字 Tom
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("name", "Tom");
System.out.println(userMapper.selectOne(wrapper)); //查询一个数据, 出现多个数据就使用list或者map
}
查询一个数据(selectOne), 出现多个数据就使用list或者map
3. 测试3
@Test
void test3() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.between("age", 20, 30);
Integer count = userMapper.selectCount(wrapper);
System.out.println(count);
}
查询数量, selectCount
4. 测试4
@Test
void test4() {
//like 模糊查询
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.notLike("name", "o")
//t开头的 t%
.likeRight("email", "t");
List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);
for (Map<String, Object> map : maps) {
System.out.println(map);
}
}
注意, like有likeLeft和likeRight
5. 测试5
@Test
void test5() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
// id 在子查询中查出来
wrapper.inSql("id", "select id from user where id < 3");
List<Object> objects = userMapper.selectObjs(wrapper);
for (Object object : objects) {
System.out.println(object);
}
}
inSql 子查询
6. 测试6
@Test
void test6() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.orderByDesc("id");
List<User> users = userMapper.selectList(wrapper);
users.forEach(System.out::println);
}