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
- <!--mybatis Configuration--
- <Bean id= "sqlsessionfactory" class="Org.mybatis.spring.SqlSessionFactoryBean">
- <property name= "dataSource" ref="C3p0datasource" />
- <property name= "configlocation" value="/web-inf/config/db/mybatisconfiguration.xml" />
- <property name= "mapperlocations" value="/web-inf/config/db/*mapper.xml" />
- <property name="typealiasespackage" value="${mybatis.alias.basepackage}" />
- </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
- Sqlsessiontemplate this needs to write a configuration file, inject sqlsession into the implementation class, and then use Sqlsession, which is fine grained control
- Sqlsessiondaosupport this only needs to inherit the special class in the implementation class, you can use the sqlsession
- Mapperfactorybean this to write the configuration file, the corresponding interface in the configuration file reference, no need to write implementation class
- 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
- Configuration file to add a new
Java code
- <bean id="sqlsession" class="Org.mybatis.spring.SqlSessionTemplate" >
- <constructor-arg index="0" ref="sqlsessionfactory"/>
- <constructor-arg index="1" value="batch"/><!---if you want to do a bulk operation, you can add this property --
- </bean>
- Injection sqlsession ()
Java code
- @Reasource //annotation injection using Spring3
- Private sqlsession sqlsession;
- Use Sqlsession to operate
Java code
- Public User GetUser (String userId) {
- return (User) sqlsession.selectone ("Org.mybatis.spring.sample.mapper.UserMapper.getUser", userId);
- }
- Sqlsessiondaosupport (Sqlsessionfactory will be automatically assembled by spring, no manual injection required)
- Inheriting the Sqlsessiondaosupport class
Java code
- Public class Userdaoimpl extends Sqlsessiondaosupport implements Userdao {
- }
- Using the Getsqlsession () method to take sqlsession for data processing
Java code
- Public User GetUser (String userId) {
- return (User) getsqlsession (). SelectOne ("Org.mybatis.spring.sample.mapper.UserMapper.getUser", userId);
- }
- Write a configuration file that introduces each DAO interface
XML code
- <Bean id= "usermapper" class="Org.mybatis.spring.mapper.MapperFactoryBean">
- <property name= "mapperinterface" value=" Org.mybatis.spring.sample.mapper.UserMapper " />
- <property name= "sqlsessionfactory" ref="sqlsessionfactory" />
- </Bean>
- Operations can be injected directly into the DAO's interface at the business layer
- Write configuration file, the configuration package name automatically introduces all the interfaces in the package
XML code
- <Bean class="Org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name= "basepackage" value="Org.mybatis.spring.sample.mapper" />
- </Bean>
- In the business layer can be injected directly into the DAO interface operation, injected with the interface name, its first letter lowercase
- 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