|
@@ -1,20 +1,14 @@
|
|
|
package com.chelvc.framework.database.mapper;
|
|
|
|
|
|
import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
-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.TableFieldInfo;
|
|
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
|
|
-import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
|
|
import com.chelvc.framework.common.util.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.chelvc.framework.database.annotation.CreateUpdate;
|
|
|
+import lombok.NonNull;
|
|
|
|
|
|
/**
|
|
|
* 插入或修改方法实现
|
|
@@ -22,42 +16,37 @@ import org.apache.ibatis.mapping.SqlSource;
|
|
|
* @author Woody
|
|
|
* @date 2024/2/27
|
|
|
*/
|
|
|
-public class InsertUpdateMethod extends AbstractMethod {
|
|
|
+public class InsertUpdateMethod extends AbstractInsertMethod {
|
|
|
+ public InsertUpdateMethod() {
|
|
|
+ super("insertUpdate");
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
- public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
|
|
- String keyColumn = null;
|
|
|
- String keyProperty = null;
|
|
|
- KeyGenerator keyGenerator;
|
|
|
- if (StringUtils.isEmpty(tableInfo.getKeyProperty())) {
|
|
|
- keyGenerator = new NoKeyGenerator();
|
|
|
- } else if (tableInfo.getIdType() == IdType.AUTO) {
|
|
|
- keyColumn = tableInfo.getKeyColumn();
|
|
|
- keyProperty = tableInfo.getKeyProperty();
|
|
|
- keyGenerator = new Jdbc3KeyGenerator();
|
|
|
- } else if (tableInfo.getKeySequence() != null) {
|
|
|
- keyColumn = tableInfo.getKeyColumn();
|
|
|
- keyProperty = tableInfo.getKeyProperty();
|
|
|
- String method = this.getMethod(SqlMethod.INSERT_ONE);
|
|
|
- keyGenerator = TableInfoHelper.genKeyGenerator(method, tableInfo, this.builderAssistant);
|
|
|
- } else {
|
|
|
- keyGenerator = new NoKeyGenerator();
|
|
|
- }
|
|
|
- List<TableFieldInfo> fields = tableInfo.getFieldList();
|
|
|
- String columns = tableInfo.getKeyInsertSqlColumn(false) +
|
|
|
- this.filterTableFieldInfo(fields, null, TableFieldInfo::getInsertSqlColumn, EMPTY);
|
|
|
- columns = columns.substring(0, columns.length() - 1);
|
|
|
- String properties = tableInfo.getKeyInsertSqlProperty(null, false) +
|
|
|
- this.filterTableFieldInfo(fields, null, i -> i.getInsertSqlProperty(null), EMPTY);
|
|
|
- properties = properties.substring(0, properties.length() - 1);
|
|
|
- String updates = this.filterTableFieldInfo(fields, null,
|
|
|
- field -> field.getColumn() + " = values(" + field.getColumn() + ")", StringPool.COMMA);
|
|
|
- String script = String.format(
|
|
|
+ protected String initializeMethodScript(@NonNull TableInfo table) {
|
|
|
+ List<TableFieldInfo> fields = table.getFieldList();
|
|
|
+ String columns = this.field2column(table, fields);
|
|
|
+ String properties = this.field2property(table, fields);
|
|
|
+ String updates = fields.stream().map(field -> {
|
|
|
+ CreateUpdate annotation = field.getField().getAnnotation(CreateUpdate.class);
|
|
|
+ if (annotation != null && !annotation.enabled()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ StringBuilder buffer = new StringBuilder().append(field.getColumn()).append(" = ");
|
|
|
+ CreateUpdate.Mode mode = annotation == null ? CreateUpdate.Mode.REPLACE : annotation.mode();
|
|
|
+ if (mode == CreateUpdate.Mode.REPLACE) {
|
|
|
+ buffer.append("VALUES(").append(field.getColumn()).append(")");
|
|
|
+ } else if (mode == CreateUpdate.Mode.INCREMENT) {
|
|
|
+ buffer.append(field.getColumn()).append(" + 1");
|
|
|
+ } else if (mode == CreateUpdate.Mode.DECREMENT) {
|
|
|
+ buffer.append(field.getColumn()).append(" - 1");
|
|
|
+ } else {
|
|
|
+ throw new IllegalArgumentException("Not support update mode: " + mode);
|
|
|
+ }
|
|
|
+ return buffer.toString();
|
|
|
+ }).filter(StringUtils::notEmpty).collect(Collectors.joining(StringPool.COMMA));
|
|
|
+ return String.format(
|
|
|
"<script>\nINSERT IGNORE INTO %s (%s) VALUES (%s)\n ON DUPLICATE KEY UPDATE %s\n</script>",
|
|
|
- tableInfo.getTableName(), columns, properties, updates
|
|
|
- );
|
|
|
- SqlSource source = this.languageDriver.createSqlSource(this.configuration, script, modelClass);
|
|
|
- return this.addInsertMappedStatement(
|
|
|
- mapperClass, modelClass, "insertUpdate", source, keyGenerator, keyProperty, keyColumn
|
|
|
+ table.getTableName(), columns, properties, updates
|
|
|
);
|
|
|
}
|
|
|
}
|