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
- SpringMVC框架中添加事务
- springmvc中验证框架
- springMVC框架,后台中session添加用户信息和获取用户信息的问题
- springmvc 项目添加 Junit 测试框架
- SpringMVC配置事务的5中方式
- 在springMVC中添加过滤器
- SpringMVC中handlerInterceptor的添加
- IntelliJ IDEA创建Web SpringMVC项目(二):添加springMVC框架
- IntelliJ IDEA创建Web SpringMVC项目(二):添加springMVC框架
- springmvc中如何验证框架
- springmvc框架中实现全选
- 用 JOTM 向Servlet中添加事务
- ssm框架下正常添加事务的演示
- SHS框架中声明事务的代码
- SpringMVC中添加Interceptor拦截器
- SpringMVC中添加Velocity的配置
- SpringMVC中添加HTML等页面视图
- 事务添加
- [DP]科技庄园
- meta标签属性
- 【已解决】HQL通过类来查询其他类
- Gray Code
- mvc HTMl.Partial,Action,RenderPartial,RenderAction区别与传参
- SpringMVC框架中添加事务
- Android 从相机获取图片,存到指定文件进行编辑,bitmap总是为空
- C++
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
- java中对于面向对象的理解
- numpy matrix 矩阵对象
- TP3.2.3常量定义---自己寻找的
- 控制table的td自动换行
- mysql using filesort