一、定义服务接口:
package cn.edu.tju.study.service.transaction2;
public interface StudentService {
public void addStudent(String name, int age);
}
二、定义服务实现类
package cn.edu.tju.study.service.transaction2;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
@Service
public class StudentServiceImpl implements StudentService{
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public void addStudent(String name,int age) {
String sql = "insert into student values('";
sql += name;
sql +="'";
sql += ",";
sql += age;
sql += ")";
jdbcTemplate.execute(sql);
int i = 1/0;
jdbcTemplate.execute(sql);
}
}
三、配置文件:文章来源:https://www.toymoban.com/news/detail-596269.html
<?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:tx="http://www.springframework.org/schema/tx"
default-autowire="byName"
xmlns:context="http://www.springframework.org/schema/context"
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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://xxx.xxx.xxx.xxx/test"></property>
<property name="username" value="root"></property>
<property name="password" value="MyPa"></property>
</bean>
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="studentService" class="cn.edu.tju.study.service.transaction2.StudentServiceImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<bean id="myFactoryBean"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager" />
<property name="target" ref="studentService" />
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
四、主类,调用TransactionProxyFactoryBean文章来源地址https://www.toymoban.com/news/detail-596269.html
package cn.edu.tju.study.service.transaction2;
import cn.edu.tju.study.service.MyConfig3;
import cn.edu.tju.study.service.aspect.MyDemoService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TransactionTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("transaction.xml");
StudentService studentService = context.getBean("myFactoryBean", StudentService.class);
studentService.addStudent("paul",23);
}
}
到了这里,关于spring复习:(42)配置文件的方式实现事务(TransactionProxyFactoryBean)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!