Spring MVC 3.0.5+spring 3.0.5+mybatis3.0.4 Full Annotated Example (iii)

Source: Internet
Author: User

In the first two chapters I gave you a detailed description of how to build the MAVEN environment, the process structure of spring MVC, the differences between spring MVC and Struts2, and the analysis of some of the configuration files in the example.    In this chapter, I will explain the hierarchy of the examples, as well as some brief descriptions of MyBatis. This article does not elaborate on the mybatis, the elephant still assumes that the friend who reads this article has the most basic understanding to MyBatis (Ibatis), only then can understand the content of this article better. For MyBatis Please see its official documentation and other references, this article is not discussed in detail.

First, Engineering Structure Diagram

Above this is a typical MAVEN project structure, using the local warehouse to manage the dependencies of the jar package, the use of plug-in packaging to compile the release is very convenient, let us free from the traditional development method, we all quickly use MAVEN to build the project!

Second, Mapping Files

Using MyBatis for persistence, you need to set up a mapping file, in general, each table corresponds to an entity object and a mapper mapping file. In MyBatis, there is no complex relationship like Hibernate, so every entity class is actually a very common Pojo class. While the mapping file is SQL statements, here is the code snippet for Rolemapper.xml in the example.

     So, how does MyBatis map database fields to Pojo objects? This is the result or structure set that will be reached within it, mapped with our defined Pojo object properties, the rule is that the first letter of the property is lowercase, the camel is named, and the field is the word and the word, underlined. For example: The database has a USER_NAME field, then the corresponding property should be username. If the field name is not underlined, the attribute can be defined directly as the word. For example, the Name field, whose property is name.

     third, based on namespace interface and common Differences in span lang= "en-US" >dao
     lang= "en-us" from mybatis3.0, lang= >mapper "en-us" to id attribute value in the mapping file, MyBatis will automatically bind and execute the corresponding sql statement. This interface implementation, the need for each mapper to create an interface, if the system is bigger, maintenance of these classes will be more troublesome, the elephant personal preference to the basic service of dao implementation class, such as the example of the Span lang= "en-us" >mybatisdao.
     IV, Mybatisdao
     persisted operation base class, sqlsessiondaosupport is Mybatis-spring plug-in, used to get sql session connection, perform database operations, I have defined a few common methods.

About the mybatis-spring plug-in I briefly introduce that the Spring 3.x release does not support the excellent SQL framework of MyBatis, although there are already such requests in its list of issues, but until the current release of the 3.0.5 version, the issue has not been addressed. But this is hard to not pour out the vast number of fans and contributors in the open source world, mybatis-spring is born in such circumstances. It is an open source project by MyBatis community enthusiasts to integrate the configuration of Spring 3.0 with MyBatis 3.0. The plugin needs to run in JDK 5.0 or later.


Five, Service

InSSM3 This example, did you find that there is no DAO defined for each entity , but a unified call to Mybatisdao as the storage service interface. Another major difference from using Hibernate is that you cannot pass an object parameter directly, and you must also write the key value, which is the namespace plus ID value.

VI. Entity
the MyBatis entity class is a simple Pojo object, just used to relate the mapping to the table field, note that the mapping I'm talking about here is not a binding relationship like Hibernate, only one object that holds the data.

Seven, test When the above steps are completed, the next step is to test whether our business interface is working properly, write a test class, and for the sake of simplicity, the elephant is testing the RoleService and UserService similar.
Before writing the test case, you need to add a srping test dependency in the Pom file, which is an elephant's omission in the first two chapters, please understand.

Then write the test class, run the test, pass!


To this, the content of this chapter is finished, if there is no understanding of the annotations place, please look at my previous SSH2 series of three, as for MyBatis, it is too much content, if not clear also please look at the use of the guide.    In the next chapter I will cover the web layer and use the annotations of spring MVC to implement the controller functionality. This is the original pineapple elephant, if you want to reprint please indicate the source. Http://www.blogjava.net/bolo/archive/2011/08/04/352845.html

Spring MVC 3.0.5+spring 3.0.5+mybatis3.0.4 Full Annotation Example (iii)

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.