标签:拦截器 SQL ibatis apache org mybatis import invocationArgs class
import org.apache.ibatis.cache.CacheKey; 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.mapping.SqlCommandType; import org.apache.ibatis.plugin.*; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.lang.reflect.Method; import java.sql.Connection; import java.util.Properties; @Intercepts( { @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class}), @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}), @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}), } ) public class SqlContextInterceptor implements Interceptor { private final Logger logger = LoggerFactory.getLogger(SqlContextInterceptor.class); private Properties properties; @Override public Object intercept(Invocation invocation) throws Throwable { Executor executor = (Executor) invocation.getTarget(); Method invocationMethod = invocation.getMethod(); Object[] invocationArgs = invocation.getArgs(); MappedStatement mappedStatement = (MappedStatement) invocationArgs[0]; SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType(); if (!SqlCommandType.SELECT.equals(sqlCommandType)) { return invocation.proceed(); } Object parameter = invocationArgs[1]; RowBounds rowBounds = (RowBounds) invocationArgs[2]; ResultHandler resultHandler = (ResultHandler) invocationArgs[3]; CacheKey cacheKey; BoundSql boundSql; //由于逻辑关系,只会进入一次 if (invocationArgs.length == 4) { //4 个参数时 boundSql = mappedStatement.getBoundSql(parameter); cacheKey = executor.createCacheKey(mappedStatement, parameter, rowBounds, boundSql); } else { //6 个参数时 cacheKey = (CacheKey) invocationArgs[4]; boundSql = (BoundSql) invocationArgs[5]; } // 在这里增加数据权限逻辑 return executor.query(mappedStatement, parameter, rowBounds, resultHandler, cacheKey, boundSql); } @Override public Object plugin(Object target) { if (target instanceof Executor) { return Plugin.wrap(target, this); } return target; } @Override public void setProperties(Properties properties) { this.properties = properties; this.logger.info(this.properties.toString()); } }
标签:拦截器,SQL,ibatis,apache,org,mybatis,import,invocationArgs,class 来源: https://www.cnblogs.com/se7end/p/14122938.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。