Advance Description:
Integration Purpose: To enable mybatis Support Service agent
It needs to be done:
1, will create mybatis objects handed over to spring
① configured with third party data source connection pool
②spring create objects sqlsession
③mybatis interfaces created by mapping objects, spring is not supported by the interface to create objects, we need to give solutions (integration package)
2, configure the transaction
① Configure the transaction manager
② configure notifications
③ use AOP cut
Specific steps:
1, the configuration data source
<! - Configure Data Source ->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="jdbc:mysql://localhost:3306/sms"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="username" value="root"></property>
<property name="password" value="1234"></property>
</bean>
2, configures the session factory for creating sqlsession
Instructions, as long as the session factory configuration, you can get sqlsession objects (see source)
<! - Configure session factory ->
<bean name="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<! - injection data sources, for creating sqlsession ->
<property name="dataSource" ref="dataSource"></property>
</bean>
3, mapping interface to configure the scan package to create an object mapper
<! - scan package configuration mapping interface for creating an object mapper -> <bean name="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <Property name = "basePackage" value = "My"> </ Property>
<-! specified session factory -> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property> </bean>
4, configure the transaction manager
<! - Configure Transaction Manager ->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
5, configure notifications
<! - Configure Notifications -> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="select*" read-only="true" /> <tx:method name="find*" read-only="true"/> <tx:method name="get*" read-only="true"/> <tx:method name="query*" read-only="true"/> <tx:method name="*" read-only="false"/> </tx:attributes> </tx:advice>
6, AOP cut
<-! AOP cut -> <aop:config> <! - entry point -> <aop:pointcut expression="execution(* my.service..*.*(..))" id="pc"/> <! - Configuration section -> <aop:advisor advice-ref="txAdvice" pointcut-ref="pc" /> </aop:config>