MybatisPlus 自定义拦截器实现查询的公共字段拼接

1,982 阅读1分钟

1、定义拦截器bean

@Bean("mybatisPlusInterceptor")
@Primary
public MybatisPlusInterceptor mybatisPlusInterceptor(){
    MybatisPlusInterceptor interceptor=new MybatisPlusInterceptor();
    interceptor.addInnerInterceptor(new CommonDbInterceptor());
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); //分页插件
    return interceptor;
}

2、(可选)配置自定义SqlSessionFactory

@Bean("sqlSessionFactory")
    @Primary
    public SqlSessionFactory mybatisSqlSession(@Qualifier("dataSourceResource") DataSource dataSource, GlobalConfig globalConfig,
                                               @Qualifier("mybatisPlusInterceptor")MybatisPlusInterceptor mybatisPlusInterceptor) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory;
        sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        sqlSessionFactory.setTypeAliasesPackage("com.ly.flightsaas.transitservice.core.dal.entity");
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactory.setMapperLocations(resolver.getResources("classpath*:mapper/*.xml"));
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        sqlSessionFactory.setConfiguration(configuration);
        sqlSessionFactory.setPlugins(new Interceptor[] { mybatisPlusInterceptor });
        sqlSessionFactory.setGlobalConfig(globalConfig);
        return sqlSessionFactory.getObject();
    }

3、配置拦截器实现类

public class CommonDbInterceptor implements InnerInterceptor {

    @Override
    public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        String sql = boundSql.getSql();
        StringBuffer whereSql = new StringBuffer("env='" + Global.getConfig("sof-env") + "' ");
        CCJSqlParserManager parserManager = new CCJSqlParserManager();
        try {
            Select select = (Select) parserManager.parse(new StringReader(sql));
            PlainSelect plain = (PlainSelect) select.getSelectBody();
            Expression where = plain.getWhere();
            String oldWhere = where.toString().trim();
            if (where != null) {
                if (oldWhere.startsWith("(")) {
                    oldWhere = oldWhere.substring(1);
                    whereSql.insert(0, "(").append(" AND " + oldWhere + " ");
                } else {
                    whereSql.append(" AND " + oldWhere + " ");
                }
            }
            Expression expression = CCJSqlParserUtil.parseCondExpression(whereSql.toString());
            plain.setWhere(expression);
            PluginUtils.mpBoundSql(boundSql).sql(select.getSelectBody().toString());
        } catch (JSQLParserException e) {
            e.printStackTrace();
        }
    }
}