这个是我公司用的东西,也不知道是哪扣来的,大致功能就是从前端返回一些例如下图的东西,实现equal等等方法,其实不太好,但是小公司开发省事
使用MapFilter.getQueryWrapper(“一个map”,“当前对应的实体类.class”)返回一个querywrapper
package com.jp.yycx.business.util;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.core.mp.support.SqlKeyword;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MapFilter {
public static Map<String, Object> newMap (Map<String, Object> oldMap){
if (!Func.isEmpty(oldMap)) {
Map<String, Object> newMap = JSON.parseObject(JSON.toJSONString(oldMap));
oldMap.forEach((k, v) -> {
if (!Func.hasEmpty(new Object[]{k, v}) && !k.endsWith("_ignore")) {
if (k.endsWith("_equal")) {
} else if (k.endsWith("_notequal")) {
} else if (k.endsWith("_notlike")) {
} else if (k.endsWith("_gt")) {
} else if (k.endsWith("_lt")) {
} else if (k.endsWith("_dategt")) {
} else if (k.endsWith("_dateequal")) {
} else if (k.endsWith("_datelt")) {
} else if (k.endsWith("_null")) {
} else if (k.endsWith("_notnull")) {
} else if(k.endsWith("_key")){
} else {
Object obj = newMap.remove(k);
}
}
});
return newMap;
}
return null;
}
/**
* 创建QueryWrapper
* @param query
* @param clazz
* @param <T>
* @return
*/
public static <T> QueryWrapper<T> getQueryWrapper(Map<String, Object> query, Class<T> clazz) {
QueryWrapper<T> qw = new QueryWrapper();
qw.setEntity(BeanUtil.newInstance(clazz));
buildCondition(query, qw);
return qw;
}
public static void buildCondition(Map<String, Object> query, QueryWrapper<?> qw) {
if (!Func.isEmpty(query)) {
query.forEach((k, v) -> {
if (!Func.hasEmpty(new Object[]{k, v}) && !k.endsWith("_ignore")) {
if (k.endsWith("_equal")) {
qw.eq(getColumn(k, "_equal"), v);
} else if (k.endsWith("_notequal")) {
qw.ne(getColumn(k, "_notequal"), v);
} else if (k.endsWith("_notlike")) {
qw.notLike(getColumn(k, "_notlike"), v);
} else if (k.endsWith("_gt")) {
qw.gt(getColumn(k, "_gt"), v);
} else if (k.endsWith("_lt")) {
qw.lt(getColumn(k, "_lt"), v);
} else if (k.endsWith("_dategt")) {
qw.gt(getColumn(k, "_dategt"), v);
} else if (k.endsWith("_dateequal")) {
qw.eq(getColumn(k, "_dateequal"), v);
} else if (k.endsWith("_datege")) {
qw.ge(getColumn(k, "_datege"), v);
} else if (k.endsWith("_datele")) {
qw.le(getColumn(k, "_datele"), v);
} else if (k.endsWith("_datelt")) {
qw.lt(getColumn(k, "_datelt"), v);
} else if (k.endsWith("_null")) {
qw.isNull(getColumn(k, "_null"));
} else if (k.endsWith("_notnull")) {
qw.isNotNull(getColumn(k, "_notnull"));
} else if (k.endsWith("_in")) {
qw.in(getColumn(k, "_in"),Func.toStrList(",", v.toString()));
} else if (k.endsWith("_notIn")) {
qw.notIn(getColumn(k, "_notIn"),Func.toStrList(",", v.toString()));
}else if (k.endsWith("_like")) {
qw.like(getColumn(k, "_like"),v);
} else if (k.endsWith("_keyLike")) {
List<String> columnLikes = Func.toStrList(",", (String) query.get("keyLike_column"));
if(!Func.isEmpty(columnLikes)) {
qw.and(wrapper -> {
columnLikes.forEach(column -> {
wrapper.or(r -> {
r.like(StringUtil.humpToUnderline(column), v);
});
});
});
}
}
}
});
}
}
private static String getColumn(String column, String keyword) {
return StringUtil.humpToUnderline(StringUtil.removeSuffix(column, keyword));
}
}