欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2021-03-31 09:11:51|栏目:JAVA代码|点击:

下面给大家介绍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>

上一篇:实例详解Java中ThreadLocal内存泄露

栏    目:JAVA代码

下一篇:JAVA 注解详解及简单实例

本文标题:mybatis interceptor 处理查询参数及查询结果的实例代码

本文地址:http://www.codeinn.net/misctech/91359.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有