国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

springMVC之事務配置(問題來源:為什么數據保存不了)

2019-11-14 21:50:36
字體:
來源:轉載
供稿:網友
sPRingMVC之事務配置(問題來源:為什么數據保存不了)

參考文章:http://m.survivalescaperooms.com/leiOOlei/p/3725911.html

自己的親身體會,來源問題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="multjsp"></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;@Repositorypublic class FilesDAO implements IFilesDAO{@Resourceprivate SessionFactory sessionFactory;@SuppressWarnings("unchecked")@Overridepublic List<Files> findFilesList(String id) {// TODO Auto-generated method stubreturn this.sessionFactory.getCurrentSession().createQuery("from Files where id='" + id+"'").list();}@Transactional@Overridepublic void deleteFile(Files file) {this.sessionFactory.getCurrentSession().delete(file);}@Transactional@Overridepublic void updateFile(Files file) {// TODO Auto-generated method stubthis.sessionFactory.getCurrentSession().update(file);}@Transactional@Overridepublic void saveFiles(Files file) {// TODO Auto-generated method stubSystem.out.println(file.getId());this.sessionFactory.getCurrentSession().save(file);System.out.println(file.getId());}}

  具體展示如下,其實spring事務配置還有其他配置方式。如需看可以訪問《Spring事務配置的5種方法》


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东台市| 洛扎县| 惠安县| 景东| 兰坪| 来宾市| 那曲县| 临沭县| 长寿区| 嵊州市| 宜川县| 宝应县| 资溪县| 南漳县| 合川市| 五原县| 凤城市| 宣汉县| 那坡县| 肥城市| 四平市| 白银市| 南投县| 武强县| 得荣县| 英超| 那坡县| 哈密市| 兴和县| 汪清县| 丹江口市| 宜良县| 雷山县| 潼关县| 那曲县| 衡南县| 诸暨市| 驻马店市| 循化| 保康县| 华坪县|