zoukankan      html  css  js  c++  java
  • Spring事务管理

    package com.transaction;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Iterator;
    import java.util.List;
    
    import com.dao.IPeopleDao;
    import com.dao.People;
    
    public class TransactionRun {
        private IPeopleDao peopleDao;
    
        public void setPeopleDao(IPeopleDao peopleDao) {
            this.peopleDao = peopleDao;
        }
    
        public IPeopleDao getPeopleDao() {
            return peopleDao;
        }
        
        public void run(){
            People people=new People();
            people.setName("事务管理");
            people.setSex("男");
            people.setAge(12);
            Date d=null;
            try {
                d = (new SimpleDateFormat("yyyy-mm-dd")).parse("2016-03-07");
            
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            people.setBirthday(d);
            
            peopleDao.addPeople(people);
            
            System.out.println(peopleDao.getPeopleCount());
            //System.out.println(peopleDao.getPeopleName(Integer.MAX_VALUE));//这个会出现异常,因为id为最大的int值的没有
            
            List<People> peoples=peopleDao.findAllPeople();
            Iterator<People> it=peoples.iterator();
            while(it.hasNext()){
                People p=it.next();
                System.out.println(p.getName()+"	"+p.getAge());
            }
        }
        
    
    }
    
    
    package com.Test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.transaction.TransactionRun;
    
    public class TestTransactionRun {
        public static void main(String[] args){
            ApplicationContext contre=new ClassPathXmlApplicationContext("applicationContext.xml");
            TransactionRun dr=contre.getBean("transactionRun",TransactionRun.class);
            dr.run();
        }
    
    }
    View Code

    前面是测试类

    <?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:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
        
        
        <bean id="configproperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
            <property name="location" value="jdbc.properties"/>
       </bean>
        
       <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
               <property name="properties" ref="configproperties"/>
       </bean>
    
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
            <property name="driverClassName" value="${jdbc.driver}" />
        </bean>

    <!-- 前三个bean是取读xml内容注入dataSource -->
    <bean id="peopleDao" class="com.dao.IPeopleDaoImpl" depends-on="dataSource" > <property name="dataSource" ref="dataSource" /> </bean> <bean id="jdbcTransactionMessage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 配置事务管理器,注入数据源--> <property name="dataSource"> <ref bean="dataSource"></ref> </property> </bean> <bean id="transactionAttributeSource" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource"> <!-- 配置事务传播特性 --> <property name="properties"> <props> <prop key="*">PROPAGATION_REQUIRED</prop><!-- PROPAGATION_REQUIRED如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。 -->
            <!--<prop key="run">PROPAGATION_REQUIRED</prop> --> </props> </property> </bean>
    <!--配置参与事务的类以及处理事务的过程--> <bean id="transactionRun" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="jdbcTransactionMessage" /> <property name="target"> <bean class="com.transaction.TransactionRun"> <property name="peopleDao" ref="peopleDao"></property> </bean> </property> <property name="transactionAttributeSource" ref="transactionAttributeSource"></property> </bean> </beans>
  • 相关阅读:
    LAMP应用 wdlinux 配置文件
    Linux mysql忘记root密码
    vim 编辑器 常用的设置
    Virtual PC(VPC)虚拟机安装CentOS 6.0网络配置
    缓存系统memcache的安装,配置和使用
    CentOS 去掉Last login提示/系统欢迎信息
    configure: error: GD build test failed. Please check the config.log for details.
    Centos 6.0 yum 更新源
    MySQL is running but PID file is not found
    Visual Studio快速开发以及Visual Studio 2010新功能介绍
  • 原文地址:https://www.cnblogs.com/aigeileshei/p/5477493.html
Copyright © 2011-2022 走看看