import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.lang.reflect.Field;
/**
* Created on 2022/4/26.
*
* @author lichuanming
*
* 将查询条件中空条件过滤掉
*/
public class CombineQueryWrapperNotNullConditionUnitl {
public static <T> QueryWrapper<T> selectByNotNullCondition(T t) {
Class<? extends T> aClass = (Class<? extends T>) t.getClass();
QueryWrapper<T> queryWrapper = new QueryWrapper();
Field[] declaredFields = aClass.getDeclaredFields();
for (int i = 0; i < declaredFields.length; i++) {
Field declaredField = declaredFields[i];
declaredField.setAccessible(true);
Object o = null;
try {
o = declaredField.get(t);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if(o==null || "serialVersionUID".equals(declaredField.getName())){
continue;
}
String underLineName = camelToUnderline(declaredField.getName(), 1);
queryWrapper.eq(underLineName,o);
}
return queryWrapper;
}
public static final char UNDERLINE = '_';
//驼峰转下划线
public static String camelToUnderline(String param, Integer charType) {
if (param == null || "".equals(param.trim())) {
return "";
}
int len = param.length();
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++) {
char c = param.charAt(i);
if (Character.isUpperCase(c)) {
sb.append(UNDERLINE);
}
if (charType == 2) {
sb.append(Character.toUpperCase(c)); //统一都转大写
} else {
sb.append(Character.toLowerCase(c)); //统一都转小写
}
}
return sb.toString();
}
//下划线转驼峰
public static String underlineToCamel(String param) {
if (param == null || "".equals(param.trim())) {
return "";
}
int len = param.length();
StringBuilder sb = new StringBuilder(len);
Boolean flag = false; // "_" 后转大写标志,默认字符前面没有"_"
for (int i = 0; i < len; i++) {
char c = param.charAt(i);
if (c == UNDERLINE) {
flag = true;
continue; //标志设置为true,跳过
} else {
if (flag == true) {
//表示当前字符前面是"_" ,当前字符转大写
sb.append(Character.toUpperCase(param.charAt(i)));
flag = false; //重置标识
} else {
sb.append(Character.toLowerCase(param.charAt(i)));
}
}
}
return sb.toString();
}
}
不会,我可以学;落后,我可以追赶;跌倒,我可以站起来!