Spring 申明式事务之XML模式

来源:互联网 发布:int java 编辑:程序博客网 时间:2024/06/05 12:01

– 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

原创粉丝点击