mybatis interceptor 处理查询参数及查询结果的实例代码

所属分类: 软件编程 / java 阅读数: 45
收藏 0 赞 0 分享

下面给大家介绍mybatis interceptor 处理查询参数及查询结果,具体代码如下所示:

/**
 * Created by windwant on 2017/1/12.
 */
@Intercepts({
    @Signature(type=Executor.class,method="update",args={MappedStatement.class,Object.class}),
    @Signature(type=Executor.class,method="query",args={MappedStatement.class,Object.class,RowBounds.class,ResultHandler.class})
})
public class EncryptInterceptor implements Interceptor {
  public static final Logger logger = LoggerFactory.getLogger(EncryptInterceptor.class);
  @Override
  public Object intercept(Invocation invocation) throws Throwable {
    dealParameter(invocation);
    Object returnValue = invocation.proceed();
    dealReturnValue(returnValue);
    return returnValue;
  }
  //查询参数加密处理
  private void dealParameter(Invocation invocation) {
    MappedStatement statement = (MappedStatement) invocation.getArgs()[0];
    String mapperl = ConfigUtils.get("mybaits.mapper.path");
    String methodName = statement.getId().substring(statement.getId().indexOf(mapperl) + mapperl.length() + 1);
    if (methodName.startsWith("UserBaseMapper")){
      if(methodName.equals("UserBaseMapper.updateDriver")){
        ((Driver) invocation.getArgs()[1]).encrypt();
      }
    }
    logger.info("Mybatis Encrypt parameters Interceptor, method: {}, args: {}", methodName, invocation.getArgs()[1]);
  }
  //查询结果解密处理
  private void dealReturnValue(Object returnValue){
    if(returnValue instanceof ArrayList<?>){
      List<?> list = (ArrayList<?>)returnValue;
      for(Object val: list){
        if(val instanceof Passenger){///
          //TODO
        }
        logger.info("Mybatis Decrypt result Interceptor, result object: {}", ToStringBuilder.reflectionToString(val));
      }
    }
  }
  @Override
  public Object plugin(Object target) {
    return Plugin.wrap(target, this);
  }
  @Override
  public void setProperties(Properties properties) {
  }
}

添加配置:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <property name="typeAliasesPackage" value="com.xx.model"/>
       <property name="dataSource" ref="dataSource"/>
       <!-- 自动扫描mapping.xml文件 -->
       <property name="mapperLocations" value="classpath*:mybatis/*.xml"></property>
       <property name="plugins">
           <array>
              <bean class="com.github.pagehelper.PageHelper">
                  <property name="properties">
                     <value>dialect=hsqldb</value>
                  </property>
              </bean>
              <bean class="com.xx.interceptor.EncryptInterceptor">
                  <property name="properties">
                     <value>property-key=property-value</value>
                  </property>
              </bean>
           </array>
       </property>
    </bean>

以上所述是小编给大家介绍的mybatis interceptor 处理查询参数及查询结果的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多