SpringMVC框架中添加事务

来源:互联网 发布:软件开发人工费 标准 编辑:程序博客网 时间:2024/06/03 23:35

        事务主要是保证一系列相关联的操作要么一次都成功,要么一次都失败,不能出现成功一部分,失败一部分的情况。

        在SpringMVC框架中使用事务,需要在配置文件中做下配置,这里以开发的一个工程为例,在applicationContext-mybatis.xml文件的头部添加关于事务的声明,关于"tx"的配置就是关于事务的声明。在数据库连接配置dataSource之后添加事务相关配置,如下所示。这样事务结合其它配置便可起作用了。

<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"       xmlns:aop="http://www.springframework.org/schema/aop"        xmlns:context="http://www.springframework.org/schema/context"       xmlns:jee="http://www.springframework.org/schema/jee"        xmlns:tx="http://www.springframework.org/schema/tx"       xmlns:task="http://www.springframework.org/schema/task"       xsi:schemaLocation="http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd        http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-3.2.xsd        http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx.xsd        http://www.springframework.org/schema/jee        http://www.springframework.org/schema/jee/spring-jee-3.2.xsd">    <context:property-placeholder location="classpath:applications.properties" />    <!--数据库连接配置-->    <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"          destroy-method="close">        <!-- Connection Info -->        <property name="driverClassName" value="${jdbc.driver}" />        <property name="url" value="${jdbc.url}" />        <property name="username" value="xxxxxx" />        <property name="password" value="xxxx" />        <!-- Connection Pooling Info -->        <property name="maxActive" value="100" />        <property name="maxIdle" value="50" />        <property name="defaultAutoCommit" value="true" />        <property name="maxAge" value="36000" />        <property name="initialSize" value="0" />    </bean>    <!--事务配置开始位置-->    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource"/>    </bean>    <!-- enable transaction annotation support -->    <tx:annotation-driven transaction-manager="txManager"/>    <!--事务配置结束-->    <!-- redis数据源 -->    <!--<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">        <property name="maxIdle" value="300" />        <property name="maxTotal" value="600" />        <property name="maxWaitMillis" value="1000" />        <property name="testOnBorrow" value="true" />    </bean>-->    <!-- Spring-redis连接池管理工厂 -->    <!--<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"          p:host-name="192.168.1.136" p:port="6379"  p:password="123"  p:pool-config-ref="poolConfig"/>-->    <!-- 使用中间类解决RedisCache.jedisConnectionFactory的静态注入,从而使MyBatis实现第三方缓存 -->    <!--<bean id="redisCacheTransfer" class="com.util.RedisCacheTransfer">-->        <!--<property name="jedisConnectionFactory" ref="jedisConnectionFactory"/>-->    <!--</bean>-->    <!--数据库连接池配置,包括与mybatis结合以及扫描的位置-->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource" />        <property name="configLocation" value="classpath:mybatis-config.xml"/>        <!-- 配置扫描实体类的位置 -->        <property name="typeAliasesPackage" value="com.entity" />        <!-- 配置扫描Mapper XML的位置 -->        <property name="mapperLocations" value="classpath:mapper/*Mapper.xml" />    </bean>    <!-- 配置扫描Mapper接口的包路径 -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>        <property name="basePackage" value="com.dao"/>    </bean></beans>




0 0
原创粉丝点击