|
@@ -11,6 +11,7 @@ import java.util.Objects;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
+import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
|
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
|
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
|
import com.chelvc.framework.base.context.SessionContextHolder;
|
|
import com.chelvc.framework.base.context.SessionContextHolder;
|
|
@@ -28,6 +29,7 @@ import lombok.extern.slf4j.Slf4j;
|
|
import net.sf.jsqlparser.JSQLParserException;
|
|
import net.sf.jsqlparser.JSQLParserException;
|
|
import net.sf.jsqlparser.expression.Alias;
|
|
import net.sf.jsqlparser.expression.Alias;
|
|
import net.sf.jsqlparser.expression.Expression;
|
|
import net.sf.jsqlparser.expression.Expression;
|
|
|
|
+import net.sf.jsqlparser.expression.JdbcParameter;
|
|
import net.sf.jsqlparser.expression.LongValue;
|
|
import net.sf.jsqlparser.expression.LongValue;
|
|
import net.sf.jsqlparser.expression.StringValue;
|
|
import net.sf.jsqlparser.expression.StringValue;
|
|
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
|
|
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
|
|
@@ -46,6 +48,7 @@ import org.apache.ibatis.executor.Executor;
|
|
import org.apache.ibatis.executor.statement.StatementHandler;
|
|
import org.apache.ibatis.executor.statement.StatementHandler;
|
|
import org.apache.ibatis.mapping.BoundSql;
|
|
import org.apache.ibatis.mapping.BoundSql;
|
|
import org.apache.ibatis.mapping.MappedStatement;
|
|
import org.apache.ibatis.mapping.MappedStatement;
|
|
|
|
+import org.apache.ibatis.mapping.ParameterMapping;
|
|
import org.apache.ibatis.mapping.SqlCommandType;
|
|
import org.apache.ibatis.mapping.SqlCommandType;
|
|
import org.apache.ibatis.plugin.Interceptor;
|
|
import org.apache.ibatis.plugin.Interceptor;
|
|
import org.apache.ibatis.plugin.Intercepts;
|
|
import org.apache.ibatis.plugin.Intercepts;
|
|
@@ -55,6 +58,8 @@ import org.apache.ibatis.plugin.Signature;
|
|
import org.apache.ibatis.reflection.SystemMetaObject;
|
|
import org.apache.ibatis.reflection.SystemMetaObject;
|
|
import org.apache.ibatis.session.ResultHandler;
|
|
import org.apache.ibatis.session.ResultHandler;
|
|
import org.apache.ibatis.session.RowBounds;
|
|
import org.apache.ibatis.session.RowBounds;
|
|
|
|
+import org.apache.ibatis.type.TypeHandler;
|
|
|
|
+import org.apache.ibatis.type.UnknownTypeHandler;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -133,13 +138,29 @@ public class PropertyUpdateInterceptor implements Interceptor {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 初始化创建信息默认值
|
|
|
|
|
|
+ * 绑定参数TypeHandler
|
|
*
|
|
*
|
|
- * @param creatable 创建信息
|
|
|
|
|
|
+ * @param table 表信息
|
|
|
|
+ * @param bound 绑定SQL
|
|
|
|
+ * @param columns 字段列表
|
|
|
|
+ * @param expressions 表达式列表
|
|
*/
|
|
*/
|
|
- private void initializeCreatableValue(Creatable<?> creatable) {
|
|
|
|
- creatable.setCreator(SessionContextHolder.getId());
|
|
|
|
- creatable.setCreateTime(new Date());
|
|
|
|
|
|
+ private void bindTypeHandler(TableInfo table, BoundSql bound, List<Column> columns, List<Expression> expressions) {
|
|
|
|
+ List<ParameterMapping> mappings = bound.getParameterMappings();
|
|
|
|
+ Map<String, TableFieldInfo> fields = table.getFieldList().stream()
|
|
|
|
+ .collect(Collectors.toMap(TableFieldInfo::getColumn, field -> field));
|
|
|
|
+ for (int i = 0; i < columns.size(); i++) {
|
|
|
|
+ Column column = columns.get(i);
|
|
|
|
+ Expression expression = expressions.get(i);
|
|
|
|
+ Class<? extends TypeHandler<?>> clazz = fields.get(column.getColumnName()).getTypeHandler();
|
|
|
|
+ if (clazz != null && expression instanceof JdbcParameter) {
|
|
|
|
+ ParameterMapping mapping = mappings.get(((JdbcParameter) expression).getIndex() - 1);
|
|
|
|
+ if (mapping.getTypeHandler() == null || mapping.getTypeHandler() instanceof UnknownTypeHandler) {
|
|
|
|
+ TypeHandler<?> handler = DatabaseContextHolder.initializeTypeHandler(clazz);
|
|
|
|
+ SystemMetaObject.forObject(mapping).setValue("typeHandler", handler);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -155,8 +176,8 @@ public class PropertyUpdateInterceptor implements Interceptor {
|
|
}
|
|
}
|
|
|
|
|
|
// 设置创建人字段值
|
|
// 设置创建人字段值
|
|
- boolean containCreator = this.isContains(insert.getColumns(), Expressions.CREATOR_COLUMN);
|
|
|
|
- if (!containCreator) {
|
|
|
|
|
|
+ boolean includeCreator = !this.isContains(insert.getColumns(), Expressions.CREATOR_COLUMN);
|
|
|
|
+ if (includeCreator) {
|
|
insert.addColumns(Expressions.CREATOR_COLUMN);
|
|
insert.addColumns(Expressions.CREATOR_COLUMN);
|
|
Expression operator = this.operator();
|
|
Expression operator = this.operator();
|
|
ItemsList items = insert.getItemsList();
|
|
ItemsList items = insert.getItemsList();
|
|
@@ -168,8 +189,8 @@ public class PropertyUpdateInterceptor implements Interceptor {
|
|
}
|
|
}
|
|
|
|
|
|
// 设置创建时间字段值
|
|
// 设置创建时间字段值
|
|
- boolean containCreateTime = this.isContains(insert.getColumns(), Expressions.CREATE_TIME_COLUMN);
|
|
|
|
- if (!containCreateTime) {
|
|
|
|
|
|
+ boolean includeCreateTime = !this.isContains(insert.getColumns(), Expressions.CREATE_TIME_COLUMN);
|
|
|
|
+ if (includeCreateTime) {
|
|
insert.addColumns(Expressions.CREATE_TIME_COLUMN);
|
|
insert.addColumns(Expressions.CREATE_TIME_COLUMN);
|
|
Expression datetime = this.datetime();
|
|
Expression datetime = this.datetime();
|
|
ItemsList items = insert.getItemsList();
|
|
ItemsList items = insert.getItemsList();
|
|
@@ -179,17 +200,7 @@ public class PropertyUpdateInterceptor implements Interceptor {
|
|
((ExpressionList) items).getExpressions().add(datetime);
|
|
((ExpressionList) items).getExpressions().add(datetime);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- return !(containCreator && containCreateTime);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 初始化修改信息默认值
|
|
|
|
- *
|
|
|
|
- * @param updatable 修改信息
|
|
|
|
- */
|
|
|
|
- private void initializeUpdatableValue(Updatable<?> updatable) {
|
|
|
|
- updatable.setUpdater(SessionContextHolder.getId());
|
|
|
|
- updatable.setUpdateTime(new Date());
|
|
|
|
|
|
+ return includeCreator || includeCreateTime;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -205,8 +216,8 @@ public class PropertyUpdateInterceptor implements Interceptor {
|
|
}
|
|
}
|
|
|
|
|
|
// 设置更新人字段值
|
|
// 设置更新人字段值
|
|
- boolean containUpdater = this.isContains(insert.getColumns(), Expressions.UPDATER_COLUMN);
|
|
|
|
- if (!containUpdater) {
|
|
|
|
|
|
+ boolean includeUpdater = !this.isContains(insert.getColumns(), Expressions.UPDATER_COLUMN);
|
|
|
|
+ if (includeUpdater) {
|
|
insert.addColumns(Expressions.UPDATER_COLUMN);
|
|
insert.addColumns(Expressions.UPDATER_COLUMN);
|
|
Expression operator = this.operator();
|
|
Expression operator = this.operator();
|
|
ItemsList items = insert.getItemsList();
|
|
ItemsList items = insert.getItemsList();
|
|
@@ -218,8 +229,8 @@ public class PropertyUpdateInterceptor implements Interceptor {
|
|
}
|
|
}
|
|
|
|
|
|
// 设置更新时间字段值
|
|
// 设置更新时间字段值
|
|
- boolean containUpdateTime = this.isContains(insert.getColumns(), Expressions.UPDATE_TIME_COLUMN);
|
|
|
|
- if (!containUpdateTime) {
|
|
|
|
|
|
+ boolean includeUpdateTime = !this.isContains(insert.getColumns(), Expressions.UPDATE_TIME_COLUMN);
|
|
|
|
+ if (includeUpdateTime) {
|
|
insert.addColumns(Expressions.UPDATE_TIME_COLUMN);
|
|
insert.addColumns(Expressions.UPDATE_TIME_COLUMN);
|
|
Expression datetime = this.datetime();
|
|
Expression datetime = this.datetime();
|
|
ItemsList items = insert.getItemsList();
|
|
ItemsList items = insert.getItemsList();
|
|
@@ -229,7 +240,7 @@ public class PropertyUpdateInterceptor implements Interceptor {
|
|
((ExpressionList) items).getExpressions().add(datetime);
|
|
((ExpressionList) items).getExpressions().add(datetime);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- return !(containUpdater && containUpdateTime);
|
|
|
|
|
|
+ return includeUpdater || includeUpdateTime;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -246,19 +257,21 @@ public class PropertyUpdateInterceptor implements Interceptor {
|
|
|
|
|
|
// 设置更新人字段值
|
|
// 设置更新人字段值
|
|
Table table = update.getTable();
|
|
Table table = update.getTable();
|
|
- boolean containUpdater = this.isContains(table, update.getColumns(), Expressions.UPDATER_COLUMN);
|
|
|
|
- if (!containUpdater) {
|
|
|
|
|
|
+ Expression operator = this.operator();
|
|
|
|
+ boolean includeUpdater = operator != Expressions.NULL_VALUE
|
|
|
|
+ && !this.isContains(table, update.getColumns(), Expressions.UPDATER_COLUMN);
|
|
|
|
+ if (includeUpdater) {
|
|
if (StringUtils.isEmpty(table.getAlias())) {
|
|
if (StringUtils.isEmpty(table.getAlias())) {
|
|
update.addColumns(Expressions.UPDATER_COLUMN);
|
|
update.addColumns(Expressions.UPDATER_COLUMN);
|
|
} else {
|
|
} else {
|
|
update.addColumns(new Column(table, Expressions.UPDATER_COLUMN.getColumnName()));
|
|
update.addColumns(new Column(table, Expressions.UPDATER_COLUMN.getColumnName()));
|
|
}
|
|
}
|
|
- update.addExpressions(this.operator());
|
|
|
|
|
|
+ update.addExpressions(operator);
|
|
}
|
|
}
|
|
|
|
|
|
// 设置更新时间字段值
|
|
// 设置更新时间字段值
|
|
- boolean containUpdateTime = this.isContains(table, update.getColumns(), Expressions.UPDATE_TIME_COLUMN);
|
|
|
|
- if (!containUpdateTime) {
|
|
|
|
|
|
+ boolean includeUpdateTime = !this.isContains(table, update.getColumns(), Expressions.UPDATE_TIME_COLUMN);
|
|
|
|
+ if (includeUpdateTime) {
|
|
if (StringUtils.isEmpty(table.getAlias())) {
|
|
if (StringUtils.isEmpty(table.getAlias())) {
|
|
update.addColumns(Expressions.UPDATE_TIME_COLUMN);
|
|
update.addColumns(Expressions.UPDATE_TIME_COLUMN);
|
|
} else {
|
|
} else {
|
|
@@ -266,16 +279,7 @@ public class PropertyUpdateInterceptor implements Interceptor {
|
|
}
|
|
}
|
|
update.addExpressions(this.datetime());
|
|
update.addExpressions(this.datetime());
|
|
}
|
|
}
|
|
- return !(containUpdater && containUpdateTime);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 初始化删除信息默认值
|
|
|
|
- *
|
|
|
|
- * @param deletable 删除信息
|
|
|
|
- */
|
|
|
|
- private void initializeDeletableValue(Deletable<?> deletable) {
|
|
|
|
- deletable.setDeleted(false);
|
|
|
|
|
|
+ return includeUpdater || includeUpdateTime;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -369,19 +373,34 @@ public class PropertyUpdateInterceptor implements Interceptor {
|
|
throw new RuntimeException(e);
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
|
|
- // 构建带默认值SQL语句
|
|
|
|
|
|
+ // 重置SQL语句
|
|
StringBuilder sql = null;
|
|
StringBuilder sql = null;
|
|
for (Statement statement : statements.getStatements()) {
|
|
for (Statement statement : statements.getStatements()) {
|
|
boolean changed = false;
|
|
boolean changed = false;
|
|
if (statement instanceof Insert) {
|
|
if (statement instanceof Insert) {
|
|
|
|
+ // 绑定参数TypeHandler
|
|
Insert insert = (Insert) statement;
|
|
Insert insert = (Insert) statement;
|
|
TableInfo table = DatabaseContextHolder.getTable(insert.getTable().getName());
|
|
TableInfo table = DatabaseContextHolder.getTable(insert.getTable().getName());
|
|
|
|
+ ItemsList items = insert.getItemsList();
|
|
|
|
+ if (items instanceof MultiExpressionList) {
|
|
|
|
+ List<Expression> expressions = ((MultiExpressionList) items).getExpressionLists()
|
|
|
|
+ .stream().flatMap(el -> el.getExpressions().stream()).collect(Collectors.toList());
|
|
|
|
+ this.bindTypeHandler(table, bound, insert.getColumns(), expressions);
|
|
|
|
+ } else {
|
|
|
|
+ this.bindTypeHandler(table, bound, insert.getColumns(), ((ExpressionList) items).getExpressions());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 初始化参数默认值
|
|
Class<?> type = ObjectUtils.ifNull(table, TableInfo::getEntityType);
|
|
Class<?> type = ObjectUtils.ifNull(table, TableInfo::getEntityType);
|
|
changed = type != null && (this.initializeCreatableValue(type, insert)
|
|
changed = type != null && (this.initializeCreatableValue(type, insert)
|
|
|| this.initializeUpdatableValue(type, insert) || this.initializeDeletableValue(type, insert));
|
|
|| this.initializeUpdatableValue(type, insert) || this.initializeDeletableValue(type, insert));
|
|
} else if (statement instanceof Update) {
|
|
} else if (statement instanceof Update) {
|
|
|
|
+ // 绑定参数TypeHandler
|
|
Update update = (Update) statement;
|
|
Update update = (Update) statement;
|
|
TableInfo table = DatabaseContextHolder.getTable(update.getTable().getName());
|
|
TableInfo table = DatabaseContextHolder.getTable(update.getTable().getName());
|
|
|
|
+ this.bindTypeHandler(table, bound, update.getColumns(), update.getExpressions());
|
|
|
|
+
|
|
|
|
+ // 初始化参数默认值
|
|
Class<?> type = ObjectUtils.ifNull(table, TableInfo::getEntityType);
|
|
Class<?> type = ObjectUtils.ifNull(table, TableInfo::getEntityType);
|
|
changed = type != null && this.initializeUpdatableValue(type, update);
|
|
changed = type != null && this.initializeUpdatableValue(type, update);
|
|
}
|
|
}
|
|
@@ -395,8 +414,6 @@ public class PropertyUpdateInterceptor implements Interceptor {
|
|
sql.append(statement.toString());
|
|
sql.append(statement.toString());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- // 重置SQL语句
|
|
|
|
if (sql != null) {
|
|
if (sql != null) {
|
|
SystemMetaObject.forObject(bound).setValue("sql", sql.toString());
|
|
SystemMetaObject.forObject(bound).setValue("sql", sql.toString());
|
|
}
|
|
}
|
|
@@ -413,18 +430,26 @@ public class PropertyUpdateInterceptor implements Interceptor {
|
|
@SuppressWarnings("unchecked")
|
|
@SuppressWarnings("unchecked")
|
|
private <T extends Entity<?>> void modify(T entity, SqlCommandType operation) {
|
|
private <T extends Entity<?>> void modify(T entity, SqlCommandType operation) {
|
|
// 初始化字段默认值
|
|
// 初始化字段默认值
|
|
- if (operation == SqlCommandType.INSERT) {
|
|
|
|
- if (entity instanceof Creatable) {
|
|
|
|
- this.initializeCreatableValue((Creatable<?>) entity);
|
|
|
|
- }
|
|
|
|
|
|
+ if (entity instanceof Creatable || entity instanceof Updatable || entity instanceof Deletable) {
|
|
|
|
+ Date now = new Date();
|
|
|
|
+ Long operator = SessionContextHolder.getId();
|
|
if (entity instanceof Updatable) {
|
|
if (entity instanceof Updatable) {
|
|
- this.initializeUpdatableValue((Updatable<?>) entity);
|
|
|
|
|
|
+ if (operator != null) {
|
|
|
|
+ ((Updatable<?>) entity).setUpdater(operator);
|
|
|
|
+ }
|
|
|
|
+ ((Updatable<?>) entity).setUpdateTime(now);
|
|
}
|
|
}
|
|
- if (entity instanceof Deletable) {
|
|
|
|
- this.initializeDeletableValue((Deletable<?>) entity);
|
|
|
|
|
|
+ if (operation == SqlCommandType.INSERT) {
|
|
|
|
+ if (entity instanceof Creatable) {
|
|
|
|
+ if (operator != null) {
|
|
|
|
+ ((Creatable<?>) entity).setCreator(operator);
|
|
|
|
+ }
|
|
|
|
+ ((Creatable<?>) entity).setCreateTime(now);
|
|
|
|
+ }
|
|
|
|
+ if (entity instanceof Deletable) {
|
|
|
|
+ ((Deletable<?>) entity).setDeleted(false);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- } else if (operation == SqlCommandType.UPDATE && entity instanceof Updatable) {
|
|
|
|
- this.initializeUpdatableValue((Updatable<?>) entity);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
// 设置加密属性值
|
|
// 设置加密属性值
|