![JAR search and dependency download from the Maven repository](/logo.png)
org.zodiac.mybatisplus.injector.methods.UpdateBatchById Maven / Gradle / Ivy
package org.zodiac.mybatisplus.injector.methods;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import java.util.List;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.mapping.SqlSource;
import org.zodiac.mybatisplus.injector.MyBatisPlusMethod;
public class UpdateBatchById extends AbstractMethod {
private static final long serialVersionUID = 6812307363744181869L;
public UpdateBatchById() {
}
@Override
public MappedStatement injectMappedStatement(Class> mapperClass, Class> modelClass, TableInfo table) {
StringBuilder set = new StringBuilder();
set.append("\n");
MyBatisPlusMethod sqlMethod = MyBatisPlusMethod.UPDATE_BATCH_BY_ID_MYSQL;
List fieldList = table.getFieldList();
for (TableFieldInfo fieldInfo : fieldList) {
set.append("\n");
set.append("\n");
set.append(convertIfTag(fieldInfo, "i.", false));
set.append("\nWHEN ").append("#{i.").append(table.getKeyProperty());
set.append("} THEN #{i.").append(fieldInfo.getEl()).append("}");
set.append(convertIfTag(fieldInfo, true));
set.append("\n ");
set.append("\n ");
}
set.append("\n ");
String sql = String.format(sqlMethod.getSql(), table.getTableName(), set.toString(), table.getKeyColumn(),
table.getKeyProperty());
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
}
protected String convertIfTag(SqlCommandType sqlCommandType, TableFieldInfo fieldInfo, String prefix,
boolean colse) {
/* 前缀处理。 */
String property = fieldInfo.getProperty();
if (null != prefix) {
property = prefix + property;
}
/* 判断策略。 */
if (sqlCommandType == SqlCommandType.INSERT && fieldInfo.getInsertStrategy() == FieldStrategy.DEFAULT) {
return "";
}
if (fieldInfo.getInsertStrategy() == FieldStrategy.IGNORED
|| fieldInfo.getUpdateStrategy() == FieldStrategy.IGNORED) {
return "";
} else if (fieldInfo.getInsertStrategy() == FieldStrategy.NOT_EMPTY
| fieldInfo.getUpdateStrategy() == FieldStrategy.NOT_EMPTY) {
if (colse) {
return "";
} else {
return String.format("\n\t", property, property);
}
} else {
/*FieldStrategy.NOT_NULL*/
if (colse) {
return " ";
} else {
return String.format("\n\t", property);
}
}
}
protected String convertIfTag(TableFieldInfo fieldInfo, String prefix, boolean colse) {
return convertIfTag(SqlCommandType.UNKNOWN, fieldInfo, prefix, colse);
}
protected String convertIfTag(TableFieldInfo fieldInfo, boolean colse) {
return convertIfTag(fieldInfo, null, colse);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy