java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > mybatis Plus条件构造器

mybatisPlus条件构造器常用方法小结

作者:Wmenghu

这篇文章主要介绍了mybatisPlus条件构造器常用方法,首先是.select和其他条件,本文结合示例代码给大家介绍的非常详细,需要的朋友可以参考下

首先是.select

在MP查询中,默认查询所有的字段,如果有需要也可以通过select方法进行指定字段。其中要注意的细节:

wrapper.select("pname")
        .eq("pname","张三")
        .or().eq("price",300);
List<User> userList = userDao.selectList(wrapper);

使用select进行sql语句拼接时,不会识别在实体类中属性对应的操作:

SELECT pname FROM USER WHERE (pname = ? or price =? )

当数据库表中的字段名,与实体类对象的属性名不一致时

wrapper.select("pname as name")
        .eq("pname","张三")
        .or().eq("price",300);
List<User> userList = userDao.selectList(wrapper);

这样拼接出来的sql语句:

SELECT pname as name FROM user WHERE (pname = ? OR price = ? )

其他条件 

函数名说明例子
eq等于 =例:eq(“name”,“张三”) :name = ‘张三’
ne不等于<>例: eq(“name”,“老王”) —> name <> ‘老王’
gt大于>例:gt(“age”,18) —> age > 18
ge大于等于>=例:ge(“age”,18) —> age >= 18
lt小于<例:lt(“age”,18) —> age < 18
le小于<=例:le(“age”,18) —> age <= 18
betweenBETWEEN值1 AND值2例:between(“age”,18,30) —> age between 18 and 30
notBetweenNOT BETWEEN值1 AND值2例: notBetween(“age”,18,30) —> age not between 18 and 30
likeLIKE ‘%值%’例: like(“name”,“王”) —–> name like '%王%’
notLikeNOT LIKE ‘%值%’例: notLike (“name”,“王”) —> name not like '%王%’
likeLeftLIKE '%值’例:likeLeft (“name”,“王”) —–> name like '%王’
likeRightLIKE’值%’例: likeRight(“name”,“王”) —> name like ‘王%’
isNull字段IS NULL例: isNul1 (“name”) —> name is null
isNotNull字段IS NOT NULL例: isNotNull(“name”) —> name is not null
in字段IN (v0, v1,…)例: in(“age”,{1,2,3} ) —–> age in (1,2,3)
notIn字段NOT IN (v0, v1,…)例: notIn(“age”,1,2,3) —> age not in (1,2,3)
inSql字段IN ( sql语句)inSql(“id”, “select id from table where id < 3”) —–> id in (select id from table where id < 3)
notInSql字段NOT IN ( sql语句)notInSql(“id”, “select id from table where id < 3”) —–> age not in (select id from table where id < 3)
groupBy分组:GROUP BY 字段,…例: groupBy(“id”, “name”) —> group by id, name
orderByAsc排序:ORDER BY字段,… ASC例: orderByAsc(“id”, “name”) —> order by id ASC, name ASC
orderByDesc排序:ORDER BY 字段,…DESC例: orderByDesc(“id”, “name”) —> order by id DESC, name DESC
orderBy排序:ORDER BY字段,…例: orderBy(true,true,“id”,“name”) —–> order by id ASC,name ASC
havingHAVING ( sql语句)having(“sum(age) >{0}”,11) —> having sum(age) > 11
or拼接OR主动调用or表示紧接着下一个方法不是用and连接!(不调用or则默认为使用and连接)例:eq(“id”,1).or().eq(“name”,“老王”) —> id = 1 or name = '老王
andAND嵌套例: and(i -> i.eq(“name”,“李白”).ne(“status”,“活着”)) —> and (name ='李白’ and status ’活着’)
apply拼接sql该方法可用于数据库函数动态入参的params对应前面sqlHaving内部的{index}部分.这样是不会有sql注入风险的,反之会有! 例: apply(“date_format(dateColumn, ‘%Y一%m-%d’) ={0}”, “2008-08-08”) —> date_format(dateColumn,’%Y一%m-%d’) = ‘2008-08-08’")
last无视优化规则直接拼接到sql 的最后无视优化规则直接拼接到sql 的最后只能调用一次,多次调用以最后一次为准有sql注入的风险,请谨慎使用例: last(“limit 1”)
exists拼接EXISTS ( sql语句)—> exists (select id from table where age = 1)例: notExists(“select id from table where age = 1”) —>exists (select id from table where age = 1)
notExists拼接NOT EXISTS ( sql语句)例: notExists(“select id from table where age = 1”) —–> not exists (select id from table where age = 1)
nested正常嵌套不带AND或者 OR正常嵌套不带AND或者OR例: nested(i -> i.eq(“name”,“李白”).ne(“status”,“活着”)) —> (name = '李白’and status 活着’)

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

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