Spring + jdbcJdbcTemplate 配置声明式事务

Spring配置声明式事务是个老话,但有必要搭建环境调试一下,加深影响与理解。


1 准备所需要的jar包,Spring核心jar包+声明式事务依赖的jar+mysql_jdbcjar+日志jar

2 建立一个java工程,把jar引进来。

3 核心Spring.xml文件(配置了数据源-jdbc包装类-声明式事务(可以这样理解,pointcut切入点:那些包的那些类需要做事务管理 。advice 切面 哪些方法需要做事务,事务的传播特性是什么。transactionManager:事务管理器))


<?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"
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-3.2.xsd  
                  http://www.springframework.org/schema/context  
                  http://www.springframework.org/schema/context/spring-context-3.2.xsd  
                  http://www.springframework.org/schema/aop   
                  http://www.springframework.org/schema/aop/spring-aop.xsd        
                 http://www.springframework.org/schema/tx   
                 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">


<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/db_test" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>


<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>


<bean id="departmentDAO" class="com.xw.dao.impl.DepartmentImplDAO">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>


<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>


<tx:advice id="TestAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>


<aop:config>
<aop:pointcut id="serviceMethod" expression="execution(* com.xw.dao.*.*(..))" />
<aop:advisor pointcut-ref="serviceMethod" advice-ref="TestAdvice" />
</aop:config>


4.MainDemo类,启动类

package com.xw.main;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.xw.dao.DepartmentDAO;


public class MainDemo {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
DepartmentDAO dao = (DepartmentDAO) context.getBean("departmentDAO");
dao.saveDepartment();
}
}


5.bean类 Department

public class Department {
private Long deptId;
private String deptNo;
private String deptName;


public Department() {


}


public Department(Long deptId, String deptNo, String deptName) {
this.deptId = deptId;
this.deptNo = deptNo;
this.deptName = deptName;
}


public Long getDeptId() {
return deptId;
}


public void setDeptId(Long deptId) {
this.deptId = deptId;
}


public String getDeptNo() {
return deptNo;
}


public void setDeptNo(String deptNo) {
this.deptNo = deptNo;
}


public String getDeptName() {
return deptName;
}


public void setDeptName(String deptName) {
this.deptName = deptName;
}


}


6. 接口自己实现吧。很简单,就两个方法,一个查,一个插


package com.xw.dao.impl;


import java.util.ArrayList;
import java.util.List;


import org.springframework.jdbc.core.JdbcTemplate;


import com.xw.bean.Department;
import com.xw.dao.DepartmentDAO;


public class DepartmentImplDAO implements DepartmentDAO {


private JdbcTemplate jdbcTemplate;


public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}


public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}


@Override
public List<Department> queryDepartment() {
List<Department> list = new ArrayList<Department>();
// try {
// Connection conn = dataSource.getConnection();
//
// String sql = "Select d.dept_id, d.dept_no, d.dept_name from
// department d";
// Statement smt = conn.createStatement();
//
// ResultSet rs = smt.executeQuery(sql);
// while (rs.next()) {
// Long deptId = rs.getLong("dept_id");
// String deptNo = rs.getString("dept_no");
// String deptName = rs.getString("dept_name");
// Department dept = new Department(deptId, deptNo, deptName);
// list.add(dept);
// }
// } catch (Exception e) {
// }
return list;
}


@Override
public void saveDepartment() {
String sql = "insert into department(dept_id,dept_no,dept_name) values(5,'5','oracle')";
jdbcTemplate.execute(sql);
throw new RuntimeException();


}


}


7.重点来了,数据库,mysql,而且注意了,引擎类型需要为 Innodb.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值