MybatisPlus学习-条件构造器

测试用例

@SpringBootTest
public class WrapperTest {

    @Autowired
    private UserMapper userMapper;

    @Test
    void contextLoads(){
        //age大于12
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.isNotNull("name")
                .isNotNull("email")
                .ge("age",12);
        userMapper.selectList(wrapper).forEach(System.out::println);
    }

    @Test
    void test2(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name","gui");
        User user = userMapper.selectOne(wrapper);//查询一个,多个用selectList
        System.out.println(user);
    }

    @Test
    void test3(){
        //age在20~30
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.between("age",20,30);

        Integer count = userMapper.selectCount(wrapper);
        System.out.println(count);
    }

    @Test
    void test4(){
        //age在20~30
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.notLike("name","e") //不存在e
                .likeRight("email","1");//以1开始
        userMapper.selectMaps(wrapper).forEach(System.out::println);
    }

    @Test
    void test5(){
        //age在20~30
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //id 在子查询查出来
        wrapper.inSql("id","select id from user where id<3");

        List<Object> objects = userMapper.selectObjs(wrapper);
        objects.forEach(System.out::println);
    }

    @Test
    void test6(){
        //id 降序排序
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");

        List<User> users = userMapper.selectList(wrapper);
        users.forEach(System.out::println);
    }
}