|
@@ -0,0 +1,71 @@
|
|
|
+package com.baomidou.mybatisplus.extension.injector.methods.additional;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.annotation.IdType;
|
|
|
+import com.baomidou.mybatisplus.core.enums.SqlMethod;
|
|
|
+import com.baomidou.mybatisplus.core.injector.AbstractMethod;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Constants;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
|
|
+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;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p> 批量新增数据,全字段 insert </p>
|
|
|
+ * <p> 不同的数据库支持度不一样!!! 只在 mysql 下测试过!!! 只在 mysql 下测试过!!! 只在 mysql 下测试过!!! </p>
|
|
|
+ * <p> 除了主键是 <strong> 数据库自增的 </strong> 理论上都可以使用!!! </p>
|
|
|
+ * <p> 如果你使用自增有报错或主键值无法回写到entity,就不要跑来问为什么了,因为我也不知道!!! </p>
|
|
|
+ * <p>
|
|
|
+ * 自己的通用 mapper 如下使用:
|
|
|
+ * int insertBatch(List<T> entityList);
|
|
|
+ * 注意: 不要加任何注解!!!
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author miemie
|
|
|
+ * @since 2018-11-09
|
|
|
+ */
|
|
|
+@SuppressWarnings("all")
|
|
|
+public class InsertBatch extends AbstractMethod {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * mapper 对应的方法名
|
|
|
+ */
|
|
|
+ private static final String MAPPER_METHOD = "insertBatch";
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
|
|
+ KeyGenerator keyGenerator = new NoKeyGenerator();
|
|
|
+ SqlMethod sqlMethod = SqlMethod.INSERT_ONE;
|
|
|
+ String insertSqlColumn = tableInfo.getAllInsertSqlColumn(true);
|
|
|
+ String columnScript = StringPool.LEFT_BRACKET + insertSqlColumn.substring(0, insertSqlColumn.length() - 1)
|
|
|
+ + StringPool.RIGHT_BRACKET;
|
|
|
+ String insertSqlProperty = tableInfo.getAllInsertSqlProperty(true, Constants.ENTITY_SPOT);
|
|
|
+ insertSqlProperty = StringPool.LEFT_BRACKET + insertSqlProperty.substring(0, insertSqlProperty.length() - 1) + StringPool.RIGHT_BRACKET;
|
|
|
+ String valuesScript = SqlScriptUtils.convertForeach(insertSqlProperty, "list", null, Constants.ENTITY, StringPool.COMMA);
|
|
|
+ String keyProperty = null;
|
|
|
+ String keyColumn = null;
|
|
|
+ // 表包含主键处理逻辑,如果不包含主键当普通字段处理
|
|
|
+ if (StringUtils.isNotEmpty(tableInfo.getKeyProperty())) {
|
|
|
+ if (tableInfo.getIdType() == IdType.AUTO) {
|
|
|
+ /** 自增主键 */
|
|
|
+ keyGenerator = new Jdbc3KeyGenerator();
|
|
|
+ keyProperty = tableInfo.getKeyProperty();
|
|
|
+ keyColumn = tableInfo.getKeyColumn();
|
|
|
+ } else {
|
|
|
+ if (null != tableInfo.getKeySequence()) {
|
|
|
+ keyGenerator = TableInfoHelper.genKeyGenerator(tableInfo, builderAssistant, sqlMethod.getMethod(), languageDriver);
|
|
|
+ keyProperty = tableInfo.getKeyProperty();
|
|
|
+ keyColumn = tableInfo.getKeyColumn();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), columnScript, valuesScript);
|
|
|
+ SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
|
|
+ return this.addInsertMappedStatement(mapperClass, modelClass, MAPPER_METHOD, sqlSource, keyGenerator, keyProperty, keyColumn);
|
|
|
+ }
|
|
|
+}
|