Spring + jdbcJdbcTemplate 配置声明式事务

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


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

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

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



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">


class="org.springframework.jdbc.datasource.DriverManagerDataSource">

















class="org.springframework.jdbc.datasource.DataSourceTransactionManager">



















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 queryDepartment() {
List list = new ArrayList();
// 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.



你可能感兴趣的:(web)