MYBATIS10 implementing class proxy object development

Source: Internet
Author: User

== Sqlsession.selectone ("test.finduserbyid", id), bad encapsulation, Because the Test.finduserbyid is variable, it is difficult to generate automatically by proxy.
 1   Mapper.xml (usermapper.xml) namespace specifies the fully qualified name of the Mapper interface  <mapper namespace  =  " cn.itcast.mybatis.mapper.usermapper   " > This step purpose: through Mapper.xml and Mapper.java ( Usermapper.java) are associated.  2  , The ID of statement in Mapper.xml is Mapper.java (Usermapper.java) in the method name  3  , Mapper.xml statement in ParameterType and Mapper.java (Usermapper.java), the method input parameter type is consistent  4 , Mapper.xml statement in Resulttype and Mapper.java (Usermapper.java) have the same type of method return value. 
1.1 . 1mapper.xml (usermapper.xml Map file) Mapper Map file naming recommendation: Table name Mapper.xmlnamespace specified as the fully qualified name of the Mapper interface

The Mapper (Usermapper.java) interface MyBatis presents the Mapper interface, which is equivalent to the DAO interface. Mapper the name of the interface Recommendation: Table name Mapper

1.1. 1 Loading Mapper.xml in Sqlmapconfig.xml

The Mapper interface returns a single object and a collection object regardless of whether the query records are individual or multiple, resulttype definitions are consistent in statement, and are the Pojo types of the one-record mappings. The Mapper interface method returns a value, if it is a single object returned, the return value type is the Pojo type, the generated proxy object gets the record internally through SelectOne, and if the return value type is a collection object, the generated proxy object is retrieved internally through SelectList.

Test:

    //Session Factory    Privatesqlsessionfactory sqlsessionfactory; //Create a factory@Before Public voidinit () throws IOException {//configuration file (sqlmapconfig.xml)String resource ="Sqlmapconfig.xml"; //loading the configuration file into the input streamInputStream InputStream =Resources.getresourceasstream (Resource); //To create a session factorySqlsessionfactory =NewSqlsessionfactorybuilder (). Build (InputStream); } @Test Public voidTestfinduserbyid () throws Exception {sqlsession sqlsession=sqlsessionfactory.opensession (); //Create a proxy objectUsermapper usermapper = Sqlsession.getmapper (usermapper.class); User User= Usermapper.finduserbyid (1); System. out. println (user); } @Test Public voidTestfinduserbyusername () throws Exception {sqlsession sqlsession=sqlsessionfactory.opensession (); //Create a proxy objectUsermapper usermapper = Sqlsession.getmapper (usermapper.class); List<User> list = Usermapper.finduserbyname ("Xiao Ming"); System. out. println (list); }

MYBATIS10 implementing class proxy object development

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.