Mybatis-Plus的条件构造器 QueryWrapper & UpdateWrapper
简介
前面我们在学习 Java Spring Boot Mybatis-Plus 的简单使用的时候,是否发现我们在构造查询的时候,基本都是简单的 where 语句的查询,而且也不能去选择字段输出,没关系,Mybatis-Plus 为我们准备了应对方案,那就是 Wrapper 构造器。
总的来说,常用的条件构造器有两类,一类是用于查询的条件构造器-QueryWrapper,一类是用于更新的条件构造器-UpdateWrapper,二者与 Wrapper 的关系如下图:
下面看看这几个类的各自作用。
Wrapper是MyBatis-Plus提供的一种查询条件封装类,用于构建查询条件。这是一个抽象类,主要有 QueryWrapper/UpdateWrapper/LambdaQueryWrapper/LambdaUpdateWrapper多个实现类,来完成查询或更新的条件构造器,由于本篇内容主要学习 QueryWrapper/UpdateWrapper,LambdaQueryWrapper/LambdaUpdateWrapper的内容请移步前往官网查阅。
AbstractWrapper,用于查询条件封装,生成 sql 的 where 条件,内部已经实现大量的条件构造语句,如 eq/lt/gt/like/orderby/groupby/in等条件构造。
QueryWrapper,Entity 对象封装操作类,用于查询。
UpdateWrapper,Update 条件封装操作类,用于更新。
通过 xxxWrapper 我们可以使用的条件构造主要有以下这些:
QueryMap 的使用
package com.example.springbootmybatisplusdemo.test; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.example.springbootmybatisplusdemo.entity.User; import com.example.springbootmybatisplusdemo.mapper.UserMapper; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.HashMap; import java.util.List; import java.util.Map; @SpringBootTest public class QueryWrapperTest { @Autowired private UserMapper userMapper; // 查询指定列, select field1, field2, ... from table_name; @Test public void testSelect() { QueryWrapper qw = new QueryWrapper<>(); qw.select("name", "age"); List<User> userList = userMapper.selectList(qw); for (User user: userList) { System.out.println(String.format("name: %s, age=%d", user.getName(), user.getAge())); } } // 带 where条件的查询, select ... from table_name where some_condition; @Test public void testWhere() { QueryWrapper qw = new QueryWrapper<>(); qw.lt("age", 20); List<User> userList = userMapper.selectList(qw); System.out.println("result: " + userList.size()); for (User user: userList) { System.out.println(user.toString()); } } // 带逻辑关系的 where条件查询,select ... from table_name where some_condition and|or ...; @Test public void testWhereLogic() { QueryWrapper qw = new QueryWrapper<>(); qw.gt("age", 20); qw.lt("age", 40); qw.likeLeft("email", "@baomidou.com"); qw.last("LIMIT 10"); List<User> userList = userMapper.selectList(qw); System.out.println("result: " + userList.size()); for (User user: userList) { System.out.println(user.toString()); } } // where子句in查询, select ... from table_name where field1 between xxx and yyy and field2 in (select field2 from table_name2); @Test public void testWhereIn() { QueryWrapper qw = new QueryWrapper<>(); qw.between("age", 10, 100); qw.inSql("age", "SELECT u.age from `user` u where age > 10 and age < 100"); List<User> userList = userMapper.selectList(qw); System.out.println("result: " + userList.size()); for (User user: userList) { System.out.println(user.toString()); } } // where子句用SQL函数等 // 通过map做参数 @Test public void testMapParams() { Map<String, Object> params = new HashMap<>(); params.put("age", 20); List<User> userList = userMapper.selectByMap(params); System.out.println("result: " + userList.size()); } // 返回List,元素为Map @Test public void testReturnMaps() { QueryWrapper qw = new QueryWrapper<>(); qw.between("age", 10, 100); List<Map<String, Object>> userList = userMapper.selectMaps(qw); System.out.println("result: " + userList.size()); for (Map<String, Object> user: userList) { System.out.println(user.toString()); } } }
UpdateWrapper的使用
package com.example.springbootmybatisplusdemo.test; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.example.springbootmybatisplusdemo.mapper.UserMapper; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest public class UpdateWrapperTest { @Autowired private UserMapper userMapper; // update wrapper 的使用,先构造更新条件,再通过 Mapper 更新 @Test public void test() { UpdateWrapper uw = new UpdateWrapper<>(); uw.eq("id", 1); uw.set("age", 81); int result = userMapper.update(uw); System.out.println("Update result: " + result); } }
参考:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律