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 处理查询参数及查询结果的实例代码,希望对大家有所帮助
上一篇: 对C++ this指针的理解
下一篇: C语言指针应用(四)-- 链表