Mybatis-mapper proxy principle, mybatis-mapper

Source: Internet
Author: User

Mybatis-mapper proxy principle, mybatis-mapper
Original dao LAYER DEVELOPMENT

After we developed the first applet using mybatis, I believe that you have come up with a rough idea for dao layer development. Other configurations do not need to be changed. The original test method should be the dao method, and the original return value can be directly received at the dao layer. It is still the old one. First, the big framework, and then the configuration file and the UserMapper. xml file are written. If you do not understand, please refer to my blog "Getting started first program".

What we need to do is to first establish a session factory and then create a session (SqlSession) using the session factory ). Then, the SQL statement is obtained by reading the configuration file, executed, and data is returned to the dao layer object. The procedure is as follows:

First, create a dao layer interface based on the original one.

    

Then write an implementation class for the dao layer interface

1 public class UserDaoImpl implements UserDao {2 3 // SqlSessionFactory 4 needs to be injected into dao implementation class // here 5 private SqlSessionFactory sqlSessionFactory is injected through the constructor; 6 7 public UserDaoImpl (SqlSessionFactory sqlSessionFactory) {8 this. sqlSessionFactory = sqlSessionFactory; 9} 10 11 @ Override12 public User findUserById (int id) throws Exception {13 SqlSession sqlSession = sqlSessionFactory. openSession (); 14 15 User user = sqlSession. selectOne ("test. findUserById ", id); 16 17 // release resource 18 sqlSession. close (); 19 20 return user; 21 22} 23 24 @ Override25 public void insertUser (User user) throws Exception {26 SqlSession sqlSession = sqlSessionFactory. openSession (); 27 28 // execute the insert operation 29 sqlSession. insert ("test. insertUser ", user); 30 31 // submit transaction 32 sqlSession. commit (); 33 34 // release resource 35 sqlSession. close (); 36 37} 38 39 @ Override40 public void deleteUser (int id) throws Exception {41 SqlSession sqlSession = sqlSessionFactory. openSession (); 42 43 // execute the insert operation 44 sqlSession. delete ("test. deleteUser ", id); 45 46 // submit transaction 47 sqlSession. commit (); 48 49 // release resource 50 sqlSession. close (); 51 52} 53 54}

Then we will test the code we have written.

In this case, there are some problems:

1. There are a lot of repeated code in the dao interface implementation class, which will increase the workload of programmers.

2. In the implementation class, the SqlSession method hardcodes the statement id (where "test. findUserById" is the statment id) when calling the SQL statement in the ing file.

    

3. variables passed in when the SqlSession method is called. Because the sqlsession method uses generics, even if the variable type is not the parameter type in the Dao method, no error is reported during compilation, which is not conducive to program development.

    

To solve these problems, we can eliminate the development of implementation classes on the entity layer by meeting some specifications. The proxy implementation classes provided by the mybatis framework dynamically call our ing files for us, then, execute the methods in the class.

Principle of mapper proxy method

Before that, the name of the mapper proxy is different from that of the previous one. Here, the interface name is changed from ** to ** Mapper. For example, the original User interface is changed to UserMapper. Its content and nature have not changed, but it has changed its naming conventions. Besides the interface, the ing file is changed from User. xml to UserMapper. xml, and its content remains unchanged.

The idea of the mapper proxy method is that the programmer writes the persistent layer interface.

    

 

And the configuration file corresponding to each interface.

    

In our original method, we need to write another interface implementation class to build the SqlSessionFactory and the corresponding SqlSession) then read the SQL statement in the ing file through the session (which is not accurate here, but can be understood as follows ). Then I can write some articles on this entity class:

1. We can create a proxy class. When the corresponding interface is called, the proxy class will create this object, and it is not difficult to create a session factory or session.

2. There is also the name of the method called by our sqlSession object. This is easy to understand and corresponds to the tag of our ing file.

3. The following is the problem of passing in parameters and receiving return values:

3.1 input parameters: We need to input two parameters in total. One is the string of the statement id of the ing file, and the other is the required parameter for execution. It is difficult to generate this string. The id of the stating file corresponding to each of our methods. For this problem, mybatis provides our solution: the strings read by our object layer are as follows:Test. insertUser

    This id can be divided into two parts. test Is the namespace value in our ing file, which makes it consistent with the address of the persistent layer interface, in this way, the proxy object can dynamically generate the First Half of the id through the interface address, that is, the part of test; the second half is to make the Statement id in the ing file consistent with the interface method.

That is to say, when you call a method of an interface, mybatis can generate a string passed in by the sqlsession object method based on the interface address and corresponding method name. In this way, the string in the input parameter is solved.

There is also the second problem: our specific input parameter, mybatis stipulates that the input parameter type must be consistent with the input parameter type of the interface. In this way, we can set the input parameter type.

3.2 after the input parameter is completed, the return value is also completed. Like the input parameter, the type of the return value must be the same as the interface return value, so that the return value can be transmitted in the proxy class.

After this is done, we can use the proxy object to replace the specific implementation class of our entity layer.

So far, these problems are almost all solved, but there is another problem: the return value of our select method, whether the proxy object uses the selectont method to query the database or the selectlist method? This is related to the type of the return value of our interface (mapper) -- if the mapper method returns a single pojo object (not a collection object), the proxy object internally queries the database through selectOne; if the er method returns a collection object, the proxy object uses selectList to query the database.

Mapper proxy method

After the principles are explained, there is basically nothing to say about it.

First, we need to create an interface.

    

Then write the xml file of the corresponding interface (the part is truncated and the namespace cannot be located ).

Finally, do not forget to load UserMapper. xml in the SqlMapConfig. xml file.

    

Test. It is worth mentioning that our proxy object is obtained through SqlSession. getMapper (interface bytecode file. :

    

So far, the implementation class of the Object layer of mybatis can be omitted. However, this method enhances code coupling. Each interface must correspond to each ing file. However, I still feel that it is better to greatly reduce the amount of code by complying with certain specifications.

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.