Java教程

MybatisPlus学习-条件构造器

本文主要是介绍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);
    }
}
这篇关于MybatisPlus学习-条件构造器的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!