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

mybatis-plus Condition拼接Sql語句各方法

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–同上

mybatis-plus 自定義sql語句

 @Override
    public Map<String, Object> kccxhj(Term term) throws Exception{
        QueryWrapper<EZShangPin> wrapper=new QueryWrapper<>();
        if(MyStrUtils.isNotEmpty(term.getQ())){
            CWMSTmdrdmx tmdrdmx=new CWMSTmdrdmx();
            tmdrdmx.setTm(term.getQ());
            tmdrdmx.setSl(1);
            tmService.recongnizeSptm(tmdrdmx);
            //log("tmdrdmx",tmdrdmx);
            if(tmdrdmx.getResult()==0){
                wrapper.eq("spdm",tmdrdmx.getSpdm());
            }else{
                wrapper.like("spdm+'_'+spmc+'_'+isnull(zjf,'')", term.getQ());
            }
        }
        wrapper.eq(MyStrUtils.isNotEmpty(term.getPpdm()),"fjsx1",term.getPpdm());
        wrapper.eq(MyStrUtils.isNotEmpty(term.getDldm()),"fjsx2",term.getDldm());
        wrapper.eq(MyStrUtils.isNotEmpty(term.getJjdm()),"fjsx3",term.getJjdm());
        wrapper.eq(MyStrUtils.isNotEmpty(term.getNddm()),"fjsx4",term.getNddm());
        wrapper.eq("isnull(tzsy,0)", 0); 
        wrapper.eq("ckdm", term.getCurrentsddm()); 
        return repMapper.kccxhj(wrapper);
    }
@Mapper
@Repository
public interface RepMapper extends BaseMapper { 
    Map<String, Object> kccxhj(@Param(Constants.WRAPPER) QueryWrapper wrapper); 
}
   <select id="kccxhj" resultType="java.util.Map"> 
        SELECT SUM(SL) AS zsl,sum(sl*bzsj) as zje
        from(
                select ckdm,ShangPin.spdm,ShangPin.spmc,ShangPin.zjf,shanpin.bzsj,ShangPin.FJSX1,ShangPin.FJSX2,ShangPin.FJSX3,ShangPin.FJSX4,SPKCB.SL
                from shangpin,SPKCB
                where shangpin.spdm=spkcb.SPDM
        ) a
        ${ew.customSqlSegment} 
    </select>

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: