欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis-plus 使用Condition拼接Sql语句各方法的使用

程序员文章站 2022-03-02 15:23:55
...

1.setSqlSelect—用于添加查询的列信息

public Wrapper<T> setSqlSelect(String... columns) {
        StringBuilder builder = new StringBuilder();
        String[] arr = columns;
        int len = columns.length;

        for(int i = 0; i$ < len; ++i$) {
            String column = arr$[i$];
            if (StringUtils.isNotEmpty(column)) {
                if (builder.length() > 0) {
                    builder.append(",");
                }

                builder.append(column);
            }
        }

        this.sqlSelect = builder.toString();
        return this;
    }

2.eq—拼接等于的where语句 column=params

public Wrapper<T> eq(boolean condition, String column, Object params) {
        if (condition) {
            this.sql.WHERE(this.formatSql(String.format("%s = {0}", column), params));
        }

        return this;
    }

3.ne—拼接不等于的where语句 column<>params

public Wrapper<T> ne(boolean condition, String column, Object params) {
        if (condition) {
            this.sql.WHERE(this.formatSql(String.format("%s <> {0}", column), params));
        }

        return this;
    }

4.allEq–拼接数据以map封装的等于的where语句

 public Wrapper<T> allEq(boolean condition, Map<String, Object> params) {
        if (condition && MapUtils.isNotEmpty(params)) {
            Iterator iterator = params.entrySet().iterator();

            while(iterator.hasNext()) {
                Entry<String, Object> entry = (Entry)iterator.next();
                Object value = entry.getValue();
                if (StringUtils.checkValNotNull(value)) {
                    this.sql.WHERE(this.formatSql(String.format("%s = {0}", entry.getKey()), entry.getValue()));
                }
            }
        }

        return this;
    }

5.gt—-拼接大于号的where语句

public Wrapper<T> gt(boolean condition, String column, Object params) {
        if (condition) {
            this.sql.WHERE(this.formatSql(String.format("%s > {0}", column), params));
        }

        return this;
    }

6.ge–拼接大于等于号的where语句 (代码类似5) 
7.lt—拼接小于号的where语句 
8.le—小于等于

    /**
         *
         AND (user_name=#{ew.paramNameValuePairs.MPGENVAL1} AND id=1)
         OR (user_status=#{ew.paramNameValuePairs.MPGENVAL2} OR status=1
         AND user_nickname NOT LIKE #{ew.paramNameValuePairs.MPGENVAL3})
         AND (new=xx AND hhh LIKE #{ew.paramNameValuePairs.MPGENVAL4})
         AND (pwd=11 AND n1 IS NOT NULL AND n2 IS NOT NULL AND n3 IS NULL)
         GROUP BY x1, x2,x3
         HAVING (x1=11 AND x3=433)
         ORDER BY dd, d1,d2
         */
        ew.where("user_name={0}", "'zhangsan'").and("id=1")
                .orNew("user_status={0}", "0").or("status=1")
                .notLike("user_nickname", "notvalue")
                .andNew("new=xx").like("hhh", "ddd")
                .andNew("pwd=11").isNotNull("n1,n2").isNull("n3")
                .groupBy("x1").groupBy("x2,x3")
                .having("x1=11").having("x3=433")
                .orderBy("dd").orderBy("d1,d2");

9.and — 在没有出现下一个andNew或者orNew之前的所有where条件在一个括号里 
10.andNew –新建一组and语句 
11.or,orNew–同上

 

--------------------- 
转自:https://blog.csdn.net/qq_36752632/article/details/79035980 
 

相关标签: mybatis