连表查询兼容原生方法

This commit is contained in:
yulichang 2021-11-18 16:50:28 +08:00
parent b55cb534c6
commit c2e575d83b
13 changed files with 361 additions and 25 deletions

View File

@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.toolkit.*;
import com.github.yulichang.annotation.EntityMapping;
import com.github.yulichang.annotation.FieldMapping;
import com.github.yulichang.exception.MPJException;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
@ -15,6 +16,10 @@ import org.apache.ibatis.reflection.Reflector;
import org.apache.ibatis.reflection.ReflectorFactory;
import org.apache.ibatis.session.Configuration;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@ -545,4 +550,25 @@ public class MPJTableInfoHelper {
/* 映射字段列表 */
mpjTableInfo.setFieldList(mpjFieldList);
}
/**
* 复制tableInfo对象
* 由于各个版本的MP的TableInfo对象存在差异为了兼容性采用反射而不是getter setter
*/
public static TableInfo copyAndSetTableName(TableInfo tableInfo, String tableName) {
try {
TableInfo table = new TableInfo(tableInfo.getEntityType());
//反射拷贝对象
Field[] fields = TableInfo.class.getDeclaredFields();
for (Field f : fields) {
f.setAccessible(true);
f.set(table, f.get(tableInfo));
}
table.setTableName(tableName);
return table;
} catch (Exception e) {
e.printStackTrace();
throw new MPJException("TableInfo 对象拷贝失败 -> " + tableInfo.getEntityType().getName());
}
}
}

View File

@ -1,22 +1,37 @@
package com.github.yulichang.injector;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.AbstractSqlInjector;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.core.injector.methods.*;
import com.baomidou.mybatisplus.core.injector.methods.Delete;
import com.baomidou.mybatisplus.core.injector.methods.DeleteById;
import com.baomidou.mybatisplus.core.injector.methods.DeleteByMap;
import com.baomidou.mybatisplus.core.injector.methods.Insert;
import com.baomidou.mybatisplus.core.injector.methods.DeleteBatchByIds;
import com.baomidou.mybatisplus.core.injector.methods.SelectById;
import com.baomidou.mybatisplus.core.injector.methods.Update;
import com.baomidou.mybatisplus.core.injector.methods.UpdateById;
import com.baomidou.mybatisplus.core.injector.methods.SelectBatchByIds;
import com.baomidou.mybatisplus.core.injector.methods.SelectByMap;
import com.baomidou.mybatisplus.core.mapper.Mapper;
import com.baomidou.mybatisplus.core.metadata.MPJTableMapperHelper;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
import com.github.yulichang.method.*;
import com.github.yulichang.method.mp.SelectCount;
import com.github.yulichang.method.mp.SelectList;
import com.github.yulichang.method.mp.SelectMaps;
import com.github.yulichang.method.mp.SelectMapsPage;
import com.github.yulichang.method.mp.SelectObjs;
import com.github.yulichang.method.mp.SelectOne;
import com.github.yulichang.method.mp.SelectPage;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.core.GenericTypeResolver;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
@ -31,11 +46,14 @@ import static java.util.stream.Collectors.toList;
@ConditionalOnMissingBean({DefaultSqlInjector.class, AbstractSqlInjector.class, ISqlInjector.class})
public class MPJSqlInjector extends DefaultSqlInjector {
private static final List<String> METHOD_LIST = Arrays.asList("SelectOne", "SelectCount",
"SelectMaps", "SelectMapsPage", "SelectObjs", "SelectList", "SelectPage");
/**
* 升级到 mybatis plus 3.4.3.2 后对之前的版本兼容
*/
@SuppressWarnings({"unused", "deprecation"})
@SuppressWarnings("unused")
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
List<AbstractMethod> list = Stream.of(
new Insert(),
@ -47,16 +65,17 @@ public class MPJSqlInjector extends DefaultSqlInjector {
new UpdateById(),
new SelectById(),
new SelectBatchByIds(),
new SelectByMap(),
new SelectOne(),
new SelectCount(),
new SelectMaps(),
new SelectMapsPage(),
new SelectObjs(),
new SelectList(),
new SelectPage()
new SelectByMap()
// new com.baomidou.mybatisplus.core.injector.methods.SelectOne(),
// new com.baomidou.mybatisplus.core.injector.methods.SelectCount(),
// new com.baomidou.mybatisplus.core.injector.methods.SelectMaps(),
// new com.baomidou.mybatisplus.core.injector.methods.SelectMapsPage(),
// new com.baomidou.mybatisplus.core.injector.methods.SelectObjs(),
// new com.baomidou.mybatisplus.core.injector.methods.SelectList(),
// new com.baomidou.mybatisplus.core.injector.methods.SelectPage()
).collect(toList());
list.addAll(getJoinMethod());
list.addAll(getSelectMethod());
return list;
}
@ -66,6 +85,8 @@ public class MPJSqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
List<AbstractMethod> list = super.getMethodList(mapperClass, tableInfo);
list.removeIf(i -> METHOD_LIST.contains(i.getClass().getSimpleName()));
list.addAll(getSelectMethod());
list.addAll(getJoinMethod());
return list;
}
@ -82,6 +103,18 @@ public class MPJSqlInjector extends DefaultSqlInjector {
return list;
}
private List<AbstractMethod> getSelectMethod() {
List<AbstractMethod> list = new ArrayList<>();
list.add(new SelectOne());
list.add(new SelectCount());
list.add(new SelectMaps());
list.add(new SelectMapsPage());
list.add(new SelectObjs());
list.add(new SelectList());
list.add(new SelectPage());
return list;
}
@Override
public void inspectInject(MapperBuilderAssistant builderAssistant, Class<?> mapperClass) {
Class<?> modelClass = getSuperClassGenericType(mapperClass, Mapper.class, 0);

View File

@ -5,10 +5,15 @@ import com.baomidou.mybatisplus.core.metadata.MPJTableInfoHelper;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.github.yulichang.config.InterceptorConfig;
import com.github.yulichang.interfaces.MPJBaseJoin;
import com.github.yulichang.method.MPJResultType;
import com.github.yulichang.toolkit.Constant;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ResultMap;
import org.apache.ibatis.mapping.ResultMapping;
@ -37,6 +42,9 @@ import java.util.concurrent.ConcurrentHashMap;
@SuppressWarnings("unchecked")
@Intercepts(@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}))
public class MPJInterceptor implements Interceptor {
private static final Log logger = LogFactory.getLog(MPJInterceptor.class);
private static final List<ResultMapping> EMPTY_RESULT_MAPPING = new ArrayList<>(0);
/**
@ -50,9 +58,14 @@ public class MPJInterceptor implements Interceptor {
if (args[0] instanceof MappedStatement) {
MappedStatement ms = (MappedStatement) args[0];
if (args[1] instanceof Map) {
Map<String, ?> map = (Map<String, ?>) args[1];
if (CollectionUtils.isNotEmpty(map)) {
if (map.containsKey(Constant.CLAZZ)) {
Map<String, Object> map = (Map<String, Object>) args[1];
Object ew = map.get(Constants.WRAPPER);
if (!map.containsKey(Constant.PARAM_TYPE)) {
map.put(Constant.PARAM_TYPE, Objects.nonNull(ew) && (ew instanceof MPJBaseJoin));
} else {
logger.warn(String.format("请不要使用MPJ预留参数名 %s", Constant.PARAM_TYPE));
}
if (CollectionUtils.isNotEmpty(map) && map.containsKey(Constant.CLAZZ)) {
Class<?> clazz = (Class<?>) map.get(Constant.CLAZZ);
if (Objects.nonNull(clazz)) {
List<ResultMap> list = ms.getResultMaps();
@ -66,10 +79,10 @@ public class MPJInterceptor implements Interceptor {
}
}
}
}
return invocation.proceed();
}
/**
* 构建新的MappedStatement
*/

View File

@ -20,8 +20,7 @@ public abstract class MPJAbstractMethod extends AbstractMethod {
*/
@Override
protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) {
String sqlScript = EMPTY;
sqlScript += SqlScriptUtils.convertIf(String.format(SqlScriptUtils.convertIf(" AND", String.format("%s and %s", WRAPPER_NONEMPTYOFENTITY, WRAPPER_NONEMPTYOFNORMAL), false) + " ${%s}", WRAPPER_SQLSEGMENT),
String sqlScript = SqlScriptUtils.convertIf(String.format("${%s}", WRAPPER_SQLSEGMENT),
String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT,
WRAPPER_NONEMPTYOFWHERE), true);
sqlScript = SqlScriptUtils.convertWhere(sqlScript) + NEWLINE;

View File

@ -0,0 +1,25 @@
package com.github.yulichang.method.mp;
import com.baomidou.mybatisplus.core.metadata.MPJTableInfoHelper;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import com.github.yulichang.toolkit.Constant;
import org.apache.ibatis.mapping.MappedStatement;
/**
* SelectCount 兼容MP原生方法
*/
public class SelectCount extends com.baomidou.mybatisplus.core.injector.methods.SelectCount implements TableAlias {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
return super.injectMappedStatement(mapperClass, modelClass,
MPJTableInfoHelper.copyAndSetTableName(tableInfo, getTableName(tableInfo)));
}
@Override
protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) {
return SqlScriptUtils.convertChoose(String.format("%s == null or !%s", Constant.PARAM_TYPE, Constant.PARAM_TYPE),
super.sqlWhereEntityWrapper(newLine, table), mpjSqlWhereEntityWrapper(newLine, table));
}
}

View File

@ -0,0 +1,25 @@
package com.github.yulichang.method.mp;
import com.baomidou.mybatisplus.core.metadata.MPJTableInfoHelper;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import com.github.yulichang.toolkit.Constant;
import org.apache.ibatis.mapping.MappedStatement;
/**
* SelectList 兼容MP原生方法
*/
public class SelectList extends com.baomidou.mybatisplus.core.injector.methods.SelectList implements TableAlias {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
return super.injectMappedStatement(mapperClass, modelClass,
MPJTableInfoHelper.copyAndSetTableName(tableInfo, getTableName(tableInfo)));
}
@Override
protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) {
return SqlScriptUtils.convertChoose(String.format("%s == null or !%s", Constant.PARAM_TYPE, Constant.PARAM_TYPE),
super.sqlWhereEntityWrapper(newLine, table), mpjSqlWhereEntityWrapper(newLine, table));
}
}

View File

@ -0,0 +1,25 @@
package com.github.yulichang.method.mp;
import com.baomidou.mybatisplus.core.metadata.MPJTableInfoHelper;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import com.github.yulichang.toolkit.Constant;
import org.apache.ibatis.mapping.MappedStatement;
/**
* SelectMaps 兼容MP原生方法
*/
public class SelectMaps extends com.baomidou.mybatisplus.core.injector.methods.SelectMaps implements TableAlias {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
return super.injectMappedStatement(mapperClass, modelClass,
MPJTableInfoHelper.copyAndSetTableName(tableInfo, getTableName(tableInfo)));
}
@Override
protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) {
return SqlScriptUtils.convertChoose(String.format("%s == null or !%s", Constant.PARAM_TYPE, Constant.PARAM_TYPE),
super.sqlWhereEntityWrapper(newLine, table), mpjSqlWhereEntityWrapper(newLine, table));
}
}

View File

@ -0,0 +1,25 @@
package com.github.yulichang.method.mp;
import com.baomidou.mybatisplus.core.metadata.MPJTableInfoHelper;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import com.github.yulichang.toolkit.Constant;
import org.apache.ibatis.mapping.MappedStatement;
/**
* SelectMapsPage 兼容MP原生方法
*/
public class SelectMapsPage extends com.baomidou.mybatisplus.core.injector.methods.SelectMapsPage implements TableAlias {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
return super.injectMappedStatement(mapperClass, modelClass,
MPJTableInfoHelper.copyAndSetTableName(tableInfo, getTableName(tableInfo)));
}
@Override
protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) {
return SqlScriptUtils.convertChoose(String.format("%s == null or !%s", Constant.PARAM_TYPE, Constant.PARAM_TYPE),
super.sqlWhereEntityWrapper(newLine, table), mpjSqlWhereEntityWrapper(newLine, table));
}
}

View File

@ -0,0 +1,25 @@
package com.github.yulichang.method.mp;
import com.baomidou.mybatisplus.core.metadata.MPJTableInfoHelper;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import com.github.yulichang.toolkit.Constant;
import org.apache.ibatis.mapping.MappedStatement;
/**
* SelectObjs 兼容MP原生方法
*/
public class SelectObjs extends com.baomidou.mybatisplus.core.injector.methods.SelectObjs implements TableAlias {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
return super.injectMappedStatement(mapperClass, modelClass,
MPJTableInfoHelper.copyAndSetTableName(tableInfo, getTableName(tableInfo)));
}
@Override
protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) {
return SqlScriptUtils.convertChoose(String.format("%s == null or !%s", Constant.PARAM_TYPE, Constant.PARAM_TYPE),
super.sqlWhereEntityWrapper(newLine, table), mpjSqlWhereEntityWrapper(newLine, table));
}
}

View File

@ -0,0 +1,30 @@
package com.github.yulichang.method.mp;
import com.baomidou.mybatisplus.core.metadata.MPJTableInfoHelper;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import com.github.yulichang.toolkit.Constant;
import org.apache.ibatis.mapping.MappedStatement;
/**
* selectOne 兼容MP原生方法
* <p>
* 查询满足条件一条数据为了精简注入方法该方法采用 list.get(0) 处理后续不再使用
*
* @see com.baomidou.mybatisplus.core.injector.methods.SelectOne
*/
@SuppressWarnings("deprecation")
public class SelectOne extends com.baomidou.mybatisplus.core.injector.methods.SelectOne implements TableAlias {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
return super.injectMappedStatement(mapperClass, modelClass,
MPJTableInfoHelper.copyAndSetTableName(tableInfo, getTableName(tableInfo)));
}
@Override
protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) {
return SqlScriptUtils.convertChoose(String.format("%s == null or !%s", Constant.PARAM_TYPE, Constant.PARAM_TYPE),
super.sqlWhereEntityWrapper(newLine, table), mpjSqlWhereEntityWrapper(newLine, table));
}
}

View File

@ -0,0 +1,25 @@
package com.github.yulichang.method.mp;
import com.baomidou.mybatisplus.core.metadata.MPJTableInfoHelper;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import com.github.yulichang.toolkit.Constant;
import org.apache.ibatis.mapping.MappedStatement;
/**
* SelectPage 兼容MP原生方法
*/
public class SelectPage extends com.baomidou.mybatisplus.core.injector.methods.SelectPage implements TableAlias {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
return super.injectMappedStatement(mapperClass, modelClass,
MPJTableInfoHelper.copyAndSetTableName(tableInfo, getTableName(tableInfo)));
}
@Override
protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) {
return SqlScriptUtils.convertChoose(String.format("%s == null or !%s", Constant.PARAM_TYPE, Constant.PARAM_TYPE),
super.sqlWhereEntityWrapper(newLine, table), mpjSqlWhereEntityWrapper(newLine, table));
}
}

View File

@ -0,0 +1,83 @@
package com.github.yulichang.method.mp;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import com.github.yulichang.toolkit.Constant;
/**
* 兼容原生方法
*
* @author yulichang
* @since 1.2.0
*/
public interface TableAlias extends Constants {
default String getTableName(TableInfo tableInfo) {
return tableInfo.getTableName() + SPACE + SqlScriptUtils.convertIf("${ew.alias}",
String.format("%s != null and %s", Constant.PARAM_TYPE, Constant.PARAM_TYPE), false)
+ SPACE + SqlScriptUtils.convertIf("${ew.from}",
String.format("%s != null and %s and %s != null and %s != ''", Constant.PARAM_TYPE, Constant.PARAM_TYPE,
"ew.from", "ew.from"), false);
}
default String mpjSqlWhereEntityWrapper(boolean newLine, TableInfo table) {
if (table.isWithLogicDelete()) {
String sqlScript = (NEWLINE + getLogicDeleteSql(table, true, true) + NEWLINE);
String normalSqlScript = SqlScriptUtils.convertIf(String.format("AND ${%s}", WRAPPER_SQLSEGMENT),
String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT,
WRAPPER_NONEMPTYOFNORMAL), true);
normalSqlScript += NEWLINE;
normalSqlScript += SqlScriptUtils.convertIf(String.format(" ${%s}", WRAPPER_SQLSEGMENT),
String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT,
WRAPPER_EMPTYOFNORMAL), true);
sqlScript += normalSqlScript;
sqlScript = SqlScriptUtils.convertChoose(String.format("%s != null", WRAPPER), sqlScript,
table.getLogicDeleteSql(false, true));
sqlScript = SqlScriptUtils.convertWhere(sqlScript);
return newLine ? NEWLINE + sqlScript : sqlScript;
} else {
String sqlScript = SqlScriptUtils.convertIf(String.format("${%s}", WRAPPER_SQLSEGMENT),
String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT,
WRAPPER_NONEMPTYOFWHERE), true);
sqlScript = SqlScriptUtils.convertWhere(sqlScript) + NEWLINE;
sqlScript += SqlScriptUtils.convertIf(String.format(" ${%s}", WRAPPER_SQLSEGMENT),
String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT,
WRAPPER_EMPTYOFWHERE), true);
sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", WRAPPER), true);
return newLine ? NEWLINE + sqlScript : sqlScript;
}
}
default String getLogicDeleteSql(TableInfo tableInfo, boolean startWithAnd, boolean isWhere) {
if (tableInfo.isWithLogicDelete()) {
String logicDeleteSql = formatLogicDeleteSql(tableInfo, isWhere);
if (startWithAnd) {
logicDeleteSql = " AND " + logicDeleteSql;
}
return logicDeleteSql;
}
return EMPTY;
}
default String formatLogicDeleteSql(TableInfo tableInfo, boolean isWhere) {
final String value = isWhere ? tableInfo.getLogicDeleteFieldInfo().getLogicNotDeleteValue() :
tableInfo.getLogicDeleteFieldInfo().getLogicDeleteValue();
if (isWhere) {
if (NULL.equalsIgnoreCase(value)) {
return "${ew.alias}." + tableInfo.getLogicDeleteFieldInfo().getColumn() + " IS NULL";
} else {
return "${ew.alias}." + tableInfo.getLogicDeleteFieldInfo().getColumn() + EQUALS + String.format(
tableInfo.getLogicDeleteFieldInfo().isCharSequence() ? "'%s'" : "%s", value);
}
}
final String targetStr = "${ew.alias}." + tableInfo.getLogicDeleteFieldInfo().getColumn() + EQUALS;
if (NULL.equalsIgnoreCase(value)) {
return targetStr + NULL;
} else {
return targetStr + String.format(tableInfo.getLogicDeleteFieldInfo().isCharSequence() ? "'%s'" : "%s", value);
}
}
}

View File

@ -25,6 +25,8 @@ public interface Constant {
String CLAZZ = "resultTypeClass_Eg1sG";
String PARAM_TYPE = "paramType_Gr8re1Ee";
/**
* " LEFT JOIN "
*/