Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to intercept and modify SQL query by mybatis

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to intercept and modify SQL queries in mybatis. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Preface

One of the functions of interceptors is that we can intercept calls to certain methods, and we can choose to add some logic before and after the execution of these intercepted methods. you can also execute your own logic when executing these intercepted methods instead of executing the intercepted methods.

One of the original intentions of the Mybatis interceptor is to allow users to implement their own logic at some point without having to touch the logic inherent in Mybatis. For example, I want to perform some fixed operation on all SQL, perform security checks on SQL queries, or record related SQL query logs, and so on.

Mybatis provides us with an Interceptor interface to implement custom interceptors.

Public interface Interceptor {Object intercept (Invocation invocation) throws Throwable; Object plugin (Object target); void setProperties (Properties properties);}

The interface contains three method definitions

The intercept method is the specific processing method of the intercepting object. The passed Invocation contains the strength of the intercepting target class, the intercepting method and the parameter group of the intercepting method. Use the procced of Invocation to execute the original function.

Execute in plugin to determine whether to intercept. If you don't need to intercept, return target directly. If you need to intercept, call the wrap static method in the Plugin class. If the current interceptor implements any interface, it returns a proxy object, otherwise it returns directly (recall the design of the proxy pattern). The proxy object is actually an instance of the Plugin class, which implements the InvocationHandler interface, and the InvocationHandler interface contains only invoke methods for callback methods.

When the interface method of the proxy object is executed, the invoke method of Plugin is called, which packages the object, method, and parameters to be executed as an Invocation object to pass to the interceptor's intercept method. Invocation defines a procced method that executes the intercepted original method.

Plugin class definition

Public class Plugin implements InvocationHandler {private Object target; private Interceptor interceptor; private Map, Set > signatureMap; private Plugin (Object target, Interceptor interceptor, Map, Set > signatureMap) {this.target = target; this.interceptor = interceptor; this.signatureMap = signatureMap;} public static Object wrap (Object target, Interceptor interceptor) {Map, Set > signatureMap = getSignatureMap (interceptor); Class type = target.getClass (); Class [] interfaces = getAllInterfaces (type, signatureMap) If (interfaces.length > 0) {return Proxy.newProxyInstance (type.getClassLoader (), interfaces, new Plugin (target, interceptor, signatureMap));} return target;} public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {try {Set methods = signatureMap.get (method.getDeclaringClass ()); if (methods! = null & methods.contains (method)) {return interceptor.intercept (new Invocation (target, method, args)) } return method.invoke (target, args);} catch (Exception e) {throw ExceptionUtil.unwrapThrowable (e);}} private static Map, Set > getSignatureMap (Interceptor interceptor) {Intercepts interceptsAnnotation = interceptor.getClass (). GetAnnotation (Intercepts.class); if (interceptsAnnotation = = null) {/ / issue # 251 throw new PluginException ("No @ Intercepts annotation was found in interceptor" + interceptor.getClass (). GetName ());} Signature [] sigs = interceptsAnnotation.value () Map, Set > signatureMap = new HashMap, Set > (); for (Signature sig: sigs) {Set methods = signatureMap.get (sig.type ()); if (methods = = null) {methods = new HashSet (); signatureMap.put (sig.type (), methods);} try {Method method = sig.type (). GetMethod (sig.method (), sig.args ()); methods.add (method) } catch (NoSuchMethodException e) {throw new PluginException ("Could not find method on" + sig.type () + "named" + sig.method () + ". Cause: "+ e, e);}} return signatureMap;} private static Class [] getAllInterfaces (Class type, Map, Set > signatureMap) {Set > interfaces = new HashSet > (); while (type! = null) {for (Class c: type.getInterfaces ()) {if (signatureMap.containsKey (c)) {interfaces.add (c);}} type = type.getSuperclass ();} return interfaces.toArray (new Class [interfaces.size ()]);}}

The setProperties method, as its name implies, is used to set the property. There are many ways to initialize properties in bean, and this is one of them.

Mybatis provides the @ Intercepts annotation to declare that the current class is an interceptor with a value of @ Signature array, indicating the interface, method, and corresponding parameter type to be intercepted.

@ Intercepts ({@ Signature (method = "prepare", type = StatementHandler.class, args = {Connection.class}), @ Signature (method = "query", type = StatementHandler.class, args = {java.sql.Statement.class, ResultHandler.class})) public class TenantInterceptor implements Interceptor {.

For example, in the class declaration above, the first Signature annotation intercepts that the input parameter under the StatementHandler class is a method named prepare of Connection.

The second Signature annotation intercepts a method named query that contains two input parameters (of type Statement and ResultHandler, respectively) in the StatementHandler class.

Finally, the declared Interceptor needs to be registered with mybatis's plug to take effect.

This is the end of this article on "how mybatis implements SQL query interception modification". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report