mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
连表查询兼容原生方法
This commit is contained in:
parent
b55cb534c6
commit
c2e575d83b
@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.config.GlobalConfig;
|
|||||||
import com.baomidou.mybatisplus.core.toolkit.*;
|
import com.baomidou.mybatisplus.core.toolkit.*;
|
||||||
import com.github.yulichang.annotation.EntityMapping;
|
import com.github.yulichang.annotation.EntityMapping;
|
||||||
import com.github.yulichang.annotation.FieldMapping;
|
import com.github.yulichang.annotation.FieldMapping;
|
||||||
|
import com.github.yulichang.exception.MPJException;
|
||||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||||
import org.apache.ibatis.logging.Log;
|
import org.apache.ibatis.logging.Log;
|
||||||
import org.apache.ibatis.logging.LogFactory;
|
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.reflection.ReflectorFactory;
|
||||||
import org.apache.ibatis.session.Configuration;
|
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.lang.reflect.Field;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
@ -545,4 +550,25 @@ public class MPJTableInfoHelper {
|
|||||||
/* 映射字段列表 */
|
/* 映射字段列表 */
|
||||||
mpjTableInfo.setFieldList(mpjFieldList);
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,37 @@
|
|||||||
package com.github.yulichang.injector;
|
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.AbstractMethod;
|
||||||
import com.baomidou.mybatisplus.core.injector.AbstractSqlInjector;
|
import com.baomidou.mybatisplus.core.injector.AbstractSqlInjector;
|
||||||
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
|
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
|
||||||
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
|
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.mapper.Mapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.MPJTableMapperHelper;
|
import com.baomidou.mybatisplus.core.metadata.MPJTableMapperHelper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
|
import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
|
||||||
import com.github.yulichang.method.*;
|
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.apache.ibatis.builder.MapperBuilderAssistant;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.core.GenericTypeResolver;
|
import org.springframework.core.GenericTypeResolver;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@ -31,11 +46,14 @@ import static java.util.stream.Collectors.toList;
|
|||||||
@ConditionalOnMissingBean({DefaultSqlInjector.class, AbstractSqlInjector.class, ISqlInjector.class})
|
@ConditionalOnMissingBean({DefaultSqlInjector.class, AbstractSqlInjector.class, ISqlInjector.class})
|
||||||
public class MPJSqlInjector extends DefaultSqlInjector {
|
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 后对之前的版本兼容
|
* 升级到 mybatis plus 3.4.3.2 后对之前的版本兼容
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings({"unused", "deprecation"})
|
@SuppressWarnings("unused")
|
||||||
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
|
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
|
||||||
List<AbstractMethod> list = Stream.of(
|
List<AbstractMethod> list = Stream.of(
|
||||||
new Insert(),
|
new Insert(),
|
||||||
@ -47,16 +65,17 @@ public class MPJSqlInjector extends DefaultSqlInjector {
|
|||||||
new UpdateById(),
|
new UpdateById(),
|
||||||
new SelectById(),
|
new SelectById(),
|
||||||
new SelectBatchByIds(),
|
new SelectBatchByIds(),
|
||||||
new SelectByMap(),
|
new SelectByMap()
|
||||||
new SelectOne(),
|
// new com.baomidou.mybatisplus.core.injector.methods.SelectOne(),
|
||||||
new SelectCount(),
|
// new com.baomidou.mybatisplus.core.injector.methods.SelectCount(),
|
||||||
new SelectMaps(),
|
// new com.baomidou.mybatisplus.core.injector.methods.SelectMaps(),
|
||||||
new SelectMapsPage(),
|
// new com.baomidou.mybatisplus.core.injector.methods.SelectMapsPage(),
|
||||||
new SelectObjs(),
|
// new com.baomidou.mybatisplus.core.injector.methods.SelectObjs(),
|
||||||
new SelectList(),
|
// new com.baomidou.mybatisplus.core.injector.methods.SelectList(),
|
||||||
new SelectPage()
|
// new com.baomidou.mybatisplus.core.injector.methods.SelectPage()
|
||||||
).collect(toList());
|
).collect(toList());
|
||||||
list.addAll(getJoinMethod());
|
list.addAll(getJoinMethod());
|
||||||
|
list.addAll(getSelectMethod());
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,6 +85,8 @@ public class MPJSqlInjector extends DefaultSqlInjector {
|
|||||||
@Override
|
@Override
|
||||||
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
|
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
|
||||||
List<AbstractMethod> list = super.getMethodList(mapperClass, tableInfo);
|
List<AbstractMethod> list = super.getMethodList(mapperClass, tableInfo);
|
||||||
|
list.removeIf(i -> METHOD_LIST.contains(i.getClass().getSimpleName()));
|
||||||
|
list.addAll(getSelectMethod());
|
||||||
list.addAll(getJoinMethod());
|
list.addAll(getJoinMethod());
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
@ -82,6 +103,18 @@ public class MPJSqlInjector extends DefaultSqlInjector {
|
|||||||
return list;
|
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
|
@Override
|
||||||
public void inspectInject(MapperBuilderAssistant builderAssistant, Class<?> mapperClass) {
|
public void inspectInject(MapperBuilderAssistant builderAssistant, Class<?> mapperClass) {
|
||||||
Class<?> modelClass = getSuperClassGenericType(mapperClass, Mapper.class, 0);
|
Class<?> modelClass = getSuperClassGenericType(mapperClass, Mapper.class, 0);
|
||||||
|
@ -5,10 +5,15 @@ import com.baomidou.mybatisplus.core.metadata.MPJTableInfoHelper;
|
|||||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
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.method.MPJResultType;
|
||||||
import com.github.yulichang.toolkit.Constant;
|
import com.github.yulichang.toolkit.Constant;
|
||||||
import org.apache.ibatis.executor.Executor;
|
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.MappedStatement;
|
||||||
import org.apache.ibatis.mapping.ResultMap;
|
import org.apache.ibatis.mapping.ResultMap;
|
||||||
import org.apache.ibatis.mapping.ResultMapping;
|
import org.apache.ibatis.mapping.ResultMapping;
|
||||||
@ -37,6 +42,9 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Intercepts(@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}))
|
@Intercepts(@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}))
|
||||||
public class MPJInterceptor implements Interceptor {
|
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);
|
private static final List<ResultMapping> EMPTY_RESULT_MAPPING = new ArrayList<>(0);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -50,17 +58,21 @@ public class MPJInterceptor implements Interceptor {
|
|||||||
if (args[0] instanceof MappedStatement) {
|
if (args[0] instanceof MappedStatement) {
|
||||||
MappedStatement ms = (MappedStatement) args[0];
|
MappedStatement ms = (MappedStatement) args[0];
|
||||||
if (args[1] instanceof Map) {
|
if (args[1] instanceof Map) {
|
||||||
Map<String, ?> map = (Map<String, ?>) args[1];
|
Map<String, Object> map = (Map<String, Object>) args[1];
|
||||||
if (CollectionUtils.isNotEmpty(map)) {
|
Object ew = map.get(Constants.WRAPPER);
|
||||||
if (map.containsKey(Constant.CLAZZ)) {
|
if (!map.containsKey(Constant.PARAM_TYPE)) {
|
||||||
Class<?> clazz = (Class<?>) map.get(Constant.CLAZZ);
|
map.put(Constant.PARAM_TYPE, Objects.nonNull(ew) && (ew instanceof MPJBaseJoin));
|
||||||
if (Objects.nonNull(clazz)) {
|
} else {
|
||||||
List<ResultMap> list = ms.getResultMaps();
|
logger.warn(String.format("请不要使用MPJ预留参数名 %s", Constant.PARAM_TYPE));
|
||||||
if (CollectionUtils.isNotEmpty(list)) {
|
}
|
||||||
ResultMap resultMap = list.get(0);
|
if (CollectionUtils.isNotEmpty(map) && map.containsKey(Constant.CLAZZ)) {
|
||||||
if (resultMap.getType() == MPJResultType.class) {
|
Class<?> clazz = (Class<?>) map.get(Constant.CLAZZ);
|
||||||
args[0] = newMappedStatement(ms, clazz);
|
if (Objects.nonNull(clazz)) {
|
||||||
}
|
List<ResultMap> list = ms.getResultMaps();
|
||||||
|
if (CollectionUtils.isNotEmpty(list)) {
|
||||||
|
ResultMap resultMap = list.get(0);
|
||||||
|
if (resultMap.getType() == MPJResultType.class) {
|
||||||
|
args[0] = newMappedStatement(ms, clazz);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -70,6 +82,7 @@ public class MPJInterceptor implements Interceptor {
|
|||||||
return invocation.proceed();
|
return invocation.proceed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构建新的MappedStatement
|
* 构建新的MappedStatement
|
||||||
*/
|
*/
|
||||||
|
@ -20,8 +20,7 @@ public abstract class MPJAbstractMethod extends AbstractMethod {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) {
|
protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) {
|
||||||
String sqlScript = EMPTY;
|
String sqlScript = SqlScriptUtils.convertIf(String.format("${%s}", WRAPPER_SQLSEGMENT),
|
||||||
sqlScript += SqlScriptUtils.convertIf(String.format(SqlScriptUtils.convertIf(" AND", String.format("%s and %s", WRAPPER_NONEMPTYOFENTITY, WRAPPER_NONEMPTYOFNORMAL), false) + " ${%s}", WRAPPER_SQLSEGMENT),
|
|
||||||
String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT,
|
String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT,
|
||||||
WRAPPER_NONEMPTYOFWHERE), true);
|
WRAPPER_NONEMPTYOFWHERE), true);
|
||||||
sqlScript = SqlScriptUtils.convertWhere(sqlScript) + NEWLINE;
|
sqlScript = SqlScriptUtils.convertWhere(sqlScript) + NEWLINE;
|
||||||
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
25
src/main/java/com/github/yulichang/method/mp/SelectList.java
Normal file
25
src/main/java/com/github/yulichang/method/mp/SelectList.java
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
25
src/main/java/com/github/yulichang/method/mp/SelectMaps.java
Normal file
25
src/main/java/com/github/yulichang/method/mp/SelectMaps.java
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
25
src/main/java/com/github/yulichang/method/mp/SelectObjs.java
Normal file
25
src/main/java/com/github/yulichang/method/mp/SelectObjs.java
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
30
src/main/java/com/github/yulichang/method/mp/SelectOne.java
Normal file
30
src/main/java/com/github/yulichang/method/mp/SelectOne.java
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
25
src/main/java/com/github/yulichang/method/mp/SelectPage.java
Normal file
25
src/main/java/com/github/yulichang/method/mp/SelectPage.java
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
83
src/main/java/com/github/yulichang/method/mp/TableAlias.java
Normal file
83
src/main/java/com/github/yulichang/method/mp/TableAlias.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -25,6 +25,8 @@ public interface Constant {
|
|||||||
|
|
||||||
String CLAZZ = "resultTypeClass_Eg1sG";
|
String CLAZZ = "resultTypeClass_Eg1sG";
|
||||||
|
|
||||||
|
String PARAM_TYPE = "paramType_Gr8re1Ee";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* " LEFT JOIN "
|
* " LEFT JOIN "
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user