条件查询器Wrapper

    科技2025-01-10  10

    学了一下mybatisplus中的wraper,记录一下。

    package com.lzp; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.lzp.mapper.UserMapper; import com.lzp.pojo.User; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; import java.util.Map; @SpringBootTest public class WrapperTest { @Autowired UserMapper userMapper; @Test void contextLoads(){ //查询name不为空,并且邮箱不为空的用户,年龄大于等于12 QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper .isNotNull("name") .isNotNull("email") .ge("age",12); userMapper.selectList(wrapper).forEach(System.out::println); } @Test void test3(){ //查询年龄20-30之间的 QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.between("age",20,30);//区间 Integer count = userMapper.selectCount(wrapper);//查询结果数\ System.out.println(count); } //模糊查询 @Test void test4(){ QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper .notLike("name","l") .likeRight("email","t");//右查询就是t% List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);//查询结果数\ maps.forEach(System.out::println); } //内查询 @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);//查询结果数\ objects.forEach(System.out::println); } @Test void test6(){ QueryWrapper<User> wrapper = new QueryWrapper<>(); //id排序 wrapper.orderByDesc("id"); List<User> userList = userMapper.selectList(wrapper);//查询结果数\ userList.forEach(System.out::println); } }
    Processed: 0.020, SQL: 8