MybatisPlus中QueryWrapper常用方法总结
作者:fking86
MyBatis-Plus是一个Mybatis增强版工具,在MyBatis上扩充了其他功能没有改变其基本功能,为了简化开发提交效率而存在,queryWrapper是mybatis plus中实现查询的对象封装操作类,本文就给大家总结了MybatisPlus中QueryWrapper的常用方法,需要的朋友可以参考下
QueryWrapper常用方法列表
函数 | 说明 | 例子 |
---|---|---|
eq | 等于(=) | queryWrapper.eq(“user_name”, “张三”); //user_name=“张三” |
ne | 不等于(<>) | queryWrapper.ne(“user_name”, “张三”); //user_name<>“张三” |
gt | 大于(>) | queryWrapper.gt(“level”, 1); //level>1 |
ge | 大于等于(>=) | queryWrapper.ge(“level”, 1); //level>=1 |
lt | 小于(<) | queryWrapper.lt(“level”, 1); //level<1 |
le | 小于等于(<=) | queryWrapper.le(“level”, 1); //level<=1 |
between | 值1和值2之间 | queryWrapper.between(“level”, 1,10); //level>=1 and level <=10 |
notBetween | 不在值1和值2之间 | queryWrapper.notBetween(“level”, 1,10);//level<1 or level >10 |
like | 模糊匹配 like %值% | queryWrapper.like(“user_name”, “张三”); //user_name like “%张三%” |
not like | 不模糊匹配 not like %值% | queryWrapper.notLike(“user_name”, “张三”); //user_name not like “%张三%” |
likeLeft | 左模糊匹配 like %值 | queryWrapper.likeLeft(“user_name”, “张三”); //user_name like “%张三” |
likeRight | 右模糊匹配 like 值% | queryWrapper.likeRight(“user_name”, “张三”); //user_name like “张三%” |
isNull | 字段 为空 | queryWrapper.isNull(“user_name”);//user_name is null |
isNotNull | 字段 不为空 | queryWrapper.isNotNull(“user_name”);//user_name is not null |
in | 字段 in (v0,v1,…) | queryWrapper.in(“user_name”, {“张三”,“李四”,“王五”}); // user_name in (“张三”,“李四”,“王五”) |
notIn | 字段 not in (v0,vl,…) | queryWrapper.notIn(“user_name”, {“张三”,“李四”,“王五”}); // user_name not in (“张三”,“李四”,“王五”) |
inSql | 字段 in ( sql语句) | queryWrapper.in(“user_name”, (select name from student where age< 23)); // user_name in (select name from student where age< 23) |
notInSql | 字段 not in ( sql语句) | queryWrapper.notIn(“user_name”, (select name from student where age< 23)); // user_name not in (select name from student where age< 23) |
groupBy | 分组:GROUP BY 字段 | queryWrapper.groupBy(“user_name”);//group by user_name |
orderByAsc | 排序:ORDER BY 字段 ASC | queryWrapper.orderByAsc(“createTime”);//order by createTime asc |
orderByDesc | 排序:ORDER BY 字段 DESC | queryWrapper.orderByDesc(“createTime”);//order by createTime desc |
orderBy | 排序:ORDER BY 字段 | queryWrapper.orderBy(true, true, “createTime”);//order by createTime asc |
or | 拼接 OR | 说明:主动调用or表示紧接着下一个方法不是用and连接!(不调用or则默认为使用and连接);queryWrapper.eq(“id”, “1”).or().eq(“user_name”, “张三”); //id = 1 or name =‘张三’ |
and | 拼接 AND | queryWrapper.eq(“id”, “1”).and(i->i.eq(“user_name”, “张三”)); //id = 1 and name =‘张三’ |
apply | 拼接 sql | 说明: 该方法可用于数据库函数动态入参的params;queryWrapper.eq(“id”, user.getId()).apply(“date_format(‘createTime’,‘%Y-%m-%d’) = {0}”,“2023-07-25”);//date_format(‘createTime’,'%Y-%m-%d) =‘2023-07-25’") |
last | 无视优化规则直接拼接到 sql 的最后 | 说明:只能调用一次,多次调用以最后一次为准 有sq1注入的风险,请谨慎使用queryWrapper.last(" limit 1");// limit 1 |
exists | 拼接 EXISTS(sql语句) | queryWrapper.exists(“SELECT id FROM Student WHERE User.id = Student.userId”);// exists (“SELECT id FROM Student WHERE User.id = Student.userId”) |
notExists | 拼接 NOT EXISTS (sg1语句) | queryWrapper.notExists(“SELECT id FROM Student WHERE User.id = Student.userId”);// not exists (“SELECT id FROM Student WHERE User.id = Student.userId”) |
nested | 正常嵌套不带 AND或者 OR | queryWrapper.nested(i->i.eq(“id”,“1”).eq(“user_name”,“张三”));// id = 1 and user_name=“张三” |
having | having ( sql语句) | queryWrapper.having(“sum(age) > {0}”,10);//having sum(age) > 10; |
常用操作
1.插入
@Override public void save(User user) { userMapper.insert(user); }
2.根据id获取
@Override public User getById(String id) { return userMapper.selectById(id); }
3.删除
@Override public void removeById(String id) { userMapper.deleteById(id); }
4.根据id更新
userMapper.updateById(user);
5.获取列表
userMapper.selectList(queryWrapper);
6.获取单条数据
userMapper.selectOne(queryWrapper);
注意:
优先顺序:!>or>and
所以and
和or
在一起,先计算or
到此这篇关于MybatisPlus中QueryWrapper常用方法总结的文章就介绍到这了,更多相关MybatisPlus QueryWrapper方法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!