Mybatis interceptors (six)

The role of interceptors is that we can intercept calls to certain methods, coupled with our own logic before and after the target method.

A mind Mybatis interceptor designed to provide users sometimes can implement your own logic without having to move the inherent logic Mybatis.

Mybatis provides us with a Interceptor interface by implementing this interface can define our own interceptor (However, to achieve interceptor, the need for mybatis source has a good understanding of the job).

/ ** 
 * MyBatis custom interceptor 
 * Step three: 
 * 1 implemented { @link Intercepts} Interface 
 * 2 knockdown was added annotations { @link Intercepts} 
 *. 3 interceptors add profile 
 * 
 * 1 implemented { @link Intercepts} Interface 
 * Note specific role can be seen in the following code for each method 
 * 2 knockdown was added annotations { @link intercepts} 
 * MyBatis default interceptor intercepts only four types, i.e., four kinds of interface type Executor, StatementHandler, ParameterHandler and ResultSetHandler 
 * our custom interceptors must be annotated mybatis provided to indicate which we want to intercept a class interface four categories 
 * specific rules are as follows: 
 * a: intercepts identify my class is an interceptor 
 * b: Signature is indicated our interceptors which interface of a method which requires interception 
 * type corresponds to one of four types of interfaces, such as is the Executor
 Method * corresponding to the types of interface methods, such as the update method Executor 
 * interface args which corresponds to a method, such as overload reasons Executor the query, a plurality of methods, args parameter type is specified to determine which one methods 
 * 3 configuration file, add the interceptor 
 * interceptor is actually a plugin, in mybatis core configuration file we need to configure our plugin: 
 * <= plugin interceptor "liu.york.mybatis.study.plugin.MyInterceptor"> 
 * <Property name = "username" value = "LiuYork" /> 
 * <Property name = "password" value = "123456" /> 
 * </ plugin> 
 * 
 * blocker sequence 
 * 1 different interceptors order: 
 * the Executor -> ParameterHandler -> StatementHandler ->ResultSetHandler 
 * 
 * 2 interception order for different objects of the same type of interceptor: 
 * in accordance with the position profile mybatis core configuration, sequentially from the top down to intercept 
 * /
@Intercepts ({ 
        @Signature (Method = "Update", type = the Executor. Class , args = {MappedStatement. Class , Object. Class }), 
        @Signature (Method = "Query", type = StatementHandler. Class , args = {the Statement. Class , . the ResultHandler class }) 
}) 
public  class myInterceptor the implements interceptor { 

    / ** 
     * this method is well understood 
     * the role of only one: we do not intercept method, after intercepting what we have to do it? 
     * This method which is what we have to do 
     * 
     before * explain this method, we must understand the method parameters { @link Invocation} What is a ghost? 
     * 1 we know, mybatis default interceptor to intercept only four types of Executor, StatementHandler, ParameterHandler and ResultSetHandler
     * 2 Either the agent, the agent of the target object that we want to intercept the object, example: 
     * For example, we want to intercept { @link Executor # Update (MappedStatement MS, the Parameter Object)} method 
     * Invocation so is this object Invocation there are three parameters method, args target 
     * target is the Executor 
     * method, is Update 
     * args MS is MappedStatement, the parameter Object 
     * 
     * If you still can not understand, I cite a case requirements: see the following method code which needs 
     * 
     * this method runs when you call 
     * / 
    @Override 
    public Object Intercept (invocation invocation) throws Throwable { 

        / * 
         * demand: we need to print sql query log statements for all pre-update
         * I can make our custom interceptor MyInterceptor interception Executor update method, print sql log before the update execution 
         * For example, we intercept point is the update method of Executor: int update (MappedStatement MS, Object the Parameter) 
         * 
         * that when after the success of our print journal, we also need to call is not the way to do this query, such as how to call it? 
         * So there have been Invocation object, which this time is actually a Executor, and corresponding method is to query method, we 
         * want to call this method, you only need to perform invocation.proceed () 
         * / 

        / * because I intercept is Executor so I can turn strong Executor, by default, the Executor is SimpleExecutor * / 
        Executor Executor = (Executor) invocation.getTarget (); 

        / * 
         * 1 obtained from a subject by reflecting Executor MappedStatement objects 
         * 2 get SqlSource target object from MappedStatement 
         * Executor update method which has a parameter MappedStatement, which is included with the sql statement so I get this object
         * The following pseudo-code, ideas:
         * 3 then acquires object from the acquired BoundSql SqlSource object 
         * 4 sql last acquired by BoundSql # getSql methods 
          
         * we directly call invocation.proceed () method* / 
        MappedStatement mappedStatement = ReflectUtil.getMethodField (Executor, MappedStatement. Class ); 
        SqlSource sqlSource = ReflectUtil.getField (. MappedStatement, SqlSource class ) ; 
        boundSql boundSql = sqlSource.getBoundSql (args); 
        String SQL = boundSql.getSql (); 
        logger.info (SQL); 

        / * 
         * now log has been printed, you need to call the target object's methods to complete the update operation 
         * into the source code is actually a common reflection calls Method.invoke (target, args) 
         * target objects corresponding to Executor
         * Method corresponds Executor update method 
         parameter update method * args correspond to the Executor 
         * / 

        return invocation.proceed (); 
    } 

    / ** 
     * This method is also well understood 
     * the role of only one: that is Mybatis creating interceptor agent determining a time will, in the end of the current class MyInterceptor or need to generate a proxy to intercept 
     * if necessary to intercept, generates a proxy object, the proxy is a { @link Plugin}, which implements the dynamic proxy interface jdk { @ Link InvocationHandler}, 
     * if no proxy is returned directly target object itself 
     * 
     * Mybatis Why would determine whether a proxy need it? 
     * By default, Mybatis only intercepted four types of interfaces: Executor, StatementHandler, ParameterHandler and ResultSetHandler 
     * by { @link Intercepts} and { @link Signature} two notes together to complete 
     * Imagine if we did not specify the type of developer on the custom interceptor, or just write an intercept point, such as Object, it Mybatis crazy, do all objects to intercept 
     * So will Mybatis do a judgment, intercept point is not to see these four interfaces inside the method, not the interception is not directly return the target object, and if you need to generate a proxy 
     * 
     is * called when the method is loaded core profile in the mybatis 
     * / 
    @override 
    public Object plugin (Object target) {
         / * 
         * read this method comment, it should be understood that the logic here is only one, is to make mybatis judgment, not to intercept, and then make a decision whether to generate a proxy 
         * 
         * below What on earth is the code, it is this one to get? 
         * Based on the judgment by the reflection Mybatis acquiring annotation Intercepts MyInterceptor interceptor and the Signature, and then parse the value inside, 
         * a first object is determined to block the Executor four types, which StatementHandler, ParameterHandler, ResultSetHandler a 
         * 2 the method name and parameters and then (because of overload) which is determined to be a method of intercepting Note: mybatis eleven intercept method according to any of the four side of the interface
         * 3 to make a decision, it is an object returns or return target object itself (target object implementation class itself four interfaces, we intercepted that these four types) 
         *
         * Well, we understand the logic of it ~~~ What !!! write code to use reflection, and then parse the comments, then according to the parameter type, finally, to generate a proxy object 
         code * me how I would be so tall white on Well, how do? 
         * 
         * This code is to use the following it ha 
         * MyBatis complexity already considered here, it provides this static methods to achieve the above logic 
          Interceptor the setProperties # (the Properties)} can obtain parameters* / 
        return Plugin.wrap (target, the this ); 
    } 

    / ** 
     * This the best way to understand, if we need to use some interceptor variable parameters, but this argument is supported by configurable 
     * similar in Spring @Value ( "$ {}") obtained from application.properties file 
     * this time we You can use this method 
     * 
     * how to use? 
     * Mybatis only need to add the following configuration similar to the configuration file, then { @link 
     * <= plugin Interceptor "liu.york.mybatis.study.plugin.MyInterceptor"> 
     * <Property name = "username" value = " 
     * <Property name = "password" value = "123456" /> 
     * </ plugin> 
     * The method of acquisition parameters: properties.getProperty ( " username "); 
     * 
     * question: Why does the existence of this method, such as direct use @Value (" $ {} ") does not have to get? 
     * The reason is mybatis framework itself is a framework can be used independently, not like doing a lot of this Spring's dependency injection function 
     * 
     * This method mybatis core configuration file is called when loaded 
     * / 
    @Override 
    public  void setProperties (the Properties the Properties) { 
        String username = Properties.getProperty ( "username" ); 
        String password = Properties.getProperty ( "TODO: 2019/2/28 service logic processing ... 
    } 
}

 

Guess you like

Origin www.cnblogs.com/myitnews/p/11563684.html
Recommended