The previous post summarizes some of the drawbacks of MyBatis's approach to using primitive DAO, and we certainly won't use it, so how do we develop DAO in MyBatis? As described in title, this blog post mainly summarizes the steps to develop DAO using the Mapper proxy method.
When developing DAO using the Mapper proxy method, programmers only need to do two things:
- Need to write Mapper.xml mapping file
- Need to write Mapper interface (equivalent to DAO interface)
From the work of doing, using MyBatis in the use of Mapper agent to develop DAO will be very convenient, do not need us to write the specific implementation class, only need to write out the interface, but the interface can not be directly used Ah, then how can I produce its implementation class object? This will give an answer below.
The so-called mapper.xml mapping file, the content is actually the same as the previous User.xml file, mainly with the definition of some of the mapping between the user this Pojo related things, the only difference is that the namespace assignment. In the previous User.xml file, we set the namespace to "test", and then, in the Java method call, we call a similar sqlSession.insert("test.insertUser", user);
method to locate the SQL statement that needs to be executed. But in the Mapper.xml mapping file, namespace is set to the address of the mapper interface that we write next, the fully qualified name. Suppose we create a new mapper package and create a new usermapper.xml inside it, as follows:
After defining the Mapper.xml mapping file, the next step is to write the Mapper interface, and the Mapper interface should follow the following four development specifications:
- In Mapper.xml, make namespace equal to the address of the Mapper interface (fully qualified name)
- The method name in the Mapper.java interface is consistent with the statement ID in the Mapper.xml
- The input parameter type of the method in the Mapper.java interface is consistent with the type of parametertype specified in the Mapper.xml statement
- The method return value type in the Mapper.java interface is consistent with the type of resulttype specified in mapper.xml statement
According to these four development specifications, we will complete the Mapper interface: Usermapper.java
//mapper interface, equivalent to DAO interface Public interface usermapper { //query user information by ID PublicUserFinduserbyid(intIdthrowsException;//Fuzzy query based on user name PublicList<user>Finduserbyname(String name)throwsException;//Add user information Public void Insertuser(User user)throwsException;//delete user information Public void DeleteUser(intIdthrowsException;//update user Information Public void UpdateUser(User user)throwsException;}
One thing to note here is the Finduserbyname method, which returns a list containing the user, However, the corresponding Resulttype type in Usermapper.xml is user, it is important to note that in the previous blog post also mentioned, Resulttype specifies the type of a single return result, that is, the type of a record, that is, user, but this finduserbyname is to return a lot of u Ser, so the return value is List<User>
. MyBatis will automatically invoke different methods based on the return value type, as follows:
If the Mapper method returns a single Pojo object (not a collection object), the proxy object queries the database internally through SelectOne
If the Mapper method returns a non-collection object, the proxy object internally queries the database through SelectList
So do not worry about this problem at all, MyBatis has helped us to solve. It's not over yet, and one more step is to not forget to configure the Usermapper.xml in the global configuration file Sqlmapconfig.xml as follows:
So far, the development of DAO steps using the Mapper proxy has basically been done. The following is the beginning of the test program. But there is a question left, that is, the beginning of the article: Now only mapper interface Ah, then how do you produce the object of the implementation class? We see in the test program:
PrivateSqlsessionfactory sqlsessionfactory;@Before //Create Sqlsessionfactory Public void setUp()throwsException {String resource ="Sqlmapconfig.xml";//mybatis configuration file //Get configuration file streamInputStream InputStream = resources.getresourceasstream (Resource);//Create session factory Sqlsessionfactory, stream the profile to pass in the MybaitsSqlsessionfactory =NewSqlsessionfactorybuilder (). Build (InputStream); }@Test Public void Testfinduserbyid()throwsException {sqlsession sqlsession = sqlsessionfactory.opensession ();//Create Usermapper object, MyBatis automatically generate Mapper proxy objectUsermapper usermapper = Sqlsession.getmapper (Usermapper.class); User user = Usermapper.finduserbyid (1); SYSTEM.OUT.PRINTLN (user); }@Test Public void Testfinduserbyname()throwsException {sqlsession sqlsession = sqlsessionfactory.opensession ();//Create Usermapper object, MyBatis automatically generate Mapper proxy objectUsermapper usermapper = Sqlsession.getmapper (Usermapper.class); list<user> list = Usermapper.finduserbyname ("Nu Shen WU"); Sqlsession.close (); SYSTEM.OUT.PRINTLN (list); }}
The original sqlsession can automatically create a proxy object for the Mapper interface! We just need to pass the byte-code object of the Mapper interface class that we just wrote to the Getmapper method, we can get a proxy object for that interface, and then we could use this proxy object to manipulate the specific method in the interface.
Here, the use of Mapper Proxy development of DAO is summed up, but there is a small detail, because the Mapper interface method parameters to be based on the parametertype in the mapping file to specify, and ParameterType only one, So all the methods in the Mapper interface have only one parameter! So what if we're going to pass in two or more parameters? This has no way, want to pass multiple parameters or dead this heart, but can solve this problem, is to enhance the incoming object, let the passed in the object contains the parameters we need. This is a minor disadvantage, but it will not affect our development.
Related reading: http://blog.csdn.net/column/details/smybatis.html
To the source of this article: https://github.com/eson15/MyBatis_Study/releases
Full Learning note Source: Https://github.com/eson15/MyBatis_Study
-Willing to share and progress together!
-More articles please see: http://blog.csdn.net/eson_15
"MyBatis Learning 04" Mapper Agent Method development DAO