场景
在项目中,会出现一些对sql处理的需求,如果sql操作很多,为了简化处理,可以在sql执行的时候加入一个拦截器,并对将要执行的sql进行统一的处理。
这里已使用了mybatisplus客户端为例的实现方式。
代码实现
- maven引入依赖jar,数据库配置就不写这了。
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.3</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.5</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency>
- 自定义拦截器
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.util.TablesNamesFinder;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;@Slf4j
public class LizzMybatisIntercepts implements InnerInterceptor {@Overridepublic boolean willDoQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {log.info("#####willDoQuery");return false;}@Overridepublic void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {log.info("#####beforeQuery");}@Overridepublic boolean willDoUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {log.info("#####willDoUpdate");// 一堆sql处理仅供参考BoundSql boundSql = ms.getBoundSql(parameter);ms.getSqlSource().getClass();String sql = boundSql.getSql();Statement statement = null;try {statement = CCJSqlParserUtil.parse(sql);} catch (JSQLParserException e) {e.printStackTrace();}TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();List<String> tableList = tablesNamesFinder.getTableList(statement);log.info("sql:{}",sql);return false;}@Overridepublic void beforeUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {log.info("#####beforeUpdate");}@Overridepublic void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout) {log.info("#####beforePrepare");}@Overridepublic void beforeGetBoundSql(StatementHandler sh) {log.info("#####beforeGetBoundSql");}@Overridepublic void setProperties(Properties properties) {log.info("#####setProperties");}
}
- 增加拦截器
@Configuration
public class CipherMybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();// 自定义拦截器,先添加先执行。interceptor.addInnerInterceptor(new LizzMybatisIntercepts());// 自带分页拦截器interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;}
}