1. 程式人生 > >Spring 申明式事務之XML模式

Spring 申明式事務之XML模式

package shangbo.spring.transaction.jdbc.schema;

public class Job {
	private String jobId;
	private String jobTitle;
	private Integer minSalary;
	private Integer maxSalary;

	public String getJobId() {
		return jobId;
	}

	public void setJobId(String jobId) {
		this.jobId = jobId;
	}

	public String getJobTitle() {
		return jobTitle;
	}

	public void setJobTitle(String jobTitle) {
		this.jobTitle = jobTitle;
	}

	public Integer getMinSalary() {
		return minSalary;
	}

	public void setMinSalary(Integer minSalary) {
		this.minSalary = minSalary;
	}

	public Integer getMaxSalary() {
		return maxSalary;
	}

	public void setMaxSalary(Integer maxSalary) {
		this.maxSalary = maxSalary;
	}

	public String toString() {
		return "Job[jobId=" + jobId + ", jobTitle=" + jobTitle + ", minSalary=" + minSalary + ", maxSalary=" + maxSalary + "]";
	}
}

package shangbo.spring.transaction.jdbc.schema;

import org.apache.commons.dbcp.BasicDataSource;

public interface BusinessService {
	Job getJob(String jobId);

	void insertJob(Job job) throws Exception;

	void updateJob(Job job) throws Exception;

	void setDataSource(BasicDataSource dataSource);
}

package shangbo.spring.transaction.jdbc.schema;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

public class DefaultBusinessService implements BusinessService {
	private JdbcTemplate jdbcTemplate;

	public Job getJob(String jobId) {
		String sql = "SELECT * FROM HR.JOBS WHERE JOB_ID = ?";
		return jdbcTemplate.queryForObject(sql, new JobRowMapper(), jobId);
	}

	public void updateJob(Job job) throws Exception {
		String sql = "UPDATE HR.JOBS SET JOB_TITLE=?, MIN_SALARY = ?, MAX_SALARY = ? WHERE JOB_ID = ?";
		jdbcTemplate.update(sql, job.getJobTitle(), job.getMinSalary(), job.getMaxSalary(), job.getJobId());

		// 測試回滾
		// throw new Exception("test");
	}

	public void insertJob(Job job) throws Exception {
		String sql = "INSERT INTO HR.JOBS VALUES (?, ?, ?, ?)";
		jdbcTemplate.update(sql, job.getJobId(), job.getJobTitle(), job.getMinSalary(), job.getMaxSalary());

		// 測試回滾
		// throw new Exception("test");
	}

	public void setDataSource(BasicDataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	private static class JobRowMapper implements RowMapper<Job> {
		public Job mapRow(ResultSet rs, int rowNum) throws SQLException {
			Job job = new Job();
			job.setJobId(rs.getString(1));
			job.setJobTitle(rs.getString(2));
			job.setMinSalary(rs.getInt(3));
			job.setMaxSalary(rs.getInt(4));

			return job;
		}
	}

}

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.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">

	<!-- 定義 businessService -->
	<bean id="businessService" class="shangbo.spring.transaction.jdbc.schema.DefaultBusinessService">
	    <property name="dataSource" ref="dataSource"/>
	</bean>
	
	<!-- 事務 Advice -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <!-- 設定以get開頭的方法為只讀事務 -->
            <tx:method name="get*" read-only="true"/>
            <!-- 設定其他方法為預設事務 -->
            <tx:method name="*" rollback-for="Exception" />
        </tx:attributes>
    </tx:advice>
	
	<!-- aop 設定 -->
    <aop:config>
        <aop:pointcut id="businessServiceOperation" expression="execution(* shangbo.spring.transaction.jdbc.schema.DefaultBusinessService.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="businessServiceOperation"/>
    </aop:config>
	
	<!-- 定義  dataSource -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
	    <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
	    <property name="username" value="hr" />
	    <property name="password" value="123456" />
	</bean>
	
	<!-- 定義 jdbc 事務管理器 -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	    <property name="dataSource" ref="dataSource"/>
	</bean>

</beans>
package shangbo.spring.transaction.jdbc.schema;

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

public class App {

	public static void main(String[] args) throws Exception {
		// 例項化 Spring IoC 容器
		ApplicationContext context = new ClassPathXmlApplicationContext("example.xml", BusinessService.class);

		// 從容器中獲得 BusinessService 的例項
		BusinessService service = context.getBean(BusinessService.class);

		// 使用 BusinessService
		// 插入 job
		Job job = newJob();
		// service.insertJob(job);

		// 更新 job
		service.updateJob(job);

		// 查詢 job
		System.out.println(service.getJob("IT"));
	}

	private static Job newJob() {
		Job job = new Job();
		job.setJobId("IT");
		job.setJobTitle("IT Engineer");
		job.setMinSalary(3);
		job.setMaxSalary(100000);

		return job;
	}
}


– 聲 明:轉載請註明出處
– Last Updated on 2017-06-12
– Written by ShangBo on 2017-06-12
– End