出现的问题
项目中使用了别人封装好的jar包,使用的是逻辑删除,但是在我们的数据库中由于唯一索引的存在,必须使用物理删除,因此需要对使用到逻辑删除的sql做监控,并且求改这些sql。
代码
/*** mybatis拦截器* 对所有sql语句进行监视,修改*/
@Slf4j
@Component
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
public class SqlignoreDeletedInterceptor implements Interceptor {@Autowiredprivate lignoreDeleteTablesBeen lignoreDeleteTables;@Overridepublic Object intercept(Invocation invocation) throws Throwable {StatementHandler statementHandler = (StatementHandler) invocation.getTarget();//通过MetaObject优雅访问对象的属性,这里是访问statementHandler的属性;:MetaObject是Mybatis提供的一个用于方便、//优雅访问对象属性的对象,通过它可以简化代码、不需要try/catch各种reflect异常,同时它支持对JavaBean、Collection、Map三种类型对象的操作。MetaObject metaObject = MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY, SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY, new DefaultReflectorFactory());//先拦截到RoutingStatementHandler,里面有个StatementHandler类型的delegate变量,其实现类是BaseStatementHandler,然后就到BaseStatementHandler的成员变量mappedStatementMappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");//id为执行的mapper方法的全路径名,如com.uv.dao.UserMapper.insertUserString id = mappedStatement.getId();// log.info("id为执行的mapper方法的全路径名:" + id);//sql语句类型 select、delete、insert、updateString sqlCommandType = mappedStatement.getSqlCommandType().toString();BoundSql boundSql = statementHandler.getBoundSql();//获取到原始sql语句String sql = boundSql.getSql();// log.info("拦截的sql语句:" + sql);//可以先打印出原始的sql语句,然后根据实际情况修改,我个人建议是下面这样修改//只在原sql上添加条件而不是删除条件sql = lignore(sql);log.info("修改之后的sql:" + sql);//通过反射修改sql语句//下面类似于替换sqlField field = boundSql.getClass().getDeclaredField("sql");field.setAccessible(true);field.set(boundSql, sql);return invocation.proceed();}@Overridepublic Object plugin(Object target) {return Interceptor.super.plugin(target);}
@Overridepublic void setProperties(Properties properties) {Interceptor.super.setProperties(properties);}/** 自己需要修改原sql的规则* 校验表是否需要去掉deleted=0** @param*/private String lignore(String sql) {List<String> tables = lignoreDeleteTables.getLignoreDeleteTables();if (!tables.isEmpty()) {for (String table : tables) {if (sql.contains(table)) {sql = sql.replaceAll("\n", " ");//过滤到换行符。sql = sql.replaceAll("\\s+", " "); //多个空格变一个空格。log.info("格式化后的sql:" + sql);if (sql.contains("SET deleted=1")) {sql = sql.replaceAll("SET deleted=1", "");sql = sql.replaceAll("UPDATE", "DELETE FROM");return sql.replaceAll("AND deleted=0", "");//TODO 这个写法有问题,连表查询会导致sql异常,暂时增加了判断条件} else if (!sql.contains(".deleted") && sql.contains("deleted = 0 AND")) {return sql.replaceAll("deleted = 0 AND", "");} else if (!sql.contains(".deleted") && sql.contains("deleted=0 AND")) {return sql.replaceAll("deleted=0 AND", "");} else if (!sql.contains(".deleted") && sql.contains("AND deleted = 0")) {return sql.replaceAll("AND deleted = 0", "");} else if (!sql.contains(".deleted") && sql.contains("AND deleted=0")) {return sql.replaceAll("AND deleted=0", "");} else if (sql.contains("WHERE deleted = 0")) {return sql.replaceAll("WHERE deleted = 0", "");} else if (sql.contains("WHERE deleted=0")) {return sql.replaceAll("WHERE deleted=0", "");} else {return sql;}}}}return sql;}}
结果
最终运行起来还是满足了对sql的监控和修改功能。