Mybatis interceptor processes query parameters and query results. mybatisinterceptor

Source: Internet
Author: User

Mybatis interceptor processes query parameters and query results. mybatisinterceptor

/*** Created by windwant on login /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 (EncryptInte Rceptor. class); @ Override public Object intercept (Invocation invocation) throws Throwable {dealParameter (invocation); Object returnValue = invocation. proceed (); dealReturnValue (returnValue); return returnValue;} // query parameter encryption private void dealParameter (Invocation invocation) {MappedStatement statement = (MappedStatement) invocation. getArgs () [0]; String mapperl = ConfigUtils. get ("mybaits. mapper. pat H "); 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]);} // decrypt the query result to process private vo Id 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 Properties ){}}

Add Configuration:

<Bean id = "sqlSessionFactory" class = "org. mybatis. spring. sqlSessionFactoryBean "> <property name =" typeAliasesPackage "value =" com. xx. model "/> <property name =" dataSource "ref =" dataSource "/> <! -- Automatically scans mapping. xml file --> <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>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.