Spring MVC 3.0.5 + spring 3.0.5 + mybatis3.0.4 detailed description of all annotation instances (III)

Source: Internet
Author: User
The first two chapters detail how to build the maven environment, spring
The process structure of MVC, the differences between spring MVC and struts2, and the analysis of some configuration files in the example. In this chapter, I will describe the sample hierarchy and give a brief introduction to mybatis. This article will not elaborate on mybatis. The elephant still assumes that the friends who read this article have the most basic understanding of mybatis (ibatis). Only in this way can we better understand the content of this article. For more information about mybatis, see its official documentation and other references.

I,Engineering Structure Diagram

The above is a typical Maven project structure. It is very convenient to use a local repository to manage the dependencies of jar packages and use plug-ins for packaging, compilation, and release. This frees us from the traditional development methods, you can use Maven to build a project!

II,Ing File

To use mybatis for persistence, you need to set a ing file. Generally, each table corresponds to an object and a Mapper ing file. Mybatis does not have a complex association like hibernate. Therefore, every entity class is actually a very common pojo class. All the ing files are SQL statements. The following is the code snippet of rolemapper. XML in the example.

So, how does mybatis map database fields to pojo objects? This is inside the pojo object, which maps the obtained result or structure set to the attributes of the defined pojo object. The rule is that the first letter of the property is lowercase and the camper naming method is used, fields are connected by underscores between words. For example, if the database has a user_name field, the corresponding attribute should be username. If the field name is not underlined, the attribute can be defined as this word. For example, for the name field, its attribute is name.

III,Based onDifferences between the namespace interface and public Dao
From mybatis3.0, a new feature is added for the namespace attribute in mapper: You can specify a specific interface as a persistence operation class and define the same method as the ID attribute value in the ing file in the interface, mybatis automatically binds and executes corresponding SQL statements. To implement this interface, you need to create an interface for each Mapper. if the system is too large, it will be difficult to maintain these classes. elephants tend to implement the DAO class based on basic services, such as mybatisdao In the example.
IV,Mybatisdao
Persistence operation base class. sqlsessiondaosupport is encapsulated in the mybatis-spring plug-in for obtaining SQL
Session connection and database operations. I have defined several common methods. For a brief introduction to the mybatis-spring plug-in, spring
3. the release of X does not support mybatis, an excellent SQL framework. Although such a request already exists in its problem list, it has been released for version 3.0.5 until now, have not solved this problem. However, it is difficult to find out that many fans and contributors in the Open Source world are born under such circumstances as mybatis-spring. It is an open-source project completed by fans of the mybatis community for spring
3.0 integration with the configuration of mybatis 3.0. This plug-in needs to run in JDK
5.0 or later.


V,ServiceIn the ssm3 example, have you found that no Dao is defined for each entity, but mybatisdao is called as the storage service interface in a unified manner. In addition, the main difference with Hibernate is that an object parameter cannot be directly transmitted, and the key value must be written, which is the namespace plus the id value.

 VI,Entity
The object class of mybatis is a simple pojo object. It is only used to associate and map table fields. Please note that the ing I mentioned here is not a binding relationship like hibernate, only one type of data storage object.

VII,TestAfter the above steps are completed, we need to test whether our business interface is normal and write a test class. for the purpose, for simplicity, elephant is testing the roleservice, And the userservice is similar.

Before writing test cases, you need to add a srping test dependency to the POM file. This is what elephants neglected in the previous two chapters. Thank you for your understanding.


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

At this point, the content of this chapter is finished. If you have any questions about annotations, please refer to my previous SSH2 Series 3. As for mybatis, there are too many contents, if you do not know, please take a look at the user guide. In the next chapter, I will introduce the web layer and use spring
MVC annotations to implement the controller function.

This article is original for pineapple elephants. If you want to reprint it, please indicate the source. Http: // W

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.