Mybatis-plus3.x 增長updateAllColumnById擴展實現

Mybatis-plus從2.x 升級到 3.x 後的變化仍是比較大的,其中就刪除了UpdateAllColumnById方法。項目裏用到的地方比較多,昨天抽時間看下源碼,寫了和擴展從新實現了這個功能。 在這記錄下,也得須要的小夥伴提供個參考。。。java

1 實現MP自定義普通UpdateAllColumnById方法sql

/**
     * <p>
     * 根據 ID 更新有值字段
     * </p>
     *
     * @author yangyining
     */
    @Slf4j
    public class UpdateAllColumnById extends AbstractMethod {

        @Override
        public MappedStatement injectMappedStatement(final Class<?> mapperClass, final Class<?> modelClass, final TableInfo tableInfo) {
            final SqlMethod sqlMethod = SqlMethod.LOGIC_UPDATE_ALL_COLUMN_BY_ID;

            // 反射修改fieldFill值爲update
            final List<TableFieldInfo> fieldList = tableInfo.getFieldList();
            for (final TableFieldInfo tableFieldInfo : fieldList) {
                final Class<? extends TableFieldInfo> aClass = tableFieldInfo.getClass();
                try {
                    final Field fieldFill = aClass.getDeclaredField("fieldFill");
                    fieldFill.setAccessible(true);
                    fieldFill.set(tableFieldInfo, FieldFill.UPDATE);
                } catch (final NoSuchFieldException e) {
                    log.error("獲取fieldFill失敗", e);
                } catch (final IllegalAccessException e) {
                    log.error("設置fieldFill失敗", e);
                }
            }

            final String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(),
                this.sqlSet(false, false, tableInfo, Constants.ENTITY_SPOT),
                tableInfo.getKeyColumn(), Constants.ENTITY_SPOT + tableInfo.getKeyProperty(),
                new StringBuilder("<if test=\"et instanceof java.util.Map\">")
                    .append("<if test=\"et.MP_OPTLOCK_VERSION_ORIGINAL!=null\">")
                    .append(" AND ${et.MP_OPTLOCK_VERSION_COLUMN}=#{et.MP_OPTLOCK_VERSION_ORIGINAL}")
                    .append("</if></if>"));
            final SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, modelClass);
            return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
        }
    }

2 實現MP自定義邏輯刪除LogicUpdateAllColumnById方法app

/**
 * <p>
 * 根據 ID 更新有值字段
 * </p>
 *
 * @author yangyining
 */
@Slf4j
public class LogicUpdateAllColumnById extends AbstractLogicMethod {

    @Override
    public MappedStatement injectMappedStatement(final Class<?> mapperClass, final Class<?> modelClass, final TableInfo tableInfo) {
        final String sql;
        final boolean logicDelete = tableInfo.isLogicDelete();
        final SqlMethod sqlMethod = SqlMethod.LOGIC_UPDATE_ALL_COLUMN_BY_ID;
        final StringBuilder append = new StringBuilder("<if test=\"et instanceof java.util.Map\">")
            .append("<if test=\"et.").append(OptimisticLockerInterceptor.MP_OPTLOCK_VERSION_ORIGINAL).append("!=null\">")
            .append(" AND ${et.").append(OptimisticLockerInterceptor.MP_OPTLOCK_VERSION_COLUMN)
            .append("}=#{et.").append(OptimisticLockerInterceptor.MP_OPTLOCK_VERSION_ORIGINAL).append(StringPool.RIGHT_BRACE)
            .append("</if></if>");
        if (logicDelete) {
            append.append(tableInfo.getLogicDeleteSql(true, false));
        }

        // 反射修改fieldFill值爲update
        final List<TableFieldInfo> fieldList = tableInfo.getFieldList();
        for (final TableFieldInfo tableFieldInfo : fieldList) {
            final Class<? extends TableFieldInfo> aClass = tableFieldInfo.getClass();
            try {
                final Field fieldFill = aClass.getDeclaredField("fieldFill");
                fieldFill.setAccessible(true);
                fieldFill.set(tableFieldInfo, FieldFill.UPDATE);
            } catch (final NoSuchFieldException e) {
                log.error("獲取fieldFill失敗", e);
            } catch (final IllegalAccessException e) {
                log.error("設置fieldFill失敗", e);
            }

        }
        sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(),
            this.sqlSet(logicDelete, false, tableInfo, Constants.ENTITY_SPOT),
            tableInfo.getKeyColumn(), Constants.ENTITY_SPOT + tableInfo.getKeyProperty(),
            append);
        final SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, modelClass);
        return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
    }
}

3 實現自定義普通SQL注入器ide

/**
 * <p>
 * SQL 自定義注入器
 * </p>
 *
 * @author yangyining
 * @since 2018-04-10
 */
public class CustomSqlInjector extends AbstractSqlInjector {


    @Override
    public List<AbstractMethod> getMethodList() {
        return Stream.of(
            new Insert(),
            new Delete(),
            new DeleteByMap(),
            new DeleteById(),
            new DeleteBatchByIds(),
            new Update(),
            new UpdateById(),
            new UpdateAllColumnById(),
            new SelectById(),
            new SelectBatchByIds(),
            new SelectByMap(),
            new SelectOne(),
            new SelectCount(),
            new SelectMaps(),
            new SelectMapsPage(),
            new SelectObjs(),
            new SelectList(),
            new SelectPage()
        ).collect(Collectors.toList());
    }

}

4 實現自定義邏輯刪除SQL注入器ui

/**
 * SQL 自定義邏輯刪除注入器
 *
 * @author yangyn
 * @version 1.0
 */
public class CustomLogicSqlInjector extends AbstractSqlInjector {


    @Override
    public List<AbstractMethod> getMethodList() {
        return Stream.of(
            new Insert(),
            new LogicDelete(),
            new LogicDeleteByMap(),
            new LogicDeleteById(),
            new LogicDeleteBatchByIds(),
            new LogicUpdate(),
            new LogicUpdateById(),
            new LogicUpdateAllColumnById(),
            new LogicSelectById(),
            new LogicSelectBatchByIds(),
            new LogicSelectByMap(),
            new LogicSelectOne(),
            new LogicSelectCount(),
            new LogicSelectMaps(),
            new LogicSelectMapsPage(),
            new LogicSelectObjs(),
            new LogicSelectList(),
            new LogicSelectPage()
        ).collect(Collectors.toList());
    }

}

5 在MybatisPlusConfig配置類中定義SQL注入器beanthis

@Bean
    public ISqlInjector sqlInjector() {
        return new CustomSqlInjector();
    }
相關文章
相關標籤/搜索