From c7c9bd6b08c9da9749a74929e9f9ae4a39b334ed Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期四, 04 十一月 2021 18:49:46 +0800
Subject: [PATCH] fix 修复 用户逻辑删除 差异问题
---
ruoyi-common/src/main/java/com/ruoyi/common/core/mybatisplus/methods/InsertAll.java | 70 +++++++++++++++++++++++++++++++----
1 files changed, 62 insertions(+), 8 deletions(-)
diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/mybatisplus/methods/InsertAll.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/mybatisplus/methods/InsertAll.java
index 84e7ef4..403ea53 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/common/core/mybatisplus/methods/InsertAll.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/mybatisplus/methods/InsertAll.java
@@ -1,32 +1,62 @@
package com.ruoyi.common.core.mybatisplus.methods;
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.core.injector.AbstractMethod;
+import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
+import com.baomidou.mybatisplus.core.metadata.TableInfo;
+import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
+import com.ruoyi.common.utils.StringUtils;
+import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
+import org.apache.ibatis.executor.keygen.KeyGenerator;
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
-import com.baomidou.mybatisplus.core.injector.AbstractMethod;
-import com.baomidou.mybatisplus.core.metadata.TableInfo;
+import java.util.List;
/**
- * 鍗晄ql鎵归噺鎻掑叆
+ * 鍗晄ql鎵归噺鎻掑叆( 鍏ㄩ噺濉厖 )
*
* @author Lion Li
*/
public class InsertAll extends AbstractMethod {
+
+ private final static String[] FILL_PROPERTY = {"createTime", "createBy", "updateTime", "updateBy"};
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
final String sql = "<script>insert into %s %s values %s</script>";
final String fieldSql = prepareFieldSql(tableInfo);
final String valueSql = prepareValuesSqlForMysqlBatch(tableInfo);
+ KeyGenerator keyGenerator = new NoKeyGenerator();
+ String sqlMethod = "insertAll";
+ String keyProperty = null;
+ String keyColumn = null;
+ // 琛ㄥ寘鍚富閿鐞嗛�昏緫,濡傛灉涓嶅寘鍚富閿綋鏅�氬瓧娈靛鐞�
+ if (StringUtils.isNotBlank(tableInfo.getKeyProperty())) {
+ if (tableInfo.getIdType() == IdType.AUTO) {
+ /** 鑷涓婚敭 */
+ keyGenerator = new Jdbc3KeyGenerator();
+ keyProperty = tableInfo.getKeyProperty();
+ keyColumn = tableInfo.getKeyColumn();
+ } else {
+ if (null != tableInfo.getKeySequence()) {
+ keyGenerator = TableInfoHelper.genKeyGenerator(sqlMethod, tableInfo, builderAssistant);
+ keyProperty = tableInfo.getKeyProperty();
+ keyColumn = tableInfo.getKeyColumn();
+ }
+ }
+ }
final String sqlResult = String.format(sql, tableInfo.getTableName(), fieldSql, valueSql);
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
- return this.addInsertMappedStatement(mapperClass, modelClass, "insertAll", sqlSource, new NoKeyGenerator(), null, null);
+ return this.addInsertMappedStatement(mapperClass, modelClass, sqlMethod, sqlSource, keyGenerator, keyProperty, keyColumn);
}
private String prepareFieldSql(TableInfo tableInfo) {
StringBuilder fieldSql = new StringBuilder();
- fieldSql.append(tableInfo.getKeyColumn()).append(",");
+ if (StringUtils.isNotBlank(tableInfo.getKeyColumn())) {
+ fieldSql.append(tableInfo.getKeyColumn()).append(",");
+ }
tableInfo.getFieldList().forEach(x -> fieldSql.append(x.getColumn()).append(","));
fieldSql.delete(fieldSql.length() - 1, fieldSql.length());
fieldSql.insert(0, "(");
@@ -37,9 +67,33 @@
private String prepareValuesSqlForMysqlBatch(TableInfo tableInfo) {
final StringBuilder valueSql = new StringBuilder();
valueSql.append("<foreach collection=\"list\" item=\"item\" index=\"index\" open=\"(\" separator=\"),(\" close=\")\">");
- valueSql.append("#{item.").append(tableInfo.getKeyProperty()).append("},");
- tableInfo.getFieldList().forEach(x -> valueSql.append("#{item.").append(x.getProperty()).append("},"));
- valueSql.delete(valueSql.length() - 1, valueSql.length());
+ if (StringUtils.isNotBlank(tableInfo.getKeyColumn())) {
+ valueSql.append("\n#{item.").append(tableInfo.getKeyProperty()).append("},\n");
+ }
+ List<TableFieldInfo> fieldList = tableInfo.getFieldList();
+ int last = fieldList.size() - 1;
+ for (int i = 0; i < fieldList.size(); i++) {
+ String property = fieldList.get(i).getProperty();
+ if (!StringUtils.equalsAny(property, FILL_PROPERTY)) {
+ valueSql.append("<if test=\"item.").append(property).append(" != null\">");
+ valueSql.append("#{item.").append(property).append("}");
+ if (i != last) {
+ valueSql.append(",");
+ }
+ valueSql.append("</if>");
+ valueSql.append("<if test=\"item.").append(property).append(" == null\">");
+ valueSql.append("DEFAULT");
+ if (i != last) {
+ valueSql.append(",");
+ }
+ valueSql.append("</if>");
+ } else {
+ valueSql.append("#{item.").append(property).append("}");
+ if (i != last) {
+ valueSql.append(",");
+ }
+ }
+ }
valueSql.append("</foreach>");
return valueSql.toString();
}
--
Gitblit v1.9.3