MyBatis Implement mapper Dynamic proxy mode

Source: Internet
Author: User

First, the realization principle

Mapper interface development method only requires programmers to write the Mapper interface (equivalent to DAO interface), the MyBatis framework creates an interface based on the interface definition of the dynamic proxy object, the proxy object's method body with the upper DAO interface implementation class method.


Mapper interface development is subject to the following specifications:
1. The namespace in the Mapper.xml file is the same as the classpath of the Mapper interface.
2. The Mapper interface method name is the same as the ID of each statement defined in the Mapper.xml
3. The input parameter type of the Mapper interface method is the same as the type of parametertype for each SQL defined in Mapper.xml
4. The output parameter type of the Mapper interface method is the same as the type of resulttype for each SQL defined in Mapper.xml

Second, mapper.xml mapping file

To define the mapper mapping file Usermapper.xml (content same as Users.xml), you need to modify the namespace value to the Usermapper interface path. Place the Usermapper.xml under the Mapper directory under Classpath.

[HTML]View PlainCopyprint?
  1. <? XML version= "1.0" encoding="UTF-8" ?>
  2. <! DOCTYPE Mapper
  3. Public "-//mybatis.org//dtd Mapper 3.0//en"
  4. "Http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="Cn.itcast.mybatis.mapper.UserMapper">
  6. <!--get user information by ID--
  7. <Select id= "Finduserbyid" parametertype="int" resulttype=" Cn.itcast.mybatis.po.User ">
  8. SELECT * from user where id = #{id}
  9. </Select>
  10. <!--custom criteria query user list-
  11. <Select id= "finduserbyusername" parametertype="java.lang.String "
  12. resulttype="Cn.itcast.mybatis.po.User">
  13. SELECT * from user where username like '%${value}% '
  14. </Select>
  15. <!--adding users--
  16. <insert id="Insertuser" parametertype="Cn.itcast.mybatis.po.User">
  17. <selectkey keyproperty="id" order="after" resulttype="Java.lang.Integer" >
  18. Select LAST_INSERT_ID ()
  19. </selectkey>
  20. Insert into User (username,birthday,sex,address)
  21. VALUES (#{username},#{birthday},#{sex},#{address})
  22. </Insert>
  23. </mapper>

Third, Mapper.java (interface file)

[Java]View PlainCopyprint?
  1. /**
  2. * User Management Mapper
  3. */
  4. Public interface Usermapper {
  5. //query user information based on user ID
  6. Public User Finduserbyid (int id) throws Exception;
  7. //query user list
  8. Public list<user> Finduserbyusername (String username) throws Exception;
  9. //Add user information
  10. public void Insertuser (user user)throws Exception;
  11. }


The interface definition has the following characteristics:
1. The ID of the Mapper interface method name and the statement defined in the Mapper.xml are the same
2. The input parameter type of the Mapper interface method is the same as the ParameterType type of the statement defined in the Mapper.xml
3. The output parameter type of the Mapper interface method is the same as the Resulttype type of the statement defined in the Mapper.xml

Iv. Loading Usermapper.xml files

To modify the Sqlmapconfig.xml file:

[HTML]View PlainCopyprint?
    1. <!--load a mapping file--
    2. <mappers>
    3. <mapper resource="Mapper/usermapper.xml"/>
    4. </mappers>

V. Testing

[Java]View PlainCopy print?
  1. Public class Usermappertest extends TestCase {
  2. private Sqlsessionfactory sqlsessionfactory;
  3. protected void SetUp () throws Exception {
  4. //mybatis configuration file
  5. String resource = "Sqlmapconfig.xml";
  6. InputStream InputStream = resources.getresourceasstream (Resource);
  7. //Use Sqlsessionfactorybuilder to create Sessionfactory
  8. Sqlsessionfactory = new Sqlsessionfactorybuilder (). Build (InputStream);
  9. }
  10. Public void Testfinduserbyid () throws Exception {
  11. //Get session
  12. sqlsession session = Sqlsessionfactory.opensession ();
  13. //Get proxy object for mapper interface
  14. Usermapper usermapper = Session.getmapper (usermapper.   Class);
  15. //Invoke proxy object method
  16. User user = Usermapper.finduserbyid (1);
  17. SYSTEM.OUT.PRINTLN (user);
  18. //Close Session
  19. Session.close ();
  20. }
  21. @Test
  22. public void Testfinduserbyusername () throws Exception {
  23. Sqlsession sqlsession = Sqlsessionfactory.opensession ();
  24. Usermapper usermapper = Sqlsession.getmapper (usermapper.   Class);
  25. list<user> list = Usermapper.finduserbyusername ("Zhang");
  26. System.out.println (List.size ());
  27. }
  28. Public void Testinsertuser () throws Exception {
  29. //Get session
  30. sqlsession session = Sqlsessionfactory.opensession ();
  31. //Get proxy object for mapper interface
  32. Usermapper usermapper = Session.getmapper (usermapper.   Class);
  33. //data to be added
  34. User user = new user ();
  35. User.setusername ("Zhang San");
  36. User.setbirthday (new Date ());
  37. User.setsex ("1");
  38. User.setaddress ("Beijing");
  39. //Add user via Mapper interface
  40. Usermapper.insertuser (user);
  41. //Submit
  42. Session.commit ();
  43. //Close Session
  44. Session.close ();
  45. }
  46. }

Vi. Summary

SelectOne and SelectList
The dynamic proxy object calls Sqlsession.selectone () and sqlsession.selectlist () are determined by the return value of the Mapper interface method, and the SelectList method is called if a list is returned. If a single object is returned, the SelectOne method is called.


Namespace
MyBatis official recommend the use of Mapper proxy method to develop Mapper interface, programmers do not have to write Mapper interface implementation class, when using the Mapper proxy method, input parameters can use Pojo wrapper object or Map object, to ensure the universality of DAO.

MyBatis Implement mapper Dynamic proxy mode

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.