一囤捻、開啟事務(wù)流程
1)在spring整合mybatis的配置文件中追加以下內(nèi)容(spring.xml)
<!-- 配置事務(wù) -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 數(shù)據(jù)源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="dataSourceTransactionManager" />
2)在業(yè)務(wù)的實(shí)現(xiàn)層追加 @Transactional注解
前方高能
:
1)若對(duì)于業(yè)務(wù)層(service)及持久的層(mapper)的掃描配置在SpringMVC的配置文件中時(shí)谅阿,一定要將這兩個(gè)類的掃描配置在spring的配置的配置文件中
2)業(yè)務(wù)層的方法一定要時(shí)public方法
ps:如果這時(shí)spring的配置文件報(bào)錯(cuò)友扰,一般都是名稱空間缺少苗傅,追加一下對(duì)應(yīng)的名稱空間即可,下面我自己的spring配置文件扳埂,僅供參考
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- <context:component-scan base-package="work.chenc.mapper"/>-->
<context:component-scan base-package="work.chenc.service"/>
<!-- 整合MyBatis -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"></property>
<property name="password" value="你的密碼"></property>
<property name="jdbcUrl" value="jdbc:mysql://47.102.143.25/test_dev?useUnicode=true&characterEncoding=UTF-8"></property>
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
<property name="initialPoolSize" value="5"></property>
<property name="maxPoolSize" value="10"></property>
</bean>
<!-- 配置MyBatis SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 對(duì)應(yīng)的Sql文件路徑 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
<!-- Mybaties配置文件路勁 -->
<property name="configLocation" value="classpath:config.xml"></property>
</bean>
<!--
掃描自定義的Mapper接口
在sql中返resultType parameterType的中直接寫類名 相當(dāng)于 work.chenc.mapper.UserEntity
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="work.chenc.mapper"></property>
</bean>
<!-- 配置事務(wù) -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 數(shù)據(jù)源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="dataSourceTransactionManager" />
</beans>