MyBatis and spring3.1 Integration

Source: Internet
Author: User

Because MyBatis has not yet released a formal version of Spring3, spring does not incorporate the latest mybatis. But the community has developed a middleware.

Required JAR Package

Mybatis-3.0.6.jar

Mybatis-spring-1.0.2.jar

Points:

1. Configuring the MyBatis factory class in spring

2. Manipulate data in the DAO layer using spring-injected tool beans

When consolidating, there are four ways to use MyBatis for data processing.

Spring must be configured in the.

Include the following in the spring configuration file

XML code
  1. <!--mybatis Configuration--
  2. <Bean id= "sqlsessionfactory" class="Org.mybatis.spring.SqlSessionFactoryBean">
  3. <property name= "dataSource" ref="C3p0datasource" />
  4. <property name= "configlocation" value="/web-inf/config/db/mybatisconfiguration.xml" />
  5. <property name= "mapperlocations" value="/web-inf/config/db/*mapper.xml" />
  6. <property name="typealiasespackage" value="${mybatis.alias.basepackage}" />
  7. </Bean>

1.SqlSessionFactoryBean (required)

is the processing class required by the middleware

2.dataSource (required)

Data source references in spring

3.configLocation (optional)

MyBatis its own configuration file, which is typically used to declare aliases

4.mapperLocation (optional)

mapping files for MyBatis

5.typeAliasesPackage (optional)

To map the package path of a class, if this is the case, you do not have to declare it in Configlocation

Four ways to use MyBatis for data processing (sqlsessiontemplate/sqlsessiondaosupport/mapperfactorybean/mapperscannerconfigurer)

Features in different ways

    1. Sqlsessiontemplate this needs to write a configuration file, inject sqlsession into the implementation class, and then use Sqlsession, which is fine grained control
    2. Sqlsessiondaosupport this only needs to inherit the special class in the implementation class, you can use the sqlsession
    3. Mapperfactorybean this to write the configuration file, the corresponding interface in the configuration file reference, no need to write implementation class
    4. Mapperscannerconfigurer this to write the configuration file, as long as the interface is located in the package, will automatically introduce the interface in the package, no need to write implementation class
    • Sqlsessiontemplate
  1. Configuration file to add a new
    Java code
    1. <bean id="sqlsession" class="Org.mybatis.spring.SqlSessionTemplate" >
    2. <constructor-arg index="0" ref="sqlsessionfactory"/>
    3. <constructor-arg index="1" value="batch"/><!---if you want to do a bulk operation, you can add this property --
    4. </bean>

  2. Injection sqlsession ()
    Java code
    1. @Reasource //annotation injection using Spring3
    2. Private sqlsession sqlsession;

  3. Use Sqlsession to operate
    Java code
    1. Public User GetUser (String userId) {
    2. return (User) sqlsession.selectone ("Org.mybatis.spring.sample.mapper.UserMapper.getUser", userId);
    3. }
    • Sqlsessiondaosupport (Sqlsessionfactory will be automatically assembled by spring, no manual injection required)
    1. Inheriting the Sqlsessiondaosupport class
      Java code
      1. Public class Userdaoimpl extends Sqlsessiondaosupport implements Userdao {
      2. }

    2. Using the Getsqlsession () method to take sqlsession for data processing
      Java code
      1. Public User GetUser (String userId) {
      2. return (User) getsqlsession (). SelectOne ("Org.mybatis.spring.sample.mapper.UserMapper.getUser", userId);
      3. }
    • Mapperfactorybean
  1. Write a configuration file that introduces each DAO interface
    XML code
    1. <Bean id= "usermapper" class="Org.mybatis.spring.mapper.MapperFactoryBean">
    2. <property name= "mapperinterface" value=" Org.mybatis.spring.sample.mapper.UserMapper " />
    3. <property name= "sqlsessionfactory" ref="sqlsessionfactory" />
    4. </Bean>
  2. Operations can be injected directly into the DAO's interface at the business layer
    • Mapperscannerconfigurer
    1. Write configuration file, the configuration package name automatically introduces all the interfaces in the package
      XML code
      1. <Bean class="Org.mybatis.spring.mapper.MapperScannerConfigurer">
      2. <property name= "basepackage" value="Org.mybatis.spring.sample.mapper" />
      3. </Bean>
    2. In the business layer can be injected directly into the DAO interface operation, injected with the interface name, its first letter lowercase
    3. Note: If there is another implementation class, the name provided is the interface name, and the first letter is lowercase, a conflict error will occur at startup

MyBatis and spring3.1 Integration

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.