麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

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

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

參考文章:http://www.companysz.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種方法》


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久久久久久久久久影视 | av黄色片网站 | 成人午夜一区二区 | 激情大乳女做爰办公室韩国 | 国产成人高潮免费观看精品 | 欧美成人一区二区视频 | 免费观看一区二区三区 | 免费国产羞羞网站视频 | 午夜视频你懂的 | 欧美亚洲啪啪 | 黄色特级一级片 | 日韩精品久久久久久 | 久久久日韩精品一区二区 | 激情综合在线观看 | 91精品国产91久久久久久蜜臀 | 高清做爰免费无遮网站挡 | 久久久久久久久久久久久九 | 91色琪琪电影亚洲精品久久 | 免费看污视频在线观看 | 亚洲极色 | 精品一区二区久久久 | 爱爱插插视频 | 最近高清无吗免费看 | 欧美一级淫片免费视频黄 | 日日狠狠久久偷偷四色综合免费 | 久久精品79国产精品 | 国产精品久久久久久模特 | a级高清免费毛片av在线 | 九九精品在线播放 | 成人在线国产 | 免费国产在线视频 | 免费观看一级黄色片 | 嫩呦国产一区二区三区av | 免费毛片免费看 | 黄色片网站在线免费观看 | 免费在线观看成人网 | 狼人狠狠干 | 久久久久久精 | 久久综合九色综合久久久精品综合 | 精品国产91一区二区三区 | 毛片在线免费观看视频 |