spring 事务一致性使用xml配置

来源:互联网 发布:mc什么意思网络用语 编辑:程序博客网 时间:2024/05/23 17:53

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

 <context:property-placeholder location="classpath:jdbc.properties" />
 <bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <property name="driverClassName" value="${driverClassName}" />
  <property name="url" value="${url}" />
  <property name="username" value="${username}" />
  <property name="password" value="${password}" />
  <!-- 连接池启动时的初始值 -->
  <property name="initialSize" value="${initialSize}" />
  <!-- 连接池的最大值 -->
  <property name="maxActive" value="${maxActive}" />
  <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
  <property name="maxIdle" value="${maxIdle}" />
  <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
  <property name="minIdle" value="${minIdle}" />
 </bean>

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

 <bean id="txManager"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
 </bean>
 
 <!-- 定义事务传播属性XML配置  -->
    <tx:advice id="txAdvice" transaction-manager="txManager"> 
        <tx:attributes> 
            <tx:method name="add*"   propagation="REQUIRED" rollback-for="Exception"/>
            <!-- 一致性事务 -->
            <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
            <!-- 一致性事务 -->
            <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
            <!-- 一致性事务 -->
            <tx:method name="*" propagation="NOT_SUPPORTED" read-only="true" />
            <!-- 只读事务-->
        </tx:attributes> 
    </tx:advice> 
      
    <aop:config> 
        <aop:pointcut id="transactionPointCut" expression="execution(* com.royzhou.jdbc..*.*(..))"/> 
        <aop:advisor pointcut-ref="transactionPointCut" advice-ref="txAdvice"/> 
    </aop:config>

</beans>

 

package com.royzhou.jdbc;

public class PersonBean {
 private int id;
 private String name;

 public PersonBean() {
 }
 
 public PersonBean(String name) {
  this.name = name;
 }
 
 public PersonBean(int id, String name) {
  this.id = id;
  this.name = name;
 }
 
 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
 
 public String toString() {
  return this.id + ":" + this.name;
 }
}

 

 

package com.royzhou.jdbc;

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

import org.springframework.jdbc.core.RowMapper;

@SuppressWarnings("unchecked")
public class PersonRowMapper implements RowMapper {
 //默认已经执行rs.next(),可以直接取数据
 public Object mapRow(ResultSet rs, int index) throws SQLException {
  PersonBean pb = new PersonBean(rs.getInt("id"),rs.getString("name"));
  return pb;
 }
}

 

 

package com.royzhou.jdbc;

import java.util.List;

public interface PersonService {
 
 public void addPerson(PersonBean person) throws Exception;
 
 public void updatePerson(PersonBean person);
 
 public void deletePerson(int id);
 
 public PersonBean queryPerson(int id);
 
 public List<PersonBean> queryPersons();
}

package com.royzhou.jdbc;

import java.sql.Types;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
@Service("personService")
public class PersonServiceImpl implements PersonService {
    @Resource
 private JdbcTemplate jdbcTemplate;
 
 /**
  * 通过Spring容器注入datasource
  * 实例化JdbcTemplate,该类为主要操作数据库的类
  * @param ds
 
 public void setDataSource(DataSource ds) {
  this.jdbcTemplate = new JdbcTemplate(ds);
 }
  */
 public void addPerson(PersonBean person) throws Exception   {
  /**
   * 第一个参数为执行sql
   * 第二个参数为参数数据
   * 第三个参数为参数类型
   */
  jdbcTemplate.update("insert into person values(seq_person.nextval,?)", new Object[]{person.getName()}, new int[]{Types.VARCHAR});
  //throw new RuntimeException("运行期异常支持事务回滚");
  throw new Exception("其他异常不支持事务回滚");
 }

 public void deletePerson(int id) {
  jdbcTemplate.update("delete from person where id = ?", new Object[]{id}, new int[]{Types.INTEGER});
 }

 
 @SuppressWarnings("unchecked")
 public PersonBean queryPerson(int id) {
  /**
   * new PersonRowMapper()是一个实现RowMapper接口的类,
   * 执行回调,实现mapRow()方法将rs对象转换成PersonBean对象返回
   */
  List<PersonBean> pbs = (List<PersonBean>)jdbcTemplate.query("select id,name from person where id = ?", new Object[]{id}, new PersonRowMapper());
  PersonBean pb = null;
  if(pbs.size()>0) {
   pb = pbs.get(0);
  }
  return pb;
 }

 
 @SuppressWarnings("unchecked")
 public List<PersonBean> queryPersons() {
  List<PersonBean> pbs = (List<PersonBean>) jdbcTemplate.query("select id,name from person", new PersonRowMapper());
  return pbs;
 }

 public void updatePerson(PersonBean person) {
  jdbcTemplate.update("update person set name = ? where id = ?", new Object[]{person.getName(), person.getId()}, new int[]{Types.VARCHAR, Types.INTEGER});
 }

 public JdbcTemplate getJdbcTemplate() {
  return jdbcTemplate;
 }

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

 

0 0