mybatis sql拦截器

@Component
@Intercepts({
        @Signature(
                type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class
        })
})
public class MySqlInterceptor implements Interceptor {
    @Autowired
    private RedisService redisService;
    @Autowired
    private CommonObjService commonObjService;
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
        MetaObject metaObject = MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY, SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY, new DefaultReflectorFactory());
        MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
        String id = mappedStatement.getId();
        String sqlCommandType = mappedStatement.getSqlCommandType().toString();
        BoundSql boundSql = statementHandler.getBoundSql();
        String sql = boundSql.getSql().toLowerCase();
        String mSql = sql;

        //注解逻辑判断  添加注解了才拦截
        Class classType = Class.forName(mappedStatement.getId().substring(0, mappedStatement.getId().lastIndexOf(".")));
        String mName = mappedStatement.getId().substring(mappedStatement.getId().lastIndexOf(".") + 1, mappedStatement.getId().length());
        for (Method method : classType.getDeclaredMethods()) {
            if (mName.equals(method.getName())) {
//                InterceptAnnotation interceptorAnnotation = method.getAnnotation(InterceptAnnotation.class);
//                if (interceptorAnnotation.flag()) {
                    if ("SELECT".equals(sqlCommandType) && sql.indexOf("*")>-1){
                        String newSqlColumns = "";
                        String prefix = "";
                        String sqlColumns = StringKit.subString(sql,"select","from");
                        if (sqlColumns.indexOf(".*")>-1){
                            prefix= StringKit.subString(sql,"select",".*").trim();
                        }
                        String tableName = StringUtils.trim(StringKit.subString(sql,"from","where"));
                        if (redisService.get(CommonMDA.REDIS_CONFIG_PREFIX+tableName) == null){
                            newSqlColumns = commonObjService.genColumns(tableName,prefix);
                        }else{
                            newSqlColumns = (String)redisService.get(CommonMDA.REDIS_CONFIG_PREFIX+tableName);
                        }
                        newSqlColumns = " "+newSqlColumns+" ";
                        if (sqlColumns.indexOf("distinct")>-1){
                            newSqlColumns = " distinct"+newSqlColumns;
                        }
                        mSql = sql.replace(sqlColumns,newSqlColumns);
                    }
//                }
            }
        }
        //通过反射修改sql语句
        Field field = boundSql.getClass().getDeclaredField("sql");
        field.setAccessible(true);
        field.set(boundSql, mSql);
        return invocation.proceed();
    }
    @Override
    public Object plugin(Object target) {
        if (target instanceof StatementHandler) {
            return Plugin.wrap(target, this);
        } else {
            return target;
        }
    }
    @Override
    public void setProperties(Properties properties) {
    }
}

你可能感兴趣的:(mybatis sql拦截器)