There are many restrictions on using mybatis independently (for example, transactions that span multiple sessions cannot be implemented), and many business systems are originally transactions managed using spring, therefore, it is best to integrate mybatis with spring.
Pre-requirement version requirements
Project |
Version |
|
Description |
Mybatis |
3.0 or above |
Https://github.com/mybatis/mybatis-3/releases |
|
Spring |
3.0 or above |
Http://projects.spring.io/spring-framework/ |
|
Mybatis-spring |
1.0 or above |
Https://github.com/mybatis/spring/releases |
|
Spring transaction Configuration
<! -- Automatically scans the business package --> <context: component-scan base-package = "com. xxx. service"/> <! -- Data source --> <jee: jndi-lookup id = "jndiDataSource" jndi-name = "java: comp/env/jdbc/datasource"/> <! -- Configure transactions --> <bean id = "txManager" class = "org. springframework. jdbc. datasource. dataSourceTransactionManager "> <property name =" dataSource "ref =" jndiDataSource "/> </bean> <! -- Configure annotation-based thing aop --> <tx: annotation-driven transaction-manager = "txManager" proxy-target-class = "true"/>
Single Integration
<! -- Integrate mybatis --> <bean id = "sqlSessionFactory" class = "org. mybatis. spring. sqlSessionFactoryBean "> <property name =" dataSource "ref =" jndiDataSource "/> <property name =" configLocation "value =" classpath:/mybatis/mybatis-config.xml "/> <! -- Automatically configure the alias --> <property name = "typeAliasesPackage" value = "com. xxx. dto"/> </bean> <! -- Create dao bean (you only need to provide interfaces without providing implementation classes) --> <bean id = "userDao" class = "org. mybatis. spring. mapper. mapperFactoryBean "> <property name =" mapperInterface "value =" com. xxx. dao. userDao "/> <property name =" sqlSessionFactory "ref =" sqlSessionFactory "/> </bean>
We should not only understand how to use it, but also understand why it should be used.
SqlSessionFactoryBeanA factory bean is used to parse configurations (data sources, aliases, etc ).
MapperFactoryBeanIt is a factory bean. In spring containers, factory beans have special purposes. When spring injects factory beans into other beans, instead of injecting the factory bean itself, it calls the bean's getObject method. Next let's take a look at what the getObjec method has done:
public T getObject() throws Exception { return getSqlSession().getMapper(this.mapperInterface); }
As you can see, this method is the same as Mybatis, which is used to obtain a Sqlsession object first, then obtain the Mapper object from the Sqlsession (the Mapper is a proxy object, which is used to represent the mapperInterface interface, which is the dao interface provided by the user ). Naturally, the Mapper object is injected to the business layer.
The actual project generally has more than one Dao. If you have more than one Dao, configure it in sequence according to the above configuration.
How to Use batch update
The previous section describes how to inject a mapper object to the business layer. mapper behavior depends on configuration. mybatis uses a single update by default (that is, ExecutorType is SIMPLE rather than BATCH by default ), of course, we can modify the default behavior by modifying the mybatis configuration file. However, if we only want one or more mappers to use batch update, we cannot. In this case, we need to use the template technology:
<! -- Customize the behavior of mybatis through the template --> <bean id = "sqlSessionTemplateSimple" class = "org. mybatis. spring. sqlSessionTemplate "> <constructor-arg index =" 0 "ref =" sqlSessionFactory "/> <! -- Update in a single mode --> <constructor-arg index = "1" value = "SIMPLE"/> </bean> <! -- Customize the behavior of mybatis through templates --> <bean id = "sqlSessionTemplateBatch" class = "org. mybatis. spring. sqlSessionTemplate "> <constructor-arg index =" 0 "ref =" sqlSessionFactory "/> <! -- BATCH update mode --> <constructor-arg index = "1" value = "BATCH"/> </bean>
Here I have defined two template objects, one using a single Update and the other using a batch update. With the template, we can change the mapper's behavior mode:
<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean"><property name="mapperInterface" value="com.xxx.dao.UserDao" /><property name="sqlSessionTemplate" ref=" sqlSessionTemplateBatch " /></bean>
Unlike the mapper configuration in the previous sectionSqlSessionFactoryAttribute, you only need to configureSqlSessionTemplate(SqlSessionFactoryAttributes have been configured in the template ).
Simplify mapper configuration through automatic scanning
As you can see in the previous chapter, we need to configure dao in the configuration file one by one. If there are many dao files, the configuration file will be very large, which makes management quite painful. Fortunately, the mybatis team also realized this. They used the automatic scanning feature provided by spring to encapsulate a tool class for automatic dao scanning, so that we can use this feature to simplify the Configuration:
<! -- Create mapper bean by means of automatic scanning (single update mode) --> <bean class = "org. mybatis. spring. mapper. mapperScannerConfigurer "> <property name =" basePackage "value =" com. xxx. dao "/> <property name =" sqlSessionTemplateBeanName "value =" sqlSessionTemplateSimple "/> <property name =" markerInterface "value =" com. xxx. dao. simpleDao "/> </bean> <! -- Create mapper bean by means of automatic scanning (batch update mode) --> <bean class = "org. mybatis. spring. mapper. mapperScannerConfigurer "> <property name =" basePackage "value =" com. xxx. dao "/> <property name =" sqlSessionTemplateBeanName "value =" sqlSessionTemplateBatch "/> <property name =" markerInterface "value =" com. xxx. dao. batchDao "/> </bean>
I will not talk about the spring technology involved in MapperScannerConfigurer. If you are interested in spring principles, you can check its source code. Let's take a look at its three attributes:
BasePackage: name of the basic package that the scanner starts scanning. nested scanning is supported;
SqlSessionTemplateBeanName: name of the template bean mentioned above;
MarkerInterface: an interface-based filter. dao that implements this interface will be scanned by the scanner. It is related to basePackage.
In addition to interface filtering, you can also use annotation Filtering:
<! -- Create mapper bean by means of automatic scanning (batch update mode) --> <bean class = "org. mybatis. spring. mapper. mapperScannerConfigurer "> <property name =" basePackage "value =" com. xxx. dao "/> <property name =" sqlSessionTemplateBeanName "value =" sqlSessionTemplateBatch "/> <property name =" annotationClass "value =" com. xxx. dao. batchAnnotation "/> </bean>
AnnotationClass:The dao with this annotation configured will be scanned by the scanner, and is associated with the basePackage.
Note that only one filter condition can be configured.