Mybatis source code analysis: static class action MapperMethod of internal SqlCommand

MapperMethod in the internal static class action SqlCommand

 

   In MapperMethod initialization, the two will first initialize the internal static class, SqlCommand is one of them, mainly in the role of SqlCommand execute MapperMethod class () method, SqlCommand to provide a method and query type two id information to use Sqlseesion perform different methods, so SqlCommand is how to get to the query types and methods id it? Which I have done what does it?

  First, look at the code in the execution sequence constructor. Class constructor needs to pass configuration, Mapper Method classes and interfaces.

  1. The method of obtaining the path name and the class where the method from the Method class
  2. MapperStatement the object corresponding to the acquired XML based configuration and interface information mapper class
  3. Determining whether the object is mapped statement is NULL, if not NULL, then derive the id and Sql type of statement to be executed, whether marked with Flush notes If NULL, again to determine the method to be executed on, if the condition is not satisfied, then BindingException will throw an exception.

  Flowchart is as follows:

     

It is worth noting that the realization of the acquisition target is mapped statement by calling resolveMappedStatement () method. See about the following.

. 1   public the SqlCommand (the Configuration Configuration, Class <?> MapperInterface, Method, Method) {
 2       // Get method name 
. 3          Final String methodName = method.getName ();
 . 4       // class where the method of obtaining 
. 5          Final Class declaringClass = <?> method.getDeclaringClass ();
 . 6      // obtain the mapping information statement 
. 7        MappedStatement MS = resolveMappedStatement (mapperInterface, methodName, declaringClass,
 . 8            Configuration);
 . 9        // if the map statement object is NULL, then if the view marked on the label FLUSH method, If so, set the query type FLUSH, otherwise an exception is thrown BindingException representation can not find the interface definition. 
10        IF(MS == null ) {
 . 11          IF (. method.getAnnotation (the Flush class !) = null ) {
 12 is            name = null ;
 13 is            type = SqlCommandType.FLUSH;
 14          } the else {
 15            the throw  new new BindingException ( "Invalid bound Statement (Not found): "
 16                . mapperInterface.getName + () +" "+ methodName);
 . 17          }
 18 is        }
 . 19        // if map statement object is not empty, then set the specified query type, if the type is UNKNOWN, directly thrown BindingException abnormal 
20       else {
21         name = ms.getId();
22         type = ms.getSqlCommandType();
23         if (type == SqlCommandType.UNKNOWN) {
24           throw new BindingException("Unknown execution method for: " + name);
25         }
26       }
27     }

resolveMappedStatement () method

   The method is mainly acquired map statement object in xml configuration, like select, update, delete, insert are required to provide the id and sql statement as well as the reference and the reference information, MappedStatement is xml to java object mapping a < select //> tag would correspond to a MappedStatement object to the current understanding, in the follow-up will be devoted to the presentation. Now a brief description of the code execution flow.

1. Obtain the statement to be executed id number, id number in the form of the class path. Method name

2. if you can find the id of the statement in the configuration class, if you can find, then returned directly MappedStatement instance from class configuration, or continue to look for from the parent class interface, or NULL if not found

3. If the reference is to the class interface path where the path method, the direct return NULL

 

. 1  Private MappedStatement resolveMappedStatement (Class <?> MapperInterface, String methodName,
 2          Class <?> DeclaringClass, the Configuration Configuration) {
 . 3          // Get the name of the interface in the form of statements id. Method name 
. 4        String StatementId mapperInterface.getName = () +. " "+ methodName;
 . 5        // determines whether there is the method of configuration ID 
. 6        IF (configuration.hasStatement (StatementId)) {
 . 7            // return map statement 
. 8          return configuration.getMappedStatement (StatementId);
 . 9        }
 10        // If the interface information is where that kind of thing, direct return NULL 
11        the else IF (mapperInterface.equals (declaringClass)) {
 12 is          return  null ;
 13 is        }
 14        // get all the information in the class interfaces 
15        for (Class <?> superInterface: mapperInterface.getInterfaces ()) {
 16            // from the parent interface find the corresponding method ID, or using a recursive manner the following statement to find 
. 17          IF (declaringClass.isAssignableFrom (superInterface)) {
 18 is            MappedStatement MS = resolveMappedStatement (superInterface, methodName,
 . 19                declaringClass, Configuration);
 20 is            IF (! MS = null ) {
 21              return MS;
22           }
23         }
24       }
25       return null;
26     }
27   }

 

Guess you like

Origin www.cnblogs.com/zhengzuozhanglina/p/11221289.html