mybatis interceptor 处理查询参数及查询结果的实例代码
下面给大家介绍mybatisinterceptor处理查询参数及查询结果,具体代码如下所示:
/**
*Createdbywindwanton2017/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})
})
publicclassEncryptInterceptorimplementsInterceptor{
publicstaticfinalLoggerlogger=LoggerFactory.getLogger(EncryptInterceptor.class);
@Override
publicObjectintercept(Invocationinvocation)throwsThrowable{
dealParameter(invocation);
ObjectreturnValue=invocation.proceed();
dealReturnValue(returnValue);
returnreturnValue;
}
//查询参数加密处理
privatevoiddealParameter(Invocationinvocation){
MappedStatementstatement=(MappedStatement)invocation.getArgs()[0];
Stringmapperl=ConfigUtils.get("mybaits.mapper.path");
StringmethodName=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("MybatisEncryptparametersInterceptor,method:{},args:{}",methodName,invocation.getArgs()[1]);
}
//查询结果解密处理
privatevoiddealReturnValue(ObjectreturnValue){
if(returnValueinstanceofArrayList<?>){
List<?>list=(ArrayList<?>)returnValue;
for(Objectval:list){
if(valinstanceofPassenger){///
//TODO
}
logger.info("MybatisDecryptresultInterceptor,resultobject:{}",ToStringBuilder.reflectionToString(val));
}
}
}
@Override
publicObjectplugin(Objecttarget){
returnPlugin.wrap(target,this);
}
@Override
publicvoidsetProperties(Propertiesproperties){
}
}
添加配置:
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"> <propertyname="typeAliasesPackage"value="com.xx.model"/> <propertyname="dataSource"ref="dataSource"/> <!--自动扫描mapping.xml文件--> <propertyname="mapperLocations"value="classpath*:mybatis/*.xml"></property> <propertyname="plugins"> <array> <beanclass="com.github.pagehelper.PageHelper"> <propertyname="properties"> <value>dialect=hsqldb</value> </property> </bean> <beanclass="com.xx.interceptor.EncryptInterceptor"> <propertyname="properties"> <value>property-key=property-value</value> </property> </bean> </array> </property> </bean>
以上所述是小编给大家介绍的mybatisinterceptor处理查询参数及查询结果的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!