MyBatis Basics (ii)----development of original DAO and development of mapper agent

Source: Internet
Author: User

Read Catalogue

    • One: The original DAO development method
    • Two: Mapper Proxy method (requires only mapper interface, equivalent to DAO interface)

To undertake the first MyBatis Foundation (i)

Read a friend, you can certainly see that there is a lot of mybatisservice in the duplicate code, it does not seem very clear, but the first time to write, is to understand the mybatis of the implementation steps, first bitter sweet!

Back to top one: Primitive DAO development method

Summary : 1. Write the DAO Interface and DAO implementation class in the frame price set up in the previous article

2. Inject sqlsessionfactory into the DAO interface implementation class and create sqlsession through Sqlsessionfactory in the method body.

    • DAO interface Class Userdao.java
Package Com.mybatis.dao;import java.util.list;import com.mybatis.entity.user;/** *  * @ClassName: Userdao * @ Description:todo (User Management DAO Interface) * @author warcaft * @date 2015-6-27 pm 10:23:42 *  */public interface Userdao {    /** based on I d Query User Information *    /Public user Finduserbyid (Integer ID);    /** query user information based on user name *    /public list<user>  Finduserbyname (String username);    /** Add User *    /public void Insertuser (user user);    /** Delete User by ID *    /public void DeleteUser (Integer id);    /** update user by ID *    /public void UpdateUser (user user);}
    • DAO Implementation Class Userdaoimpl.java
Package Com.mybatis.dao;import Java.util.list;import Org.apache.ibatis.session.sqlsession;import Org.apache.ibatis.session.sqlsessionfactory;import com.mybatis.entity.user;/** * * @ClassName: UserDaoImpl * @ Description:todo (Implementation class for user management interface) * @author warcaft * @date 2015-6-27 PM 10:29:35 * */public class Userdaoimpl implements user    DAO {private Sqlsessionfactory sqlsessionfactory; You need to inject sqlsessionfactory into the DAO implementation class//By constructing the public Userdaoimpl (Sqlsessionfactory sqlsessionfactory) {THIS.S    Qlsessionfactory = sqlsessionfactory;        } @Override Public User Finduserbyid (Integer id) {sqlsession sqlsession = sqlsessionfactory.opensession ();        User user = Sqlsession.selectone ("Test.finduserbyid", id);        Release resources Sqlsession.close ();    return user; } @Override Public list<user> finduserbyname (String username) {sqlsession sqlsession = Sqlsessionfacto        Ry.opensession (); list<user> list = sqlsession.SelectList ("Test.finduserbyname", username);        Commit Transaction Sqlsession.commit ();        Release resources Sqlsession.close ();    return list;        } @Override public void Insertuser (user user) {sqlsession sqlsession = sqlsessionfactory.opensession ();        Perform the insert Operation Sqlsession.insert ("Test.insertuser", user);        Commit Transaction Sqlsession.commit ();    Release resources Sqlsession.close ();        } @Override public void DeleteUser (Integer id) {sqlsession sqlsession = sqlsessionfactory.opensession ();        Perform insert operation Sqlsession.delete ("Test.deleteuser", id);        Commit Transaction Sqlsession.commit ();    Release resources Sqlsession.close ();        } @Override public void UpdateUser (user user) {sqlsession sqlsession = sqlsessionfactory.opensession ();        Perform the insert Operation Sqlsession.update ("Test.updateuser", user);        Commit Transaction Sqlsession.commit ();    Release resources Sqlsession.close (); }}
    • Junittest Test Userdaoimpltest.java
Package Com.mybatis.dao.test;import Java.io.inputstream;import Java.util.date;import java.util.list;import Org.apache.ibatis.io.resources;import Org.apache.ibatis.session.sqlsessionfactory;import Org.apache.ibatis.session.sqlsessionfactorybuilder;import Org.junit.before;import Org.junit.Test;import Com.mybatis.dao.userdaoimpl;import Com.mybatis.entity.user;public class Userdaoimpltest {private SqlSessionFactory    Sqlsessionfactory;        @Before public void SetUp () throws Exception {String resource = "Sqlmapconfig.xml";        InputStream InputStream = resources.getresourceasstream (Resource);    Sqlsessionfactory = new Sqlsessionfactorybuilder (). Build (InputStream);        } @Test public void Finduserbyidtest () {Userdaoimpl Userdao = new Userdaoimpl (sqlsessionfactory);        User user = Userdao.finduserbyid (1);    SYSTEM.OUT.PRINTLN (user);    } @Test public void Finduserbynametest () {Userdaoimpl Userdao = new Userdaoimpl (sqlsessionfactory);    list<user> list = Userdao.finduserbyname ("small");    SYSTEM.OUT.PRINTLN (list);        } @Test public void Insertusertest () {Userdaoimpl Userdao = new Userdaoimpl (sqlsessionfactory);        User user = new user ();        User.setusername ("Zhang San Fung");        User.setsex ("1");        User.setbirthday (New Date ());        User.setaddress ("Wudangshan");    Userdao.insertuser (user);        } @Test public void Deleteusertest () {Userdaoimpl Userdao = new Userdaoimpl (sqlsessionfactory);    Userdao.deleteuser (8);        } @Test public void Updateusertest () {Userdaoimpl Userdao = new Userdaoimpl (sqlsessionfactory);        User user = new user ();        User.setid (1);        User.setusername ("Wang VI");        User.setsex ("2");        User.setaddress ("Tianjin");        User.setbirthday (New Date ());    Userdao.updateuser (user); }}

The above code can be seen, a little clearer than the contents of the previous Mybatisservice.java class. However, the following problems still exist:

There are a lot of template methods in the 1.dao interface, can we put this code up and reduce our workload

2. Hard-coded ID of statement when calling the Sqlsession method

3. Call Sqlsession the variable passed in, because the Sqlsession method uses generics, even if the variable type passed in the error, in the compilation phase does not error, is not conducive to program development.

So we take these several questions to see Mapper agent development method, whether can solve these problems?

Back to top two: Mapper Proxy method (requires only mapper interface, equivalent to DAO interface)

1. Overview: (1). Write the Xxxmapper.xml mapping file

(2). Writing mapper interfaces requires following some development specifications, MyBatis can automatically generate Mapper interface implementation class proxy objects.

2. Development specification:

(1). namespace equals Mapper interface address in Xxxmapper.xml;

  

(2). The method in the Xxxmapper.java interface is identical to the ID of statement in the Mapper.xml.

(3) The method input parameter in the. Mapper.java interface is consistent with the parametertype specified in the Mapper.xml statement.

(4) The return value type of the method in the. Mapper.java interface is the same as the resulttype specified in mapper.xml statement.

  

  

Summary: The above development specifications are mainly for the following code for unified generation

    Sqlsession sqlsession = Sqlsessionfactory.opensession ();    User user = Sqlsession.selectone ("Test.finduserbyid", id);
......

3.usermapper.java Class Code

View Code

4. Change the original user.xml copy name to Usermapper.xml, just modify this line of code to

<!--namespace namespace, the role is to classify the SQL management, understanding of SQL Isolation    Note: When using Mapper agent development, namespace has a special role, namespace equals the Mapper interface address-- <mapper namespace= "Com.mybatis.mapper.UserMapper" >

5. Load Usermapper.xml in Sqlmapconfig.xml

<!--load the mapping file--    <mappers>        <mapper resource= "Sqlmap/user.xml"/>        <mapper resource= " Mapper/usermapper.xml "/>    </mappers>

6.Junit Test Usermappertest.java

View Code

7. Summary

(1). Proxy object Internal calls SelectOne () and SelectList (): If the Mapper object returns a single Pojo object (not a collection object) inside the proxy object through the SelectOne query database, if the Mapper method returns a collection object, The proxy object queries the database internally through SelectList.

(2). Only one of the method parameters in the Mapper interface can affect system development:

Mapper interface method parameters can only have one, is the system detrimental to maintenance?

Answer: In the system framework, the DAO layer code is common to the business layer. The Mapper interface has only one parameter, which can be used to meet the needs of different business methods using the Pojo of the wrapper type.

MyBatis Basics (ii)----development of original DAO and development of mapper agent

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.