在操作数据库的时候,有些操作是需要连贯起来不中断的执行,即需要原子性,事务控制就可以做到这一点
这里做一个转账的demo,转入和转出需要连贯的完成,不允许只执行其中一个
步骤:
1)导入坐标
2)创建接口,实现类,方法
3)将实现类的创建交给spring,即配置xml,不配置事务
4)创建测试类
5)加入自杀式报错并配置事务
6)测试
小结遇到的问题
1)`
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<!-- <scope>test</scope>-->
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.4</version>
</dependency>
'
2)
文件目录,controller是用于测试的
daoimpl
serviceimpl
接口自己写
domain类是根据数据库表写的实体类,数据库里面有wzy和ljx两个name,Money都是100
3)
配置xml文件,jdbc.properties文件自己写,或者翻我前面的文康康
<?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: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
">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}"/>
<property name="jdbcUrl" value="${url}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
<!-- //将自己的数据库登入信息注入到jdbc模板里-->
<bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="datasource"></property>
</bean>
<!-- 将jdbc模板注入到dao层实现类中-->
<bean id="acountdaoimpl" class="wzy.dao.impl.accountdaoimpl">
<property name="jdbcTemplate" ref="jdbctemplate"></property>
</bean>
<!-- 将dao层实现类注入到service层中-->
<bean id="accountserviceimpl" class="wzy.service.impl.accountserviceimpl">
<property name="accountdao" ref="acountdaoimpl"></property>
</bean>
</beans>
4)
测试
测试之后看数据库里面的数据有没有变
5)
在serviceimpl里面自杀一下
6)在xml里编写事务控制
先添加命名空间,加上tx和aop
<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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
">
<!-- 配置平台事务管理器-->
<bean id="transactionmanager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"></property>
</bean>
<!-- 通知 事务的增强-->
<tx:advice id="txadvice" transaction-manager="transactionmanager">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务的aop织入-->
<aop:config>
<aop:advisor advice-ref="txadvice" pointcut="execution(* wzy.service.impl.*.*(..))"></aop:advisor>
</aop:config>
7)同样使用controller的测试
小结问题:
我个人在测试的时候忘记了mysql默认的引擎myisam是不支持事务控制的
试了很多次发现那个事务配置一直没用
可以在数据库可视化界面里面执行一下sql语句“show engines”
【这是我修改过默认引擎后的查询,可以看到上面显示只有innodb是支持事务的】
接下来输入sql语句"show variables like “default_storage_engine”"
未修改过的value一栏是myisam
找到自己mysql的安装文件夹中的my.ini,用记事本打开修改默认引擎为innodb
之后再在mysql可视化界面里面属于sql语句"alter table tableName ENGINE=InnoDB" ,tablename是已经创建好了的表的名字,这个语句意义是修改表的引擎为innodb
修改好后重启数据库就ok啦(还是不行那就重启电脑吧)
最后测试出来结果会报除0的错误,然后数据库数据并未改变