Mybatis User Guide-Part 2

Source: Internet
Author: User

P8


Tips about namespaces

The namespace is optional in the previous example. It is also confusing and beneficial. The namespace is needed now, and its purpose is not only to isolate statements with long fully qualified names.
As you can see, namespaces make binding interfaces possible. Even if you don't think you will use them now, you should still follow these practices to prevent you from changing your mind in the future. Using a namespace at a time and placing it in a proper Java package namespace will make your code clean and tidy and improve the availability of mybatis for a long time.

Name resolution: to reduce the input volume, mybatis uses the following name resolution scheme for naming configuration elements, including statements, result ing, and caching:
The fully qualified name (for example, "com. mypackage. mymapper. selectallthings") will be searched directly. If yes, it will be used.
Short names (such as "selectallthings") are directly used to reference non-Ambiguous items. However, if there are more than two short names (such as "com. foo. selectallthings, Com. bar. selectallthings), the program will throw an error, telling you that the short name is ambiguous, you must use a fully qualified name.

There is another confusing place for er classes such as blogmapper. Their ing statements do not need XML at all. On the contrary, they can use Java annotations. For example, the above XML can be replaced with the following content:
Package Org. mybatis. example; <br/> Public interface blogmapper {<br/> @ select ("select * from blog where ID =#{ ID }") <br/> blog selectblog (int id); <br/>}< br/>
P9:
For simple statements, this annotation is very concise, but Java annotation is not only functional but also messy for complex statements. If you need to handle complex situations, it is best to use XML ing statements.
Which method is correct to you and the ing statement is defined as consistent is important to you, depending on you and your project team. That is to say, never lock yourself in one way. You can easily migrate XML ing statements to annotations, and vice versa.

Scope and lifecycle
Understanding the various scopes and lifecycle classes we have discussed is very important. Incorrect use may cause serious concurrency problems.

Sqlsessionfactorybuilder
This class can be initialized, used, and discarded. Once you create sqlsessionfactory, there is no need to save sqlsessionfactorybuilder. Therefore, the best scope of its instance is the method scope (such as a local method variable ). You can use sqlsessionfactorybuilder to create multiple sqlsessionfactory instances, but it is recommended that you do not save sqlsessionfactorybuilder instances to ensure that all XML parsing resources are available for more important purposes.

Sqlsessionfactory
Once created, sqlsessionfactory should exist throughout the execution period of the application. There is little or no reason to destroy or recreate it. The best practice is to avoid rebuilding sqlsessionfactory multiple times during a program running period, which is "disgusting ". Therefore, the scope of sqlsessionfactory is preferably the application scope. There are several ways to do this. The simplest one is the singleton or static Singleton mode. However, none of them are widely accepted as best practices. On the contrary, you may prefer to study dependency injection containers, such as Google guide or spring. This framework allows you to create a provider to manage the lifecycle of a sqlsessionfactory Singleton.

Sqlsession
Each thread should have its own sqlsession instance. Sqlsession instances are not shared and thread-safe. Therefore, the best scope is request scope or method scope. Never keep the reference of the sqlsession instance in the static field of the class or even the instance field. Never reference the sqlsession instance in any managed domain, such as the http session of the servlet framework. If you are using any web framework, consider using a scope similar to HTTP Request (Translator's note: the original article is consider the sqlsession to follow a similar scope to that of an HTTP request, actually, there are some clouds, but the basic meaning should be the same as I said ). In other words, when an HTTP request is received, a sqlsession can be opened. When a response is returned, the sqlsession is closed. It is important to disable sqlsession. Ensure that it is disabled in the finally statement block. The following is a standard mode to ensure that sqlsession is disabled:
Sqlsession session = sqlsessionfactory. opensession (); <br/> try {<br/> // write your business logic <br/>} finally {<br/> session. close (); <br/>}< br/> 

 
P10:
If you keep using this function, you can ensure that all database resources are properly closed (assuming that you have never used your connection to tell mybatis that you want to manage the connection resources on your own ).

Er instance
Mappings are a set of interfaces used to bind ing statements. Instances of the ER interface can be obtained from sqlsession. In this way, theoretically, its instance scope is the same as the sqlsession that sends the request. That is to say, they should be requested in the method they should be used and then discarded. You do not need to explicitly close them. You do not need to keep these instances in a request. This is similar to sqlsession. You will find that if you manage too many resources at this level, things will soon become uncontrollable. Keep simple, and keep the ER in the method scope. The following example illustrates this.
Sqlsession session = sqlsessionfactory. opensession (); <br/> try {<br/> blogmapper mapper = session. getmapper (blogmapper. class); <br/> // do work <br/>} finally {<br/> session. close (); <br/>}< br/>

 
Er xml configuration
The mybatis configuration file contains powerful settings and attributes. The following is the advanced structure of the XML document:

· Configuration

O Properties

O settings

O typealiases

O typehandlers

O objectfactory

O plugins

O Environments

§ Environment

· Transactionmanager

· Datasource

O mappers

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.