Python 官方文档:入门教程 => 点击学习
目录1.组装查询条件2.组装排序条件3.使用Lambda表达式解决条件优先级4.组装select语句5.组装子查询6.使用UpdateWrapper实现修改功能7.模拟开发
组装查询其实很简单,可以支持条件的链式编程:
查询用户名包含a,年龄在 10 - 20 之间并且邮箱不为空的用户:
@Test
void contextLoads() {
// 查询用户名包含a,年龄在 10 - 20 之间并且邮箱不为空的用户
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.like("name","a")
.between("age",10,20)
.isNotNull("email");
List<User> users = userMapper.selectList(wrapper);
users.forEach(System.out::println);
}
==> Preparing: SELECT id,name,age,email,is_delete FROM user WHERE is_delete=0 AND (name LIKE ? AND age BETWEEN ? AND ? AND email IS NOT NULL)
==> Parameters: %a%(String), 10(Integer), 20(Integer)
<== Columns: id, name, age, email, is_delete
<== Row: 2, dada, 11, 111@qq.com, 0
<== Row: 4, dahe, 12, 34567@qq.com, 0
<== Total: 2
查询用户信息,按照年龄的降序排序,若年龄相同,则按照id升序排序:
@Test
void contextLoads() {
// 查询用户信息,按照年龄的降序排序,若年龄相同,则按照id升序排序
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.orderByDesc("age").orderByAsc("id");
List<User> users = userMapper.selectList(wrapper);
users.forEach(System.out::println);
}
==> Preparing: SELECT id,name,age,email,is_delete FROM user WHERE is_delete=0 ORDER BY age DESC,id ASC
==> Parameters:
<== Columns: id, name, age, email, is_delete
<== Row: 3, dahe, 33, 34567@qq.com, 0
<== Row: 4, dahe, 12, 34567@qq.com, 0
<== Row: 2, dada, 11, 111@qq.com, 0
<== Total: 3
将用户名中包含有a并且(年龄大于20或邮箱为null)的用户信息查询出来:
Lambda中的条件优先执行!
@Test
void contextLoads() {
// 将用户名中包含有a并且(年龄大于20或邮箱为null)的用户信息查询出来
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.like("name", "a")
.and(i -> i.gt("age", 20).or().isNull("email"));
List<User> users = userMapper.selectList(wrapper);
users.forEach(System.out::println);
}
==> Preparing: SELECT id,name,age,email,is_delete FROM user WHERE is_delete=0 AND (name LIKE ? AND (age > ? OR email IS NULL))
==> Parameters: %a%(String), 20(Integer)
<== Columns: id, name, age, email, is_delete
<== Row: 3, dahe, 33, 34567@qq.com, 0
<== Total: 1
有时我们不想查询数据库某张表的所有字段,可以使用maps集合选择只查询某些字段的值:
@Test
void contextLoads() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.select("name","email");
List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);
maps.forEach(System.out::println);
}
==> Preparing: SELECT name,email FROM user WHERE is_delete=0
==> Parameters:
<== Columns: name, email
<== Row: dada, 111@qq.com
<== Row: dahe, 34567@qq.com
<== Row: dahe, 34567@qq.com
<== Total: 3
Closing non transactional sqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@237add]
{name=dada, email=111@qq.com}
{name=dahe, email=34567@qq.com}
{name=dahe, email=34567@qq.com}
@Test
void contextLoads() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.inSql("id","select id from user where id > 1");
List<User> users = userMapper.selectList(wrapper);
users.forEach(System.out::println);
}
==> Preparing: SELECT id,name,age,email,is_delete FROM user WHERE is_delete=0 AND (id IN (select id from user where id > 1))
==> Parameters:
<== Columns: id, name, age, email, is_delete
<== Row: 2, dada, 11, 111@qq.com, 0
<== Row: 3, dahe, 33, 34567@qq.com, 0
<== Row: 4, dahe, 12, 34567@qq.com, 0
<== Total: 3
将名字中包含a的记录修改:
@Test
void contextLoads() {
// 将名字中包含a的记录修改
UpdateWrapper<User> wrapper = new UpdateWrapper<>();
wrapper.like("name","a");
wrapper.set("name","hello").set("age",18);
userMapper.update(null, wrapper);
}
JDBC Connection [com.Mysql.cj.jdbc.ConnectionImpl@36c07c75] will not be managed by spring
==> Preparing: UPDATE user SET name=?,age=? WHERE is_delete=0 AND (name LIKE ?)
==> Parameters: hello(String), 18(Integer), %a%(String)
<== Updates: 3
真实的开发中往往有这样的情况:我们不知道前端传来的条件是什么,此时无法正常的拼接sql,那么就需要在后端进行相应的判断:
@Test
void contextLoads() {
String name = null;
Integer ageBegin = 18;
Integer ageEnd = 28;
QueryWrapper<User> wrapper = new QueryWrapper<>();
if (StringUtils.isNotBlank(name)) {
wrapper.like("name", name);
}
if (ageBegin != null) {
wrapper.ge("age", ageBegin);
}
if (ageEnd != null) {
wrapper.le("age", ageEnd);
}
List<User> users = userMapper.selectList(wrapper);
users.forEach(System.out::println);
}
==> Preparing: SELECT id,name,age,email,is_delete FROM user WHERE is_delete=0 AND (age >= ? AND age <= ?)
==> Parameters: 18(Integer), 28(Integer)
<== Columns: id, name, age, email, is_delete
<== Row: 3, hello, 18, 34567@qq.com, 0
<== Row: 4, hello, 18, 34567@qq.com, 0
<== Total: 2
但是,这样的判断我们认为过于的简陋和复杂,事实上,mybatis Plus的条件构造器中为我们封装了解决此类问题的一些重载方法:
@Test
void contextLoads() {
String name = null;
Integer ageBegin = 18;
Integer ageEnd = 28;
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.like(StringUtils.isNotBlank(name), "name", name);
wrapper.ge(ageBegin != null, "age", ageBegin);
wrapper.le(ageEnd != null, "age", ageEnd);
List<User> users = userMapper.selectList(wrapper);
users.forEach(System.out::println);
}
==> Preparing: SELECT id,name,age,email,is_delete FROM user WHERE is_delete=0 AND (age >= ? AND age <= ?)
==> Parameters: 18(Integer), 28(Integer)
<== Columns: id, name, age, email, is_delete
<== Row: 3, hello, 18, 34567@qq.com, 0
<== Row: 4, hello, 18, 34567@qq.com, 0
<== Total: 2
使用LambdaQueryWrapper
和LambdaUpdateWrapper
可以防止我们的字段名修改导致的程序错误或字段名写错
例如:
@Test
void contextLoads() {
String name = null;
Integer ageBegin = 18;
Integer ageEnd = 28;
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
wrapper.like(StringUtils.isNotBlank(name), User::getName, name);
wrapper.ge(ageBegin != null, User::getAge, ageBegin);
wrapper.le(ageEnd != null, User::getAge, ageEnd);
List<User> users = userMapper.selectList(wrapper);
users.forEach(System.out::println);
}
==> Preparing: SELECT id,name,age,email,is_delete FROM user WHERE is_delete=0 AND (age >= ? AND age <= ?)
==> Parameters: 18(Integer), 28(Integer)
<== Columns: id, name, age, email, is_delete
<== Row: 3, hello, 18, 34567@qq.com, 0
<== Row: 4, hello, 18, 34567@qq.com, 0
<== Total: 2
LambdaUpdateWrapper
的使用方法类似
到此这篇关于MyBatis Plus的条件构造器的文章就介绍到这了,更多相关MyBatis Plus的条件构造器内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: 一文掌握MyBatis Plus的条件构造器方法
本文链接: https://lsjlt.com/news/195271.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0