目录
  • 首先是.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
between BETWEEN值1 AND值2 例:between(“age”,18,30) —> age between 18 and 30
notBetween NOT BETWEEN值1 AND值2 例: notBetween(“age”,18,30) —> age not between 18 and 30
like LIKE ‘%值%’ 例: like(“name”,“王”) —–> name like '%王%’
notLike NOT LIKE ‘%值%’ 例: notLike (“name”,“王”) —> name not like '%王%’
likeLeft LIKE '%值’ 例:likeLeft (“name”,“王”) —–> name like '%王’
likeRight LIKE’值%’ 例: 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
having HAVING ( 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 = '老王
and AND嵌套 例: 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 活着’)
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。