欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-07 15:54:03
下面给大家介绍mybatis interceptor 处理查询参数及查询结果,具体代码如下所示: /** * created by windwant on 2...

下面给大家介绍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 处理查询参数及查询结果的实例代码,希望对大家有所帮助