Spring 申明式事务之XML模式

– Start
点击此处观看本系列配套视频。


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;
	}
}

更多参见:Spring Framework 精萃
– 声 明:转载请注明出处
– Last Updated on 2017-06-12
– Written by ShangBo on 2017-06-12
– End

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值