MyBatisPlus_条件构造器

条件构造器

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);
    }
}

image-20201016145457244

2. 测试2

@Test
void test2() {
    // 查询名字 Tom
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper.eq("name", "Tom");
    System.out.println(userMapper.selectOne(wrapper));  //查询一个数据, 出现多个数据就使用list或者map
}

image-20201016151514674

查询一个数据(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);
}

image-20201016151843697

查询数量, 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);
    }
}

image-20201016152544415

注意, 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);
    }
}

image-20201016153230600

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);
}

image-20201016153830879

posted @ 2020-10-19 09:59  山人西来  阅读(429)  评论(0编辑  收藏  举报