mybatis插件-自动更新updateTime

@Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})})
public class AutoSetUpdateTime implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object[] args = invocation.getArgs();
        MappedStatement ms = (MappedStatement) args[0];
        Object parameterObject = args[1];

        BoundSql boundSql = ms.getBoundSql(parameterObject);

        String sql = boundSql.getSql();
        MySqlStatementParser mySqlStatementParser = new MySqlStatementParser(sql);
        SQLStatement statement = mySqlStatementParser.parseStatement();

        MySqlUpdateStatement mySqlUpdateStatement = (MySqlUpdateStatement)statement;
        List list =  mySqlUpdateStatement.getItems();

        boolean containModifyTime = false;
        for (SQLUpdateSetItem setItem : list){
            String colnumName = setItem.getColumn().toString();
            if("gmt_modified".equals(colnumName)){
                containModifyTime = true;
            }
        }

        if(!containModifyTime) {
            if (null != parameterObject) {
                String[] sqls = sql.split(";");
                StringBuilder newSql = new StringBuilder();
                for (int i=0;i0){
                        newSql.append(";");
                    }
                    int index = sqls[i].indexOf(" where ");
                    String subSql = sqls[i].substring(0,index);
                    newSql.append(subSql).append(",gmt_modified=").append(System.currentTimeMillis());
                    String whereSql = sqls[i].substring(index);
                    newSql.append(whereSql);
                }

                BoundSql bs = new BoundSql(ms.getConfiguration(),newSql.toString(),boundSql.getParameterMappings(),parameterObject);

                MappedStatement newMs = copyFromMappedStatement(ms, new LimitInterceptorBysqlParser.BoundSqlSqlSource(bs));
                for (ParameterMapping mapping : boundSql.getParameterMappings()) {
                    String prop = mapping.getProperty();
                    if (boundSql.hasAdditionalParameter(prop)) {
                        bs.setAdditionalParameter(prop, boundSql.getAdditionalParameter(prop));
                    }
                }
                args[0] = newMs;
            }
        }
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target,this);
    }

    @Override
    public void setProperties(Properties properties) {

    }

    private MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) {
        MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, ms.getSqlCommandType());
        builder.resource(ms.getResource());
        builder.fetchSize(ms.getFetchSize());
        builder.statementType(ms.getStatementType());
        builder.keyGenerator(ms.getKeyGenerator());
        if (ms.getKeyProperties() != null && ms.getKeyProperties().length > 0) {
            builder.keyProperty(ms.getKeyProperties()[0]);
        }
        builder.timeout(ms.getTimeout());
        builder.parameterMap(ms.getParameterMap());
        builder.resultMaps(ms.getResultMaps());
        builder.resultSetType(ms.getResultSetType());
        builder.cache(ms.getCache());
        builder.flushCacheRequired(ms.isFlushCacheRequired());
        builder.useCache(ms.isUseCache());
        return builder.build();
    }
}

 

你可能感兴趣的:(java)