springMVC之事务配置(问题来源:为什么数据保存不了)
一 问题来源
自己的亲身体会,来源问题this.sessionFactory.getCurrentSession().save(obj);保存不了数据。
二、解决方法
原因在springMVC配置事务时,事务注解应该在类扫描注解之后,然后在到相对应dao层的方法要加@Transaction
全注解配置如下:
第一步,首先看一下web.xml,如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>lei-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/lei-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>lei-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
第二步,spring-hibernate配置,见以下spring-hibernate.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean> <!-- 配置sessionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hiberante.format_sql">true</prop>
<prop key="current_session_context_class">thread</prop>
</props>
</property>
<property name="configLocations">
<list>
<value>
classpath*:hibernate.cfg.test.xml
</value>
</list>
</property>
</bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> </beans>
第三步:springMVC配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
"> <!-- 注解扫描包 -->
<context:component-scan base-package="com.example.*"/> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 引入注解类
下面两个都可以注释
-->
<mvc:annotation-driven/>
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> --> <!-- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> --> <!-- 文件上传配置 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="10485760000" />
<property name="maxInMemorySize" value="40960" />
</bean> <!-- 静态资源访问配置 -->
<mvc:resources location="/img/" mapping="/img/**"/>
<mvc:resources location="/topui/" mapping="/topui/**"/>
<!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
到这配置完毕,相对应的dao层如下:
package com.example.dao.impl; import java.util.List; import javax.annotation.Resource; import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional; import com.example.dao.IFilesDAO;
import com.example.entity.Files; @Repository
public class FilesDAO implements IFilesDAO{ @Resource
private SessionFactory sessionFactory; @SuppressWarnings("unchecked")
@Override
public List<Files> findFilesList(String id) {
// TODO Auto-generated method stub
return this.sessionFactory.getCurrentSession().createQuery("from Files where id='" + id+"'").list();
} @Transactional
@Override
public void deleteFile(Files file) {
this.sessionFactory.getCurrentSession().delete(file);
} @Transactional
@Override
public void updateFile(Files file) {
// TODO Auto-generated method stub
this.sessionFactory.getCurrentSession().update(file);
} @Transactional
@Override
public void saveFiles(Files file) {
// TODO Auto-generated method stub
System.out.println(file.getId());
this.sessionFactory.getCurrentSession().save(file);
System.out.println(file.getId());
} }
具体展示如下,其实spring事务配置还有其他配置方式。如需看可以访问《Spring事务配置的5种方法》
三 参考文章:
http://www.cnblogs.com/leiOOlei/p/3725911.html
springMVC之事务配置(问题来源:为什么数据保存不了)的更多相关文章
- springmvc aop 事务配置
对应的中文: 任意公共方法的执行: execution(public * *(..)) 任何一个以“set”开始的方法的执行: execution(* set*(..)) AccountService ...
- 使用SpringMVC+mybatis+事务控制+JSON 配置最简单WEB
最近在总结一些项目的基础知识,根据公司最近的一些意向和技术路线,初步整理了一个简单的配置例子 1.使用springmvc代替strutsMVC 2.使用请求json数据串的方式代替传统 ...
- SpringMVC对包的扫描范围扩大后,导致的事务配置不生效问题
问题场景 项目使用的框架:Spring 4.1.4 + Hibernate 4.3.8 + MySQL. web.xml中对Spring的配置: <!-- 把 Spring 容器集成到 Web ...
- springmvc+hibernate4事务管理配置
1.事务的特性 事务的四种特性: 原子性:体现一个事务的操作的不可分割,要么权执行,要么全不执行. 一致性:事务的执行结果必须从一种一致性状态变到另一种一致性状态.最典型的就是转账,两个账户A.B总金 ...
- springmvc学习笔记三:整合JDBC,简单案例==数据库事务配置(切面)
package cn.itcast.bean; import org.springframework.jdbc.core.PreparedStatementSetter; public class U ...
- ssm框架 pom的配置 / 还有里面springMVC.xml的配置 / webapp.xml的配置
首先是pom的配置: <dependencies> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-jav ...
- springmvc+mybatis事务回滚
spring-mybatis.xml中 配置了 <!-- 拦截器方式配置事物 --> <tx:advice id="transactionAdvice" tran ...
- Spring事务配置的五种方式(转载)
Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSo ...
- Spring事务配置的五种方式
Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSo ...
随机推荐
- iOS - UI - UITableView
1.UITableView 表格视图 服从数据源 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSIn ...
- Operation与GCD的不同
最大并发数: 什么是并发数? 同时执行的任务数.比如同时开启三个线程执行三个任务,并发数就是3. 最大并发数相关的方法: -(NSInteger)maxConcurrentOperationCount ...
- Java学习笔记——单例设计模式Singleton
单例设计模式:singleton 解决的问题: 确保程序在运行过程中,某个类的实例instance只有一份. 特点: 1 构造函数私有化 2 自己内部声明自己 3 提供一个public方法,负责实例化 ...
- [转]Webservice client timeout
本文转自:http://social.msdn.microsoft.com/Forums/vstudio/en-us/ed89ae3c-e5f8-401b-bcc7-333579a9f0fe/webs ...
- ASP.Net 验证控件 CompareValidator
CompareValidator 作用就是比较两个输入框所输入的值是否一致,常用来作为验证输入密码和确认密码的业务. 属性 描述 Operator 比较操作符,可选值DataTypeCheck.Equ ...
- 获取XML数据
http://www.w3school.com.cn/xml/xml_elements.asp <?xml version="1.0" encoding="gb23 ...
- html2canvas 踩坑总结
需求:将html表格导出为图片,表格可以自己编辑数据,并适配各种屏幕大小.上网搜了下,找到了html2canvas,一开始使用的是最新版0.5.0,最终因为需要支持自定义div编辑框自动换行选择了v0 ...
- js使用CSS将图片转换成黑白(灰色、置灰)
详细内容请点击 可能早就知道,像汶川这种糟糕的日子网站全灰在IE下是可以轻松实现的(filter: gray;),不过,当时,其他浏览器是无解的. 不过,时代发展,如今,CSS3的逐步推进,我们也开始 ...
- Android OpenGL ES(二)----平滑着色
直线或者三角形上的每个片段混合后的颜色可以用一个varying生成.我们不仅能混合颜色,还可以给varying传递任何值,OpenGL会选择属于那条直线的两个值,或者属于那个三角形的三个值,并平滑地在 ...
- Server.MapPath()获取绝对路径
1. Server.MapPath("/") 应用程序根目录所在的位置 如 C:\Inetpub\wwwroot\ 2.Server.MapPath("./&qu ...