使用IOC(控制反转)技术容器全权负责的组件的装配,它会把符合依赖关系的对象通过JavaBean属性或者构造函数传递给需要的对象。
一.建立数据库database,表明tb_user
CREATE TABLE `tb_user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`age` int(10) unsigned NOT NULL,
`sex` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
)
二、建立在src下建立包并建立applicationContext.xml
1.main包
1.1建立Manager.java
2.transaction
2.1TransactionExample.java
三在WEB-INF下的lib导入jar包
1.aopalliance.jar(这个包是AOP联盟的API包,里面包含了针对面向切面的接口。)
2.commons-logging.jar(Commons Logging (JCL)提供的是一个日志(Log)接口(interface),同时兼顾轻量级和不依赖于具体的日志实现工具。)
3.mysql-connector-java-5.1.20-bin.jar
4.org.springframework.asm-3.1.1.RELEASE.jar
5.org.springframework.beans-3.1.1.RELEASE.jar(Spring IoC的基础的实现)
6.org.springframework.core-3.1.1.RELEASE.jar(Spring 的核心模块)
7.org.springframework.jdbc-3.1.1.RELEASE.jar(Spring 的JDBC模块)
8.org.springframework.transaction-3.1.1.RELEASE.jar
四、以下是程序
Manager.java
package com.mr.main;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import com.mr.transaction.TransactionExample;
public class Manager {
public static void main(String[] args) {
Resource resource =new ClassPathResource("applicationContext.xml");//装载配置文件
BeanFactory factory =new XmlBeanFactory(resource);
TransactionExample transactionExample = (TransactionExample) factory.getBean("transactionExample");//获取UserDAO
transactionExample.transactionOperation();//执行添加方法
}
}
2.TransactionExample.java
package com.mr.transaction;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
public class TransactionExample {
DataSourcedataSource;//注入数据源
PlatformTransactionManagertransactionManager;//注入事务管理器
TransactionTemplatetransactionTemplate;//注入TransactionTemplate模板
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public PlatformTransactionManager getTransactionManager() {
return transactionManager;
}
public void setTransactionManager(PlatformTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
public TransactionTemplate getTransactionTemplate() {
return transactionTemplate;
}
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
public void transactionOperation() {
transactionTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
Connection conn = DataSourceUtils.getConnection(dataSource);//获得数据库连接
try {
Statement stmt = conn.createStatement();
//执行两次添加方法
stmt.execute("insert into tb_user(name,age,sex) values('小强','26','男')");
stmt.execute("insert into tb_user(name,age,sex) values('小红','22','女')");
System.out.println("操作执行成功!");
}catch (Exception e) {
transactionManager.rollback(status);//事务回滚
System.out.println("操作执行失败,事务回滚!");
System.out.println("原因:"+e.getMessage());
}
return null;
}
});
}
}
3.applicationContext.xml
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!--配置数据源-->
<beans>
<bean id="database"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value=" jdbc:mysql://localhost:3306/db_database17"/>
<property name="username" value="root"/>
<property name="password" value="密码" />
</bean>
<!--定义TransactionTemplate模板 -->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref bean="transactionManager"/>
<property name="propagationBehaviorName" value="PROPAGATION_REQUIRED"/>
</bean>
<!--定义事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref bean="dataSource"/>
</bean>
<!--为TransactionExample注入数据源 、事务管理器、TransactionTemplate模板-->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="dataSource" ref bean="dataSource"/>
<property name="transactionManager" ref bean="transactionManager" />
<property name="transactionTemplate" ref bean="transactionTemplate"/>
</bean>
</beans>
五、运行结果