1.首先要实现事务管理,要确定所要管理的Mysql数据库表的引擎。下边简单介绍两种我开发时常用的引擎:
MyISAM:MYISAM强调了快速读取操作,这可能就是为什么MySQL受到了WEB开发如此青睐的主要原因:在WEB开发中你所进行的大量数据操作都是读取操作。所以,大多数虚拟主机提供商和INTERNET平台提供商只允许使用MYISAM格式。MyISAM格式的一个重要缺陷就是不能在表损坏后恢复数据。
InnoDB:InnoDB数据库引擎都是造就MySQL灵活性的技术的直接产品,这项技术就是MYSQL+API。MyISAM数据库引擎不支持事务处理(transaction process)也不支持外来键。尽管它比MyISAM引擎慢很多,但是InnoDB包括了对事务处理和外来键的支持,这两点是MyISAM引擎所没有的。
综上所述,如果要使用事务管理,需要采用InnoDB引擎。
2.spring命名空间
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
3.配置注解类型的Bean扫描
<context:annotation-config />
<context:component-scan base-package="com.**.was.**.service.impl" />
4.定义配置文件读取类
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:application.properties</value>
</list>
</property>
</bean>
5.数据源配置,下面贴出基于DBCP连接池的mysql数据源配置
<bean id="sqlDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${sqlDataSource.jdbc.url}" />
<property name="username" value="${sqlDataSource.jdbc.username}" />
<property name="password" value="${sqlDataSource.jdbc.password}" />
<!-- 连接池最大数量 -->
<property name="maxActive" value="300" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="150000" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="10" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="10" />
</bean>
6.myBatis配置
<!-- mybatis 配置start -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="sqlDataSource" />
<!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
<property name="typeAliasesPackage" value="com.**.**.model" />
<!-- 显式指定Mapper文件位置 -->
<property name="mapperLocations" value="classpath*:com/**/**/mappers/*Mapper.xml" />
</bean>
<!-- 扫描basePackage下所有以@MyBatisRepository标识的 接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.**.**.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
以上是标准配置,下面正式开始介绍两种事务配置方式:
首先要配置事务管理器,此为两种配置方式的必须配置
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="sqlDataSource" />
</bean>
1.结合spring的@Transactional完成事务控制
spring中止只需配置一句话:
<tx:annotation-driven transaction-manager="transactionManager" /> <!--使用注解驱动-->
之后在所需要进行事务管理的类或者方法上添加@Transactional注解
2.spring全配置,不需要结合@Transactional注解
<!-- 配置事务的通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 配置切面 -->
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut expression="execution(* com.**..*service.*.*(..))" id="pointcut"/>
<!-- 配置切面 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
以上就是spring+mybatis+mysql的所有配置。