mybatis-plus 条件构造器 Wrapper
官网:https://mp.baomidou.com/guide/wrapper.html
作用:用于写复杂的SQL
案例
一、排序
@Test public void orderWrapper(){ QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.orderByDesc("id"); List<User> userList = userMapper.selectList(wrapper); for (User user : userList) { System.out.println(user); } }
二、模糊查询
@Test public void likeWrapper(){ QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper // like %a% .like("name","a"); // .likeRight("email","@"); User user = userMapper.selectOne(wrapper); System.out.println(user); }
三、子查询
@Test public void sonWrapper(){ QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.inSql("id", "select id from user where id < 6"); List<User> userList = userMapper.selectList(wrapper); for (User user : userList) { System.out.println(user); } }
四、between and
@Test public void betweenWrapper(){ QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.between("age", "10", "23"); List<User> userList = userMapper.selectList(wrapper); for (User user : userList) { System.out.println(user); } }
五、查询条件
与通过Map查询作用相似
@Test public void searchWrapper(){ QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.eq("name", "wt"); Integer count = userMapper.selectCount(wrapper); System.out.println(count); }