《Spring》------声明式事务处理

- 前言

提到对数据库的CRUD操作就想到了事务,在之前做过的项目中操作事务时,用到的是Spring框架,Spring的声明式事务处理将,操作起来相对于JDBC来说,简单了很多,其目的是让程序员不再关注事务。

操作步骤

创建持久化类和映射文件

 创建hibernate.cfg.xml,person.class、person.hbm.xml
 代码小编省略

写spring的配置文件,引入sessionFactory

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

  <!--JDBC文件配置-->
<bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>classpath:jdbc.properties</value>
        </property>
</bean>

    <bean id="dataSource" destroy-method="close"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <!--引入SessionFactory-->

    <bean id="sessionFactory2"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 映射文件所在的路径 -->
        <property name="mappingDirectoryLocations">
            <list>
                <!-- spring容器会去该包及子包下搜索所有的映射文件 -->
                <value>com/dmsd/spring/domain</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

    <!-- 
          创建 事物的管理器  其作用就是告诉Spring容器使用的是什么事物管理技术
            1.HiberanteTransactionManager
            2.DataSourceTransactionManager
            3.JdoTransactionManager
    -->
    <bean id="transactionManager" 
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory2" />
        </property>

    </bean>

    <!-- 
        事物的声明 
            1、告诉Spring容器事物管理器
            2.告诉Spring容器什么样的目标方法采用什么样的事物策略
    -->
    <tx:advice transaction-manager="transactionManager" id="tx">
        <tx:attributes>
        <!-- 
            name:目标方法的范围
            isolation:事物的隔离机制
            propagation:传播属性,解决事物的嵌套问题
            read-only:true(只读事物) false(读写事物)
         -->
            <tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED"
                read-only="false" />
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut expression="execution(* com.dmsd.spring.service.impl.*.*(..))"
            id="perform" />
        <aop:advisor advice-ref="tx" pointcut-ref="perform" />
    </aop:config>

  <!--编写personService和personDao的bean-->

    <bean id="personService" class="com.dmsd.spring.service.impl.PersonServiceImpl">
        <property name="personDao">
            <ref bean="personDao" />
        </property>
    </bean>

    <bean id="personDao" class="com.dmsd.spring.dao.impl.PersonDaoImpl">
        <property name="sessionFactory">
            <ref bean="sessionFactory2" />
        </property>
    </bean>
</beans>

Persondao

public interface PersonDao {
    public void savePerson(Person person);
}

PersonDaoImpl

public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao {

    public void savePerson(Person person) {
        this.getHibernateTemplate().save(person);
    }

}

PersonService

public interface PersonService {
    public void savePerson(Person person);
}

PersonServiceImpl

public class PersonServiceImpl implements PersonService {

    private PersonDao personDao;

    public PersonDao getPersonDao() {
        return personDao;
    }

    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }

    public void savePerson(Person person) {
        this.personDao.savePerson(person);
    }

}

测试类

@Test
    public void testSavePerson(){
        ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");
        PersonService personService =(PersonService) context.getBean("personService");
        Person person=new Person();
        person.setDescription("这是谁的媳妇儿????");
        person.setName("王二狗");
        personService.savePerson(person);

    }

小结

  • Spring的声明式事务处理配置在项目中的应用大概就这几个步骤,其中主要的配置都在Spring的配置文件中了,在引入SessionFactory的时候,要配置好DataSource,然后声明事务管理器,声明事务策略,配置AOP切面,这样,一条线就下来了。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 20
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值