<properties>
<spring.version>4.0.2.RELEASEspring.version>
properties>
<dependencies>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.12version>
dependency>
<dependency>
<groupId>com.mchangegroupId>
<artifactId>c3p0artifactId>
<version>0.9.5.5version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.44version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.6version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>
dependencies>
【mybatis的配置----1.读取database.properties文件;2.连接数据库;3.读取mapper文件等配置将会在 整合文件spring-mybatis.xml 中进行】
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
settings>
<typeAliases>
<package name="com.hz.pojo" />
typeAliases>
configuration>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
/*
*spring配置文件代码
*/
beans>
<context:property-placeholder location="classpath:database.properties"/>
【创建对象名为dataSource,相当于new了一个系统对象ComboPooledDataSource,并给其对象下四个属性赋值】
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.jules.pojo"/>
<property name="mapperLocations" value="classpath:mappers/*.xml"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.jules.dao"/>
bean>
<context:component-scan base-package="com.jules.service"/>
就正常的按照mybatis的形式写增删改查测试(用mapper,xml写sql,或者注解写–看图),下面只给出测试类的代码。
public class UserServiceImplTest {
@Test
public void findUserById() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-mybatis.xml");
UserService userService = (UserService) applicationContext.getBean("userServiceImpl");
User user = userService.findUserById(1);
System.out.println(user.toString());
}
}
【事务的四大特性:原子性、一致性、隔离性、持久性】
【事务的作用:在数据层保障一系列的数据库操作同成功同失败】
【Spring事务的作用:在 数据层 或 业务层 保障一系列的数据库操作同成功同失败】
事务传播机制:【就是事务在多个方法的调用中是如何传递的,是重新创建事务还是使用父方法的事务?父方法的回滚对子方法的事务是否有影响?这些都是可以通过事务传播机制来决定的】
- propagation_requierd(默认):如果当前没有事务,就新建一个事务,如果已存在一个事务中,加入到这个事务中,这是最常见的选择。
- propagation_supports:支持当前事务,如果没有当前事务,就以非事务方法执行。
- propagation_mandatory:使用当前事务,如果没有当前事务,就抛出异常。
- propagation_required_new:新建事务,如果当前存在事务,把当前事务挂起。
- propagation_not_supported:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
- propagation_never:以非事务方式执行操作,如果当前事务存在则抛出异常。
- propagation_nested:如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与propagation_required类似的操作
▶pom.xml
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.7version>
<scope>runtimescope>
dependency>
▶1.spring-mybatis.xml中导入命名空间,tx与aop的
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
▶2.定义事务管理器DataSourceTransactionManager并为其注入数据源Bean
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
bean>
<tx:advice id="txadvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true" timeout="1000" propagation="SUPPORTS"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="*"/>
tx:attributes>
tx:advice>
<aop:config>
<aop:pointcut id="serviceMethod" expression="execution(* com.hz.service..*.*(..))"/>
<aop:advisor advice-ref="txadvice" pointcut-ref="serviceMethod"/>
aop:config>
tx:method属性 | 说明 |
---|---|
timeout | 事务超时时间,允许事务运行的最长时间,以秒为单位。默认值为-1,表示不超时 |
read-only | 事务是否为只读,默认值为false |
rollback-for | 设定能够触发回滚的异常类型Spring默认只在抛出runtime exception时才标识事务回滚,可以通过全限定类名指定需要回滚事务的异常,多个类名用逗号隔开 |
no-rollback-for | 设定不触发回滚的异常类型 |
▶3.java代码:
两个账号的一转一收,同时进行的,一方出错,另一方的金额也是无法增加或者减少的。
java的测试类代码省略,具体事务的实现理解见:https://blog.csdn.net/m0_70083523/article/details/126751937
………………略
▶1.spring-mybatis.xml中
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
bean>
<tx:annotation-driven />
▶2.注解使用
//@Transactional //类中开启事务
@Service
public class MoneyServiceImpl implements MoneyService {
@Resource
private MoneyDao moneyDao;
//使用事务(传播机制)
@Transactional(propagation = Propagation.REQUIRED)
public int updateMoney() {
moneyDao.updateMoney("Jules","300");
moneyDao.updateMoney("兰波","700");
return 0;
}
}