1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| package io.github.vampireachao.stream.plugin.mybatisplus.engine.methods;
import com.baomidou.mybatisplus.core.injector.AbstractMethod; import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; import io.github.vampireachao.stream.core.stream.Steam; import io.github.vampireachao.stream.plugin.mybatisplus.engine.constant.PluginConst; import io.github.vampireachao.stream.plugin.mybatisplus.engine.enumration.SqlMethodEnum; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlSource;
import static com.baomidou.mybatisplus.core.enums.SqlKeyword.IN;
public class UpdateOneSql extends AbstractMethod implements PluginConst {
public UpdateOneSql(String methodName) { super(methodName); }
@Override public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) { SqlMethodEnum sqlMethod = SqlMethodEnum.UPDATE_ONE_SQL; String caseWhenScript = buildCaseWhen(tableInfo); StringBuilder whereScript = buildWhereSql(tableInfo); String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), caseWhenScript, whereScript); SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); return this.addUpdateMappedStatement(mapperClass, modelClass, sqlSource); }
private String buildCaseWhen(TableInfo tableInfo) { String safeKeyProperty = SqlScriptUtils.safeParam(ENTITY_DOT + tableInfo.getKeyProperty()); return Steam.of(tableInfo.getFieldList()) .filter(i -> !i.isLogicDelete()) .map(i -> i.getColumn() + EQUALS + CASE + SPACE + tableInfo.getKeyColumn() + NEWLINE + SqlScriptUtils.convertForeach(SqlScriptUtils.convertChoose( String.format(NON_NULL_CONDITION, ENTITY, ENTITY_DOT + i.getProperty()) , String.format(WHEN_THEN, safeKeyProperty, SqlScriptUtils.safeParam(ENTITY_DOT + i.getProperty())), String.format(WHEN_THEN, safeKeyProperty, i.getColumn())), COLLECTION_PARAM_NAME, null, ENTITY, null) + END ) .join(COMMA + NEWLINE) + NEWLINE; }
private StringBuilder buildWhereSql(TableInfo tableInfo) { return new StringBuilder().append(NEWLINE) .append(tableInfo.getKeyColumn()).append(SPACE).append(IN.getSqlSegment()).append(NEWLINE) .append(LEFT_BRACKET) .append(SqlScriptUtils.convertForeach(SqlScriptUtils.safeParam(ENTITY_DOT + tableInfo.getKeyProperty()), COLLECTION_PARAM_NAME, null, ENTITY, COMMA)) .append(RIGHT_BRACKET).append(NEWLINE) .append(tableInfo.getLogicDeleteSql(true, true)); } }
|