|
|
package com.utils;
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
|
|
|
import com.baomidou.mybatisplus.mapper.Wrapper;
|
|
|
|
|
|
/**
|
|
|
* Mybatis-Plus工具类
|
|
|
* 该类提供了一系列与MyBatis-Plus相关的工具方法,用于简化常见的数据库操作,
|
|
|
* 如构建查询条件、对象属性处理以及字符串格式转换等。
|
|
|
*/
|
|
|
public class MPUtil {
|
|
|
public static final char UNDERLINE = '_';
|
|
|
|
|
|
/**
|
|
|
* 将对象的属性转换为MyBatis-Plus的allEQ表达式形式,并为键添加指定前缀。
|
|
|
* 先使用BeanUtil将对象转换为Map,然后将Map的键从驼峰格式转换为下划线格式,
|
|
|
* 最后返回带有前缀的新Map。
|
|
|
*
|
|
|
* @param bean 要转换的对象
|
|
|
* @param pre 要添加的前缀
|
|
|
* @return 转换后的Map,键为下划线格式并带有前缀,值为对象属性值
|
|
|
*/
|
|
|
public static Map allEQMapPre(Object bean, String pre) {
|
|
|
Map<String, Object> map = BeanUtil.beanToMap(bean);
|
|
|
return camelToUnderlineMap(map, pre);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将对象的属性转换为MyBatis-Plus的allEQ表达式形式,不添加前缀。
|
|
|
* 先使用BeanUtil将对象转换为Map,然后将Map的键从驼峰格式转换为下划线格式,
|
|
|
* 最后返回转换后的新Map。
|
|
|
*
|
|
|
* @param bean 要转换的对象
|
|
|
* @return 转换后的Map,键为下划线格式,值为对象属性值
|
|
|
*/
|
|
|
public static Map allEQMap(Object bean) {
|
|
|
Map<String, Object> map = BeanUtil.beanToMap(bean);
|
|
|
return camelToUnderlineMap(map, "");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据对象属性构建MyBatis-Plus的Wrapper的like条件,并为键添加指定前缀。
|
|
|
* 先将对象转换为Map,再将Map的键转换为下划线格式并添加前缀,
|
|
|
* 然后使用转换后的Map生成like条件并返回Wrapper。
|
|
|
*
|
|
|
* @param wrapper MyBatis-Plus的Wrapper对象
|
|
|
* @param bean 包含条件属性的对象
|
|
|
* @param pre 要添加的前缀
|
|
|
* @return 配置了like条件的Wrapper对象
|
|
|
*/
|
|
|
public static Wrapper allLikePre(Wrapper wrapper, Object bean, String pre) {
|
|
|
Map<String, Object> map = BeanUtil.beanToMap(bean);
|
|
|
Map result = camelToUnderlineMap(map, pre);
|
|
|
return genLike(wrapper, result);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据对象属性构建MyBatis-Plus的Wrapper的like条件,不添加前缀。
|
|
|
* 直接将对象转换为Map,然后使用转换后的Map生成like条件并返回Wrapper。
|
|
|
*
|
|
|
* @param wrapper MyBatis-Plus的Wrapper对象
|
|
|
* @param bean 包含条件属性的对象
|
|
|
* @return 配置了like条件的Wrapper对象
|
|
|
*/
|
|
|
public static Wrapper allLike(Wrapper wrapper, Object bean) {
|
|
|
Map result = BeanUtil.beanToMap(bean, true, true);
|
|
|
return genLike(wrapper, result);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据Map参数构建MyBatis-Plus的Wrapper的like条件。
|
|
|
* 遍历Map,对每个键值对设置like条件,多个条件之间使用and连接。
|
|
|
*
|
|
|
* @param wrapper MyBatis-Plus的Wrapper对象
|
|
|
* @param param 包含条件键值对的Map
|
|
|
* @return 配置了like条件的Wrapper对象
|
|
|
*/
|
|
|
public static Wrapper genLike(Wrapper wrapper, Map param) {
|
|
|
Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator();
|
|
|
int i = 0;
|
|
|
while (it.hasNext()) {
|
|
|
if (i > 0) wrapper.and();
|
|
|
Map.Entry<String, Object> entry = it.next();
|
|
|
String key = entry.getKey();
|
|
|
String value = (String) entry.getValue();
|
|
|
wrapper.like(key, value);
|
|
|
i++;
|
|
|
}
|
|
|
return wrapper;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据对象属性构建MyBatis-Plus的Wrapper的like或eq条件。
|
|
|
* 先将对象转换为Map,然后根据Map中值是否包含"%"来决定设置like或eq条件。
|
|
|
*
|
|
|
* @param wrapper MyBatis-Plus的Wrapper对象
|
|
|
* @param bean 包含条件属性的对象
|
|
|
* @return 配置了like或eq条件的Wrapper对象
|
|
|
*/
|
|
|
public static Wrapper likeOrEq(Wrapper wrapper, Object bean) {
|
|
|
Map result = BeanUtil.beanToMap(bean, true, true);
|
|
|
return genLikeOrEq(wrapper, result);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据Map参数构建MyBatis-Plus的Wrapper的like或eq条件。
|
|
|
* 遍历Map,根据值中是否包含"%"来决定设置like或eq条件,多个条件之间使用and连接。
|
|
|
*
|
|
|
* @param wrapper MyBatis-Plus的Wrapper对象
|
|
|
* @param param 包含条件键值对的Map
|
|
|
* @return 配置了like或eq条件的Wrapper对象
|
|
|
*/
|
|
|
public static Wrapper genLikeOrEq(Wrapper wrapper, Map param) {
|
|
|
Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator();
|
|
|
int i = 0;
|
|
|
while (it.hasNext()) {
|
|
|
if (i > 0) wrapper.and();
|
|
|
Map.Entry<String, Object> entry = it.next();
|
|
|
String key = entry.getKey();
|
|
|
if (entry.getValue().toString().contains("%")) {
|
|
|
wrapper.like(key, entry.getValue().toString().replace("%", ""));
|
|
|
} else {
|
|
|
wrapper.eq(key, entry.getValue());
|
|
|
}
|
|
|
i++;
|
|
|
}
|
|
|
return wrapper;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据对象属性构建MyBatis-Plus的Wrapper的eq条件。
|
|
|
* 先将对象转换为Map,然后使用转换后的Map生成eq条件并返回Wrapper。
|
|
|
*
|
|
|
* @param wrapper MyBatis-Plus的Wrapper对象
|
|
|
* @param bean 包含条件属性的对象
|
|
|
* @return 配置了eq条件的Wrapper对象
|
|
|
*/
|
|
|
public static Wrapper allEq(Wrapper wrapper, Object bean) {
|
|
|
Map result = BeanUtil.beanToMap(bean, true, true);
|
|
|
return genEq(wrapper, result);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据Map参数构建MyBatis-Plus的Wrapper的eq条件。
|
|
|
* 遍历Map,对每个键值对设置eq条件,多个条件之间使用and连接。
|
|
|
*
|
|
|
* @param wrapper MyBatis-Plus的Wrapper对象
|
|
|
* @param param 包含条件键值对的Map
|
|
|
* @return 配置了eq条件的Wrapper对象
|
|
|
*/
|
|
|
public static Wrapper genEq(Wrapper wrapper, Map param) {
|
|
|
Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator();
|
|
|
int i = 0;
|
|
|
while (it.hasNext()) {
|
|
|
if (i > 0) wrapper.and();
|
|
|
Map.Entry<String, Object> entry = it.next();
|
|
|
String key = entry.getKey();
|
|
|
wrapper.eq(key, entry.getValue());
|
|
|
i++;
|
|
|
}
|
|
|
return wrapper;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据Map参数构建MyBatis-Plus的Wrapper的between条件。
|
|
|
* 遍历Map,根据键名是否以"_start"或"_end"结尾来决定设置ge(大于等于)或le(小于等于)条件。
|
|
|
*
|
|
|
* @param wrapper MyBatis-Plus的Wrapper对象
|
|
|
* @param params 包含条件键值对的Map
|
|
|
* @return 配置了between条件的Wrapper对象
|
|
|
*/
|
|
|
public static Wrapper between(Wrapper wrapper, Map<String, Object> params) {
|
|
|
for (String key : params.keySet()) {
|
|
|
String columnName = "";
|
|
|
if (key.endsWith("_start")) {
|
|
|
columnName = key.substring(0, key.indexOf("_start"));
|
|
|
if (StringUtils.isNotBlank(params.get(key).toString())) {
|
|
|
wrapper.ge(columnName, params.get(key));
|
|
|
}
|
|
|
}
|
|
|
if (key.endsWith("_end")) {
|
|
|
columnName = key.substring(0, key.indexOf("_end"));
|
|
|
if (StringUtils.isNotBlank(params.get(key).toString())) {
|
|
|
wrapper.le(columnName, params.get(key));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return wrapper;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据Map参数为MyBatis-Plus的Wrapper设置排序条件。
|
|
|
* 根据"order"和"sort"参数的值设置升序或降序排序。
|
|
|
*
|
|
|
* @param wrapper MyBatis-Plus的Wrapper对象
|
|
|
* @param params 包含排序相关键值对的Map,如"order"(排序方向)和"sort"(排序字段)
|
|
|
* @return 配置了排序条件的Wrapper对象
|
|
|
*/
|
|
|
public static Wrapper sort(Wrapper wrapper, Map<String, Object> params) {
|
|
|
String order = "";
|
|
|
if (params.get("order") != null && StringUtils.isNotBlank(params.get("order").toString())) {
|
|
|
order = params.get("order").toString();
|
|
|
}
|
|
|
if (params.get("sort") != null && StringUtils.isNotBlank(params.get("sort").toString())) {
|
|
|
if (order.equalsIgnoreCase("desc")) {
|
|
|
wrapper.orderDesc(Arrays.asList(params.get("sort")));
|
|
|
} else {
|
|
|
wrapper.orderAsc(Arrays.asList(params.get("sort")));
|
|
|
}
|
|
|
}
|
|
|
return wrapper;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将驼峰格式字符串转换为下划线格式字符串。
|
|
|
* 遍历字符串,若字符为大写,则在其前添加下划线并转换为小写,否则直接添加字符。
|
|
|
*
|
|
|
* @param param 要转换的驼峰格式字符串
|
|
|
* @return 转换后的下划线格式字符串
|
|
|
*/
|
|
|
public static String camelToUnderline(String param) {
|
|
|
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);
|
|
|
sb.append(Character.toLowerCase(c));
|
|
|
} else {
|
|
|
sb.append(c);
|
|
|
}
|
|
|
}
|
|
|
return sb.toString();
|
|
|
}
|
|
|
|
|
|
public static void main(String[] ages) {
|
|
|
System.out.println(camelToUnderline("ABCddfANM"));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将Map的键从驼峰格式转换为下划线格式,并根据前缀进行处理。
|
|
|
* 遍历Map,将每个键转换为下划线格式,然后根据前缀的情况将键值对添加到新的Map中。
|
|
|
*
|
|
|
* @param param 要转换的Map
|
|
|
* @param pre 前缀
|
|
|
* @return 转换后的Map,键为下划线格式,根据前缀处理后的值
|
|
|
*/
|
|
|
public static Map camelToUnderlineMap(Map param, String pre) {
|
|
|
Map<String, Object> newMap = new HashMap<String, Object>();
|
|
|
Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator();
|
|
|
while (it.hasNext()) {
|
|
|
Map.Entry<String, Object> entry = it.next();
|
|
|
String key = entry.getKey();
|
|
|
String newKey = camelToUnderline(key);
|
|
|
if (pre.endsWith(".")) {
|
|
|
newMap.put(pre + newKey, entry.getValue());
|
|
|
} else if (StringUtils.isEmpty(pre)) {
|
|
|
newMap.put(newKey, entry.getValue());
|
|
|
} else {
|
|
|
newMap.put(pre + "." + newKey, entry.getValue());
|
|
|
}
|
|
|
}
|
|
|
return newMap;
|
|
|
}
|
|
|
} |