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();
    }
相关文章
相关标签/搜索