diff --git a/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJTableInfoHelper.java b/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJTableInfoHelper.java index c32aa01..b62cd21 100644 --- a/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJTableInfoHelper.java +++ b/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJTableInfoHelper.java @@ -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()); + } + } } diff --git a/src/main/java/com/github/yulichang/injector/MPJSqlInjector.java b/src/main/java/com/github/yulichang/injector/MPJSqlInjector.java index 094c8d5..7b022f2 100644 --- a/src/main/java/com/github/yulichang/injector/MPJSqlInjector.java +++ b/src/main/java/com/github/yulichang/injector/MPJSqlInjector.java @@ -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 METHOD_LIST = Arrays.asList("SelectOne", "SelectCount", + "SelectMaps", "SelectMapsPage", "SelectObjs", "SelectList", "SelectPage"); + /** * 升级到 mybatis plus 3.4.3.2 后对之前的版本兼容 */ - @SuppressWarnings({"unused", "deprecation"}) + @SuppressWarnings("unused") public List getMethodList(Class mapperClass) { List 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 getMethodList(Class mapperClass, TableInfo tableInfo) { List 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 getSelectMethod() { + List 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); diff --git a/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java b/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java index 1be1daf..daaaccb 100644 --- a/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java +++ b/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java @@ -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 EMPTY_RESULT_MAPPING = new ArrayList<>(0); /** @@ -50,17 +58,21 @@ public class MPJInterceptor implements Interceptor { if (args[0] instanceof MappedStatement) { MappedStatement ms = (MappedStatement) args[0]; if (args[1] instanceof Map) { - Map map = (Map) args[1]; - if (CollectionUtils.isNotEmpty(map)) { - if (map.containsKey(Constant.CLAZZ)) { - Class clazz = (Class) map.get(Constant.CLAZZ); - if (Objects.nonNull(clazz)) { - List list = ms.getResultMaps(); - if (CollectionUtils.isNotEmpty(list)) { - ResultMap resultMap = list.get(0); - if (resultMap.getType() == MPJResultType.class) { - args[0] = newMappedStatement(ms, clazz); - } + Map map = (Map) 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 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(); } + /** * 构建新的MappedStatement */ diff --git a/src/main/java/com/github/yulichang/method/MPJAbstractMethod.java b/src/main/java/com/github/yulichang/method/MPJAbstractMethod.java index 4417285..933de31 100644 --- a/src/main/java/com/github/yulichang/method/MPJAbstractMethod.java +++ b/src/main/java/com/github/yulichang/method/MPJAbstractMethod.java @@ -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; diff --git a/src/main/java/com/github/yulichang/method/mp/SelectCount.java b/src/main/java/com/github/yulichang/method/mp/SelectCount.java new file mode 100644 index 0000000..45f9271 --- /dev/null +++ b/src/main/java/com/github/yulichang/method/mp/SelectCount.java @@ -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)); + } +} \ No newline at end of file diff --git a/src/main/java/com/github/yulichang/method/mp/SelectList.java b/src/main/java/com/github/yulichang/method/mp/SelectList.java new file mode 100644 index 0000000..a04cc76 --- /dev/null +++ b/src/main/java/com/github/yulichang/method/mp/SelectList.java @@ -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)); + } +} diff --git a/src/main/java/com/github/yulichang/method/mp/SelectMaps.java b/src/main/java/com/github/yulichang/method/mp/SelectMaps.java new file mode 100644 index 0000000..d7c195d --- /dev/null +++ b/src/main/java/com/github/yulichang/method/mp/SelectMaps.java @@ -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)); + } +} diff --git a/src/main/java/com/github/yulichang/method/mp/SelectMapsPage.java b/src/main/java/com/github/yulichang/method/mp/SelectMapsPage.java new file mode 100644 index 0000000..e43532d --- /dev/null +++ b/src/main/java/com/github/yulichang/method/mp/SelectMapsPage.java @@ -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)); + } +} diff --git a/src/main/java/com/github/yulichang/method/mp/SelectObjs.java b/src/main/java/com/github/yulichang/method/mp/SelectObjs.java new file mode 100644 index 0000000..aea2cfe --- /dev/null +++ b/src/main/java/com/github/yulichang/method/mp/SelectObjs.java @@ -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)); + } +} diff --git a/src/main/java/com/github/yulichang/method/mp/SelectOne.java b/src/main/java/com/github/yulichang/method/mp/SelectOne.java new file mode 100644 index 0000000..21ae56b --- /dev/null +++ b/src/main/java/com/github/yulichang/method/mp/SelectOne.java @@ -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原生方法 + *

+ * 查询满足条件一条数据,为了精简注入方法,该方法采用 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)); + } +} diff --git a/src/main/java/com/github/yulichang/method/mp/SelectPage.java b/src/main/java/com/github/yulichang/method/mp/SelectPage.java new file mode 100644 index 0000000..5ca3b37 --- /dev/null +++ b/src/main/java/com/github/yulichang/method/mp/SelectPage.java @@ -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)); + } +} diff --git a/src/main/java/com/github/yulichang/method/mp/TableAlias.java b/src/main/java/com/github/yulichang/method/mp/TableAlias.java new file mode 100644 index 0000000..4f2925b --- /dev/null +++ b/src/main/java/com/github/yulichang/method/mp/TableAlias.java @@ -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); + } + } +} diff --git a/src/main/java/com/github/yulichang/toolkit/Constant.java b/src/main/java/com/github/yulichang/toolkit/Constant.java index 31854d7..e6ee5c2 100644 --- a/src/main/java/com/github/yulichang/toolkit/Constant.java +++ b/src/main/java/com/github/yulichang/toolkit/Constant.java @@ -25,6 +25,8 @@ public interface Constant { String CLAZZ = "resultTypeClass_Eg1sG"; + String PARAM_TYPE = "paramType_Gr8re1Ee"; + /** * " LEFT JOIN " */