Mybatis-plus QueryWrapper获取条件SQL
•
数据库
在使用QueryWrapper.inSql()查询时,传参是写死的SQL字符串。如果需要条件查询时需要手动拼接字符串,很不方便。所以可以利用QueryWrapper转化为所需要的条件SQL
package io.jujiang.common.utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.Map;
/**
* @author xianhuiyi
*/
public class StringUtil {
public static String toSql(QueryWrapper queryWrapper) {
String customSqlSegment = queryWrapper.getCustomSqlSegment();
for (Map.Entry entry : queryWrapper.getParamNameValuePairs().entrySet()) {
customSqlSegment = customSqlSegment.replace("#{ew.paramNameValuePairs." + entry.getKey() + "}", StringUtil.formatParamValue(entry.getValue()));
}
return customSqlSegment;
}
public static String formatParamValue(Object arg) {
String paramValue;
if (arg instanceof String) {
paramValue = "'" + arg.toString().replace("'", "\\'") + "'";
} else if (arg instanceof Date) {
paramValue = "'" + DateUtils.format((Date) arg, "yyyy-MM-dd HH:mm:ss") + "'";
} else if (arg != null) {
paramValue = arg.toString();
} else {
paramValue = null;
}
return paramValue;
}
}
本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://www.net2asp.com/81098476ee.html
