"MyBatis Learning 03" Primitive DAO development method and its disadvantage

Source: Internet
Author: User

The previous blog summarizes the introduction of MyBatis, the next is to develop the DAO method, this blog post mainly summarizes the method of MyBatis DAO development, and finally summarizes the shortcomings of the original DAO development method. DAO development in MyBatis should use the method of Mapper proxy, which will be introduced in the next Article blog post.
The original DAO development idea is relatively simple, writes the DAO interface and the DAO realization class to be able. You need to inject sqlsessionfactory into the DAO implementation class to create sqlsession through Sqlsessionfactory in the method body. Why is it created in the method body? Because sqlsession is thread insecure in mybatis. If created outside of the method as a member variable, a thread-safety issue can be raised. The following summarizes the steps of the original DAO Development (the DAO approach is consistent with the method in the previous section of the starter program):

1. Write Userdao interface
 Public  interface Userdao {    //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;}
2. Write Userdaoimpl implementation class
 Public  class Userdaoimpl implements Userdao {    PrivateSqlsessionfactory sqlsessionfactory;//need to inject sqlsessionfactory into the DAO implementation class, because it is not integrated with spring, this is injected through the constructor function     Public Userdaoimpl(Sqlsessionfactory sqlsessionfactory) { This. sqlsessionfactory = Sqlsessionfactory; }@Override     PublicUserFinduserbyid(intIdthrowsException {sqlsession sqlsession = sqlsessionfactory.opensession (); User user = Sqlsession.selectone ("Test.finduserbyid", id);//Release ResourcesSqlsession.close ();returnUser }@Override     PublicList<user>Finduserbyname(String name)throwsException {sqlsession sqlsession = sqlsessionfactory.opensession (); list<user> list = Sqlsession.selectlist ("Test.finduserbyname", name);//Release ResourcesSqlsession.close ();returnList }@Override     Public void Insertuser(User user)throwsException {sqlsession sqlsession = sqlsessionfactory.opensession (); Sqlsession.insert ("Test.insertuser", user); Sqlsession.commit ();//Execute insert to commit firstSqlsession.close (); }@Override     Public void DeleteUser(intIdthrowsException {sqlsession sqlsession = sqlsessionfactory.opensession (); Sqlsession.delete ("Test.deleteuser", id); Sqlsession.commit ();//Execute insert to commit firstSqlsession.close (); }@Override     Public void UpdateUser(User user)throwsException {sqlsession sqlsession = sqlsessionfactory.opensession (); Sqlsession.delete ("Test.updateuser", user); Sqlsession.commit ();//Execute insert to commit firstSqlsession.close (); }}

From the Userdaoimpl implementation class can be seen, first sqlsessionfactory need to inject, here through the constructor to inject, pass a sqlsessionfactory come in to complete the injection. In addition, sqlsession are created internally within a specific method and do not put sqlsession outside, because within the method, it is independent of each thread and does not cause thread safety issues. As for the internal implementation of each method, it is the same as the introductory program logic in the previous section.

3. Write Unit Test userdaoimpltest
 Public  class userdaoimpltest {    PrivateSqlsessionfactory sqlsessionfactory;@Before     Public void setUp()throwsException {//Create SqlsessionfactoryString 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 {//Create an Userdao objectUserdao Userdao =NewUserdaoimpl (sqlsessionfactory); System.out.println (Userdao.finduserbyid (1)); }}

As can be seen from the JUnit test program, Sqlsessionfactory is initialized before the test method is executed by @before annotations, and then passed directly through the constructor in the test method, which is connected to the Userdaoimpl implementation class above. Then test the add user.

4. Problems with original DAO development

From the above code, it can be obvious that the original DAO development method has the following drawbacks:

  1. There is a large number of duplicated code in the DAO interface implementation class method, which should be extracted from the design point of view.
  2. When the Sqlsession method is called, the ID of the satement is hard-coded, which is similar to "Test.finduserbyid", which is written dead.
  3. Sqlsession method, the required parameter is an object type (generic), that is, if I passed the wrong parameter, compile will not error, execution will be error, not conducive to development.

These are the problems existing in the original DAO development way, after understanding the original DAO development way question, then uses the mapper agent to develop the DAO, can form the sharp contrast. This blog post summarizes this.

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 03" Primitive DAO development method and its disadvantage

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.