QueryWrapper类和 UpdateWrapper类,可以实现多条件复杂查询
package com.zhmsky.mybatisplus_02; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.zhmsky.mybatisplus_02.pojo.User; import com.zhmsky.mybatisplus_02.service.UserService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; @SpringBootTest public class WrapperTest { @Autowired private UserService userService; @Test void contextLoads() { } //查询邮箱不为空且name不为空 ,年命令大于20的用户 @Test void selectUser1(){ QueryWrapperwrapper = new QueryWrapper<>(); wrapper.isNotNull("email").isNotNull("name").ge("age",20); List userList = userService.list(wrapper); for (User user : userList) { System.out.println(user.toString()); } } //只查询一个用户使用getOne @Test void selectUser2(){ QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("name","嘻嘻嘻").ge("age",20); User user = userService.getOne(wrapper); System.out.println(user.toString()); } //查询年龄在18-23的用户 @Test void selectUser3(){ QueryWrapper wrapper = new QueryWrapper<>(); wrapper.between("age",18,23); //区间 List userList = userService.list(wrapper); userList.forEach(System.out::println); } //模糊查询(查询名字不包含a的和邮箱以t开头的用户) @Test void selectUser4(){ QueryWrapper wrapper = new QueryWrapper<>(); wrapper.notLike("name","a").likeRight("email","test"); // %在左边,也就是test% List userList = userService.list(wrapper); for (User user : userList) { System.out.println(user.toString()); } } //子查询 @Test void selectUser5(){ QueryWrapper wrapper = new QueryWrapper<>(); //SELECT id,name,age,email,version,deleted,create_time,update_time FROM user WHERe deleted=0 AND (id IN (select id from user where id<3)) wrapper.inSql("id","select id from user where id<3"); List userList = userService.list(wrapper); userList.forEach(System.out::println); } //降序排序 @Test void selectUser6(){ QueryWrapper wrapper = new QueryWrapper<>(); wrapper.orderByDesc("age"); List userList = userService.list(wrapper); userList.forEach(System.out::println); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)