条件查询
@SpringBootTest public class WrapperTest { @Autowired private UserMapper userMapper; @Test void contextLoads() { QueryWrapper<User> wrapper = new QueryWrapper<>(); //查询name和email不为空,年龄大于等于12。 wrapper .isNotNull("name") .isNotNull("email") .ge("age", 12); userMapper.selectList(wrapper).forEach(System.out::println); } @Test void test1() { QueryWrapper<User> wrapper = new QueryWrapper<>(); //查询年龄在20到30之间 wrapper.between("age", 20, 30); Integer count = userMapper.selectCount(wrapper); System.out.println(count); } @Test void test2() { QueryWrapper<User> wrapper = new QueryWrapper<>(); //查询名字=XXX wrapper.eq("name", "兰姐姐"); userMapper.selectList(wrapper).forEach(System.out::println); } }
模糊查询
//模糊查询 @Test void tet4() { QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper .notLike("name", "e") // %e% .likeRight("email", "t"); // % 在左和右 t% List<Map<String, Object>> maps = userMapper.selectMaps(wrapper); maps.forEach(System.out::println); } }
void test5() { QueryWrapper<User> wrapper = new QueryWrapper<>(); //id 在字查询中查出来 wrapper.inSql("id", "select id from user where id>2"); List<Object> objects = userMapper.selectObjs(wrapper); objects.forEach(System.out::println); }
排序
@Test void test6() { QueryWrapper<User> wrapper = new QueryWrapper<>(); // 通过 id 进行排序 wrapper.orderByDesc("id"); List<Object> objects = userMapper.selectObjs(wrapper); objects.forEach(System.out::println); }