mybatis-and spring integration in layman's

Source: Internet
Author: User

mybatis-and Spring integration in layman 's

Directory (?) [+]

There are many limitations to using mybatis alone (such as the inability to implement transactions that span multiple sessions), and many business systems are used to manage transactions using spring, so mybatis is best integrated with spring.

Predecessor Requirements version Requirements

Project

Version

Description

MyBatis

3.0 and above

Https://github.com/mybatis/mybatis-3/releases

Spring

3.0 and above

http://projects.spring.io/spring-framework/

Mybatis-spring

1.0 and above

Https://github.com/mybatis/spring/releases

Spring Transaction Configuration [HTML]View Plaincopy
  1. <!--automatically scan business packs--
  2. <context:component-scan base-package="Com.xxx.service" />
  3. <!--data Source-
  4. <jee:jndi-lookup id="Jndidatasource" jndi-name="Java:comp/env/jdbc/datasource" / >
  5. <!--configuration Transactions--
  6. <Bean id="Txmanager"
  7. class="Org.springframework.jdbc.datasource.DataSourceTransactionManager">
  8. <property name= "dataSource" ref="Jndidatasource" />
  9. </Bean>
  10. <!--configuring annotation-based things AOP--
  11. <tx:annotation-driven transaction-manager="Txmanager" proxy-target-class="true"/>

Single integration [HTML]View Plaincopy
  1. <!--integrated MyBatis--
  2. <Bean id= "sqlsessionfactory" class="Org.mybatis.spring.SqlSessionFactoryBean">
  3. <property name= "dataSource" ref="Jndidatasource" />
  4. <property name= "configlocation" value="Classpath:/mybatis/mybatis-config.xml" / >
  5. <!--Auto-Configure aliases --
  6. <property name= "typealiasespackage" value="com.xxx.dto" />
  7. </Bean>
  8. <!--to create a DAO bean (simply provide an interface that does not require an implementation class)--
  9. <Bean id= "Userdao" class="Org.mybatis.spring.mapper.MapperFactoryBean">
  10. <property name= "mapperinterface" value="Com.xxx.dao.UserDao" />
  11. <property name= "sqlsessionfactory" ref="sqlsessionfactory" />
  12. </Bean>

We need to understand not only how to use it, but also why we should use it.

Sqlsessionfactorybean is a factory bean that functions as a parsing configuration (data source, alias, and so on).

Mapperfactorybean is a factory bean, in a spring container, the factory bean has a special purpose, and when spring injects the factory bean into another bean, Instead of injecting the factory bean itself, it calls the Bean's GetObject method. Let's take a look at what this getobjec method did:

[Java]View Plaincopy
    1. Public T GetObject () throws Exception {
    2. return Getsqlsession (). Getmapper (this.mapperinterface);
    3. }

See here people should be very clear, this method and we used to use MyBatis in the same way, is to get a Sqlsession object, and then get the Mapper object from Sqlsession (again, the Mapper is a proxy object, It proxies the Mapperinterface interface, which is the user-supplied DAO interface. Naturally, the final injection into the business layer is the Mapper object.

The actual project is generally more than one DAO, if you have more than one DAO, then configure it according to the above configuration.

How to use Bulk update

The previous section describes how to inject a mapper object into the business layer, Mapper's behavior depends on the configuration, and MyBatis uses a single update by default (that is, executortype defaults to simple rather than batch). Of course we can modify the default behavior by modifying the MyBatis configuration file, but if we just want to use a batch update for one or several mapper, it's not going to work. This is when we need to use template technology:

[HTML]View Plaincopy
  1. <!--customize MyBatis behavior through templates--
  2. Lt;bean id="sqlsessiontemplatesimple" class="Org.mybatis.spring.SqlSessionTemplate">
  3. <constructor-arg index="0" ref="sqlsessionfactory" />
  4. <!--updates in a single mode--
  5. <constructor-arg index="1" value="simple"/>
  6. </Bean>
  7. <!--customize MyBatis behavior through templates--
  8. Lt;bean id="Sqlsessiontemplatebatch" class="Org.mybatis.spring.SqlSessionTemplate"> /c15>
  9. <constructor-arg index="0" ref="sqlsessionfactory" />
  10. <!--update in batch mode--
  11. <constructor-arg index="1" value="BATCH"/>
  12. </Bean>

Here I have defined two template objects, one using a single update, one using batch update. With the template we can change the way mapper behaves:

[HTML]View Plaincopy
  1. <Bean id= "Userdao" class="Org.mybatis.spring.mapper.MapperFactoryBean">
  2. <property name= "mapperinterface" value="Com.xxx.dao.UserDao" />
  3. <property name= "sqlsessiontemplate" ref="Sqlsessiontemplatebatch" />
  4. </Bean>

Unlike the mapper configuration in the previous section, there is no need to configure the Sqlsessionfactory property, just configure sqlsessiontemplate( The Sqlsessionfactory property is already configured in the template).

Simplifies mapper configuration with automatic scanning

As can be seen in the previous chapters, our DAO needs a configuration in the configuration file, if there are many DAO words, the configuration file will be very large, so the management will be more painful. Fortunately, the MyBatis team was aware of this, and they took advantage of spring's automated scanning capabilities to encapsulate a tool class that automatically scans DAO, so that we can use this feature to simplify configuration:

[HTML]View Plaincopy
  1. <!--create mapper beans with automatic scanning (single update mode)--
  2. <Bean class="Org.mybatis.spring.mapper.MapperScannerConfigurer">
  3. <property name= "basepackage" value="Com.xxx.dao" />
  4. <property name= "sqlsessiontemplatebeanname" value="sqlsessiontemplatesimple" />
  5. <property name= "markerinterface" value="Com.xxx.dao.SimpleDao" />
  6. </Bean>
  7. <!--create mapper beans with automatic scanning (batch update mode)--
  8. <Bean class="Org.mybatis.spring.mapper.MapperScannerConfigurer">
  9. <property name= "basepackage" value="Com.xxx.dao" />
  10. <property name= "sqlsessiontemplatebeanname" value="Sqlsessiontemplatebatch" />
  11. <property name= "markerinterface" value="Com.xxx.dao.BatchDao" />
  12. </Bean>

Mapperscannerconfigurer itself involves the spring technology I will not talk about, interested in and the spring principle of more understanding can go to see its source. Let's take a look at its three properties:

Basepackage: Scanner starts scanning the base package name, supports nested scan;

Sqlsessiontemplatebeanname: The name of the template Bean mentioned earlier;

Markerinterface: The interface-based filter, which implements the DAO of the interface, is scanned by the scanner, and the basepackage is the same as the function.

In addition to using interface filtering, you can also use annotation filtering:

[HTML]View Plaincopy
  1. <!--create mapper beans with automatic scanning (batch update mode)--
  2. <Bean class="Org.mybatis.spring.mapper.MapperScannerConfigurer">
  3. <property name= "basepackage" value="Com.xxx.dao" />
  4. <property name= "sqlsessiontemplatebeanname" value="Sqlsessiontemplatebatch" />
  5. <property name= "annotationclass" value="com.xxx.dao.BatchAnnotation" />
  6. </Bean>

Annotationclass : The DAO that is configured with this annotation is scanned by the scanner, with the Basepackage function.

It is important to note that two filter conditions can only be used with one.

mybatis-and spring integration in layman's

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.