In depth analysis of the system architecture and mapping principle of ibatis framework — reprint

http://www.ibm.com/developerworks/cn/java/j-lo-ibatis-principle/

Ibatis maps Java objects into SQL statements through SQL map and converts the result set into Java objects. Compared with other ORM frameworks, ibatis not only solves the mapping between Java objects and input parameters and result sets, but also allows users to easily use SQL statements by handwriting. This paper mainly introduces the architecture and running process of ibatis framework, and how ibatis completes the parsing of SQL statements and the establishment of the mapping relationship between Java objects and data fields. Finally, an example is used to illustrate how ibatis helps us complete the work.

Generally speaking, the system structure of ibatis is relatively simple. It mainly completes two things: establishing a connection with the database according to the JDBC specification; Through reflection, we can get through the mutual transformation relationship between Java objects and database parameters. Ibatis's framework structure also organizes the class hierarchy according to this idea. In fact, it is a typical interactive framework. Prepare the necessary conditions for interaction in advance, and then build an interactive environment. The interactive environment is also divided into sessions, and each session also has an environment. When these environments are ready, the rest is to exchange data. In fact, when it comes to network communication, it will generally be handled in a similar way. Figure 1 shows the main class hierarchy of ibatis framework:

In the above class diagram, the sqlmapclient interface on the left mainly defines the operation behavior of the client, including select, insert, update and delete. The right side mainly defines the execution environment of the current client in the current thread. Sqlmapsession can be shared or created by yourself. If it is created by yourself, you must call the close interface to close it at the end. When the user holds the sqlmapclientimpl object, it can work with ibatis. Another class sqlmapexecutordelegate will also be mentioned here. From its name, it can be seen that it is an execution agent class. This class is very important because it couples the execution behavior of the client and the execution environment. It holds the data required for the execution of the operation and provides an environment for managing the dependency of the execution operation. Therefore, it is a strongly coupled class and can also be regarded as a tool class. The main design purpose of ibatis is to make it more convenient for us to manage the input and output data when executing SQL. Therefore, how to write SQL and obtain the execution results of SQL is the core competitiveness of ibatis. So how does ibatis achieve its core competitiveness? An important part of ibatis framework is its sqlmap configuration file. The core of sqlmap configuration file is statement statement, including Ciud. Ibatis obtains all statement execution statements by parsing the sqlmap configuration file. At the same time, it will form two objects, parametermap and resultmap, which are used to process parameters and SQL objects handed over to the database for processing after parsing. In this way, the database connection is removed, and the execution conditions of an SQL have been met. Figure 2 describes the class structure diagram related to statement: Figure 2 shows the basic structural relationship around SQL execution, but another key part is how to define the relationship between parameters in SQL statements and Java objects, which also involves a series of problems such as the conversion from Java types to database types. The general process of data mapping is as follows: parse the parameters according to the SQL statement defined in the statement, save them in the map collection according to the order of their occurrence, and parse the Java data type of the parameters according to the parametermap object type defined in the statement. The typehandler object is built according to its data type, and the parameter value is copied through the dataexchange object. Figure 3 shows the class structure related to parameter mapping: Figure 3 shows the mapping structure of input parameters, and the mapping of returned result resultmap is similar. The main purpose is to solve the correspondence between the parameters in the SQL statement and the column names of the returned results and the attributes in parameterclass and resultclass defined in the statement. The structure of the main classes of ibatis framework was analyzed in general. Here we mainly look at how these classes are connected and work. Figure 4 describes the main execution steps of the whole process. The creation and release of the sqlmapsession object described in the above figure will vary according to different situations, because sqlmapsession is responsible for creating the connection to the database, including the management of transactions. Ibatis can manage the management transactions by itself or externally. Ibatis manages itself by sharing the sqlmapsession object, Multiple statements share an sqlmapsession instance during execution, and are thread safe. If it is an external program management, you should control the life cycle of sqlmapsession object by yourself. Figure 5 is a detailed sequence diagram of executing a statement through spring calling ibatis: (see Figure 5.) ibatis mainly works on connection and interaction, so different transaction environments must be designed according to different transaction costs. Next, we will analyze how a statement completes the mapping according to a specific example. We will use a typical query statement to see how the data in Java objects are assigned to the parameters in SQL, and then look at the query of SQL How the query results are converted into Java objects. Let's take a look at part of the code and configuration file of the example. For the complete code, please see the attachment. Spring's ApplicationContext configuration file: < bean > < property > < bean > < bean > < property > the following is account A statement of XML: the following is the test class of Java: the SQL parsing mentioned here is only for the SQL statements defined in the ibatis configuration file, as shown in Listing 2 in the previous section. Unlike standard SQL statements, The assignment of the parameter is "# "Package variable name. How to resolve this variable is what ibatis needs to do. Of course, there are many other forms of SQL expression, such as dynamic SQL. Now we are concerned that when we execute: ibatis will select the statement in Listing 2 to resolve, and finally resolve it into a standard SQL and submit it to the database for execution, and two will be set Select condition parameters. What are the details of parameter mapping in this process? As explained in the second section above, ibatis will parse the sqlmap configuration file into statements, including parametermap, resultmap, and the parsed SQL. After ibatis builds the requestscope execution environment, the work to be done is to extract a parameter array from the passed object data combined with the information in the parametermap. The order of this array corresponds to the order of parameters in SQL, and then Preparedstatement will be called SetXXX (I, parameter) submits parameters. In Listing 3, We assign the ID attribute and firstname attribute of the account object to 1 and "Tao" respectively ", when executing the code in Listing 4, ibatis must pass these two attribute values to the parameters of the object in the SQL statement in Listing 2. In fact, how to do this is very simple. Figure 3 describes the relationship between the classes related to parametermap. These classes save all the necessary information for initializing the sqlmap configuration file to parse the statement in Listing 2 The specific information is as follows: the final SQL statement is: #id: integer # will be resolved to JDBC type of integer, and the parameter value takes the ID attribute of the account object# Firstname # is also resolved to the firstname attribute of the account object, and parameterclass = "account" indicates the class type of account. Notice in Listing 5 that #id: integer # and #firstname # are replaced with "?", How does ibatis ensure their order? In the process of parsing Listing 2, ibatis will take out the legal variable names according to the "#" separator to build a parameter object array. The order of the array is the order in which the variables appear in SQL. Ibatis then creates appropriate dataexchange and parameterplan objects based on these variables and the types specified by parameterclass. In the parameterplan object, the list of setter and getter methods of variables is saved in the previous order. Therefore, the assignment of parameter is to obtain the parameter value array corresponding to Listing 5 by using the reflection mechanism according to the getter method list saved in parameterplan and the passed in account object, and then submit this array to the database according to the specified JDBC type. The above processes can be clearly described by the timing diagram in Figure 6: in step 8 in Figure 4, if the value value is null, the Preparedstatement will be set Setnull (I, JDBC type) an error may occur if the variable in Listing 2 does not have the JDBC type set. After executing SQL, the database will return the execution result. In the example in Section 4, there are two pieces of information with ID 1 and firstname "Tao". How can ibatis set these two records to the account object? Similar to parametermap, the resources required to fill in the returned information have been included in the resultmap. When you have a resultset object that saves the returned results, you need to map the column names to the corresponding properties of the account object. The process is as follows: create the return object according to the resultclass defined in the resultmap, here is the account object. Get all writable property arrays of this object, that is, the setter method, and then match the previous property array according to the column names in the returned resultset, The matching results are constructed into a set (resultmappinglist), followed by the selection of dataexchange type and accessplan type to support the real data exchange. According to the resultmappinglist set, the corresponding values of the columns are taken from the resultset to form a value array (columnvalues), the order of this array is the order of the corresponding column names in SQL. Finally, set the columnvalues value to the object by calling the setter method of the attribute of the account object. This process can be represented by the following sequence diagram: the first two sections mainly describe the mapping principle of input parameters and output results, which will be analyzed in combination with the example in Section 4 The result of executing the code in Listing 3. The result printed by executing the code shown in Listing 3 is: the above result seems different from the expected result. Look at the code, the attribute values of the account object we insert into the database are {1, "Tao", "Bao"“ junshan@taobao.com ”, "time"}, and then call the query in Listing 2 to return the same result. The result of ID is incorrect, and the value of date attribute is missing. If you take a closer look at the statement in Listing 2, you can find that the column names of the returned results are {acc_id, firstname, LastName, emailaddress, acc_date} respectively, where id and date cannot be mapped to the attributes of the account class. ID is assigned the default number 0, while date is not assigned. Another noteworthy thing is that the variable ID is followed by the JDBC type. Is this JDBC type useful? It is usually useless, so you can not set it. Ibatis will automatically select the default type. However, if you want this value to be empty, there may be a problem if you do not specify the JDBC type. Although it works normally in Oracle, it will cause multiple compilation of the current SQL, which will affect the performance of the database. Also, if the same Java type corresponds to multiple JDBC types (for example, the JDBC type corresponding to date includes java.sql.date and java.sql.timestamp). You can save different values to the database by specifying the JDBC type. If you use the most concise words to summarize the main functions of ibatis, I think the following codes are enough to summarize. Ibatis decomposes and packages the above lines of code, but the final execution is still these lines of code. The first two lines are the management of the data source of the database, including transaction management. Lines 3 and 4 ibatis manage the mapping of SQL and input parameters through the configuration file. Lines 6, 7 and 8 are the mapping of the returned results obtained by ibatis to Java objects, which are also managed through the configuration file. The configuration file corresponds to the corresponding code, as shown in the figure: the purpose of ibatis is to put the data concerned by users and easy to change into the configuration file for configuration, which is convenient for user management. The process and fixed are handed over to ibatis for implementation. This is simple and convenient for users to operate the database, which is also the value of ibatis. To sum up, the above is the in-depth analysis of the system architecture and mapping principle of ibatis framework collected by programming home for you -- reprint all the contents. I hope this article can help you solve the problem of in-depth analysis of the system architecture of ibatis framework
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>