Spring integration mybatis

Source: Internet
Author: User

 

In the previous article, crab has explained the most classic configuration of spring annotations. Next, we will integrate mybatis, so that the entire project will be relatively complete.


For the source code of this instance, you can go 《Spring MVC annotation examples and instructions.

 

If you need to reprint, please indicate the original source: http://www.itxxz.com/a/kuangjia/2014/0531/5.html

First, find the main configuration file applicationContext. xml of spring, and then perform the following Configuration:



There are two purposes to do this: one is that the entity class corresponding to qlSessionFactory will be directly initialized at project startup, and other related classes do not need to be inherited in the dao layer.
The second is to load the total configuration file (mybatis-config.xml) of mybatis, the configuration content is as follows:



Here, we separate the SQL statement from the returned result set, because if the module has many functions and many tables are designed, writing all the content to a configuration file will inevitably look bloated, especially when it is used for post-maintenance or other personnel take over development, it will cause a lot of trouble.

Then open the SysUserMapper. xml file. The configuration content is as follows:


 
The yellow annotation is the mapper class corresponding to the file, that is, the dao layer, and the method in the dao interface must correspond to the method id in the file, otherwise, an error is reported during project initialization. If you still don't know, you can go to spring MVC annotation mode's classic implementation to see the specific configuration. The address is as follows.


Another is the namespace (annotation part) + id format used by resultMap. The advantage of this method is that it facilitates all the resultMap files under the module, no matter whether you are looking for it in the future or as a programming specification, it is an error-prone option, as long as it is clear.

Finally, let's take a look at the configuration of resultMap. xml.



So far, spring's integration of mybatis configuration has ended. Do you think it is very simple, especially the process of self-built from the beginning to the end of the successful operation? Is it a sense of accomplishment. If it is unclear in some places, you can leave a message below the article. The crab will take time to answer the question. If it is not running, you can check the configuration in the source code.

For SQL statements written in the mybatis file, there are two more crabs to mention:

1. Do not add ";" at the end of SQL,

Many developers who are used to writing SQL statements will add an terminator at the end, indicating that the statement has ended, while the mybatis has already been added by themselves, then, adding mybatis will make you think that you are too small to look at it, isn't it?

2. CDATA zone applications

Many people cannot understand why a CDATA zone is added for query judgment in mysql? Let's take a look at the figure.



Are you clear? The crab just wrote a judgment, so there is no complicated syntax for a simple judgment. How can I report an error?
Let's first look at the reported error:




Obviously, our minor account is not listed as invalid characters by mybatis! Why?

When writing SQL statements, the SQL controller or tool treats SQL as a pure statement, while mybtais does not. It is a powerful orm framework that encapsulates many syntaxes to make it easy to use, provided that it is used correctly. It considers that for the keywords or characters in SQL do not conflict with their own parser, it uses the common CDATA zone in xml files to set, as long as it is written in, it will be parsed as pure SQL. So, when we make a mistake, don't worry, and see if it violates the rules of mybatis.

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.