mybatisPlus 实现条件查询总结
程序员文章站
2024-03-02 13:22:34
...
官网上没查到,看源码看到的,总结一下,方便大家,慢慢总结,不断更新。
1、mybatisPlus_eq 实现类似
<if test="a!=null">
AND a = #{a}
</if>
Wrapper wrapper= Condition.create().eq(VerifyUtil.isNNull(sysOffice.getParentId()),"PARENT_ID",sysOffice.getParentId());
VerifyUtil.isNNull(sysOffice.getParentId()),"PARENT_ID",sysOffice.getParentId());
2、like
like(boolean condition, String column, String value)
like(String column, String value)
like(boolean condition, String column, String value, SqlLike type)
like(String column, String value, SqlLike type)
condition 是一些条件,符合才可加上你的sql
column 为 数据库属性
value 为 你传入的值
type 为 % 拼在那边:
SqlLike 为枚举类
LEFT("left", "左边%"),
RIGHT("right", "右边%"),
CUSTOM("custom", "定制"),
DEFAULT("default", "两边%");
3、addFilterIfNeed
如果前置条件为true,则 加上 后置位的那些sql
public Wrapper<T> addFilterIfNeed(boolean need, String sqlWhere, Object... params) {
return need?this.where(sqlWhere, params):this;
}
eg
wrapper = Condition.create()
.addFilterIfNeed(VerifyUtil.isNNull(setmealId)," ccompany.ID in (SELECT COMPANY_ID from base_setmeal_company where SETMEAL_ID = '"+ setmealId +"') ");
4、orderAsc 或者 orderDesc
public Wrapper<T> orderAsc(Collection<String> columns) {
return this.orderBy(true, columns, true);
}
public Wrapper<T> orderDesc(Collection<String> columns) {
return this.orderBy(true, columns, false);
}
参数是Collection,可以用set 如
Set<String> set = new HashSet<>();
set.add("SORT");
Wrapper wrapper= Condition.create()
.orderAsc(set);