一、开启事务流程
1)在spring整合mybatis的配置文件中追加以下内容(spring.xml)
<!-- 配置事务 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="dataSourceTransactionManager" />
2)在业务的实现层追加 @Transactional注解
前方高能:
1)若对于业务层(service)及持久的层(mapper)的扫描配置在SpringMVC的配置文件中时,一定要将这两个类的扫描配置在spring的配置的配置文件中
2)业务层的方法一定要时public方法
ps:如果这时spring的配置文件报错,一般都是名称空间缺少,追加一下对应的名称空间即可,下面我自己的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>
<!-- 对应的Sql文件路径 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
<!-- Mybaties配置文件路劲 -->
<property name="configLocation" value="classpath:config.xml"></property>
</bean>
<!--
扫描自定义的Mapper接口
在sql中返resultType parameterType的中直接写类名 相当于 work.chenc.mapper.UserEntity
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="work.chenc.mapper"></property>
</bean>
<!-- 配置事务 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="dataSourceTransactionManager" />
</beans>