spring数据库事务
常用的注解方式@Transactional,可用于类和方法中,配置项中常用的isolation隔离级别,propagation传播行为两个。
spring+mybatis组合使用事务
xml配置
启用扫描机制
<context:annotation-confg />
<context:component-sacan base-package="xxxx.xxx.xxx"/>
数据库连接池
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name=“driverClassName" value="com.mysql.jdbc.Driver" />
..........
集成mybatis
<bean id="sqlsessionfactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:/mybatis-config.xml" />
配置事务管理器
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="dataSource" />
使用注解方式事务
<tx:annotation-driven transaction-manager="transactionManager" />
采用自动扫描方式生成bean
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="xxx.xxx.xxx" />
<property name="SqlSessionFactoryBeanName" value="SqlSessionFactory" />
<property name="annotationClass" value="org.springframework.stereotype.Repository" />
相应的实体类pojo,接口,和mapper.xml对应,并配置mybatis-config.xml引入映射器
<configuration>
<mapper resource="com/ss/sqlMapper/RoleMapper.xml">
-----
@Repository可理解为数据访问层DAO,@Service可理解为BLL业务逻辑层