java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > MybatisPlus QueryWrapper方法

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 字段 ASCqueryWrapper.orderByAsc(“createTime”);//order by createTime asc
orderByDesc排序:ORDER BY 字段 DESCqueryWrapper.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拼接 ANDqueryWrapper.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或者 ORqueryWrapper.nested(i->i.eq(“id”,“1”).eq(“user_name”,“张三”));// id = 1 and user_name=“张三”
havinghaving ( 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所以andor在一起,先计算or

到此这篇关于MybatisPlus中QueryWrapper常用方法总结的文章就介绍到这了,更多相关MybatisPlus QueryWrapper方法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文