SpringMvc Jpa mysql開發專案,事務失效,現象:Controller增加和修
收藏 不顯示刪除回覆顯示所有回覆顯示星級回覆顯示得分回覆 SpringMvc Jpa mysql開發專案,事務失效,現象:Controller增加和修改無效,但單元測試可以,不知道什麼原因,程式碼如下:
各位高手幫忙解決一下啊,謝謝了。
實體類:
@Entity
public class Customer implements Serializable{
private static final long serialVersionUID = -803075682525328610L;
private Integer customerId;
private String loginName;
private String password="123456";
private String nickName;
private String email;
private String cellphone;
@Id @GeneratedValue
public Integer getCustomerId() {
return customerId;
}
}
DAO:
public interface DAO<T> {
public void save(Object entity);
}
DAO抽象類:
@Transactional
public abstract class DaoSupport<T> implements DAO<T> {
protected Class<T> entityClass = GenericsUtils.getSuperClassGenricType(this.getClass());
@PersistenceContext protected EntityManager em;
public void save(Object entity) {
em.persist(entity);
}
}
Service層:
public interface CustomerService extends DAO<Customer>{
}
@Service
@Transactional
public class CustomerServiceBean extends DaoSupport<Customer> implements CustomerService {
}
Controller層:
@Controller
public class ModiPersonDataController {
@Resource CustomerService customerService;
@RequestMapping("/modiPersonDataUI.do")
public String jumpToModiUI() {
return "modiPersonData";
}
/**
* 修改個人資料和密碼
*/
@RequestMapping("/modiPersonData.do")
public void modiPersonData(CustomerForm customerForm){
Customer customer = new Customer();
customer.setLoginName(customerForm.getLoginName());
customer.setPassword(customerForm.getPassword());
customer.setNickName(customerForm.getNickName());
customer.setCellphone(customerForm.getCellphone());
customer.setEmail(customerForm.getEmail());
customerService.save(customer);
}
}
webform層:
public class CustomerForm implements Serializable{
private static final long serialVersionUID = -4425312158083346598L;
private Integer customerId;
private String loginName;
private String password;
private String nickName;
private String cellphone;
private String email;
public String getLoginName() {
return loginName;
}
。。。
}
persistence.xml 配置如下:
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="leo" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.jdbc.fetch_size" value="18"/>
<property name="hibernate.jdbc.batch_size" value="10"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="false"/>
</properties>
</persistence-unit>
</persistence>
beans.xml配置如下:
<aop:aspectj-autoproxy/>
<context:component-scan base-package="cn.kuaishang"/>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<property name="initialSize" value="${initialSize}"/>
<property name="maxActive" value="${maxActive}"/>
<property name="maxIdle" value="${maxIdle}"/>
<property name="minIdle" value="${minIdle}"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
jdbc.properties)(略)
spring mvc 配置如下:
<!-- ①:對web包中的所有類進行掃描,以完成Bean建立和自動依賴注入的功能 -->
<context:component-scan base-package="cn.kuaishang"/>
<!-- ②:啟動Spring MVC的註解功能,完成請求和註解POJO的對映 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- ③:對模型檢視名稱的解析,即在模型檢視名稱新增前字尾 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
web.xml 配置如下:
<display-name>Spring Annotation MVC Sample</display-name>
<!-- Spring 服務層的配置檔案 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- Spring 容器啟動監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- Spring MVC 的Servlet,它將載入WEB-INF/annomvc-servlet.xml 的
配置檔案,以啟動Spring MVC模組-->
<servlet>
<servlet-name>annomvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>annomvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
各位高手幫忙解決一下啊,謝謝了。
實體類:
@Entity
public class Customer implements Serializable{
private static final long serialVersionUID = -803075682525328610L;
private Integer customerId;
private String loginName;
private String password="123456";
private String nickName;
private String email;
private String cellphone;
@Id @GeneratedValue
public Integer getCustomerId() {
return customerId;
}
}
DAO:
public interface DAO<T> {
public void save(Object entity);
}
DAO抽象類:
@Transactional
public abstract class DaoSupport<T> implements DAO<T> {
protected Class<T> entityClass = GenericsUtils.getSuperClassGenricType(this.getClass());
@PersistenceContext protected EntityManager em;
public void save(Object entity) {
em.persist(entity);
}
}
Service層:
public interface CustomerService extends DAO<Customer>{
}
@Service
@Transactional
public class CustomerServiceBean extends DaoSupport<Customer> implements CustomerService {
}
Controller層:
@Controller
public class ModiPersonDataController {
@Resource CustomerService customerService;
@RequestMapping("/modiPersonDataUI.do")
public String jumpToModiUI() {
return "modiPersonData";
}
/**
* 修改個人資料和密碼
*/
@RequestMapping("/modiPersonData.do")
public void modiPersonData(CustomerForm customerForm){
Customer customer = new Customer();
customer.setLoginName(customerForm.getLoginName());
customer.setPassword(customerForm.getPassword());
customer.setNickName(customerForm.getNickName());
customer.setCellphone(customerForm.getCellphone());
customer.setEmail(customerForm.getEmail());
customerService.save(customer);
}
}
webform層:
public class CustomerForm implements Serializable{
private static final long serialVersionUID = -4425312158083346598L;
private Integer customerId;
private String loginName;
private String password;
private String nickName;
private String cellphone;
private String email;
public String getLoginName() {
return loginName;
}
。。。
}
persistence.xml 配置如下:
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="leo" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.jdbc.fetch_size" value="18"/>
<property name="hibernate.jdbc.batch_size" value="10"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="false"/>
</properties>
</persistence-unit>
</persistence>
beans.xml配置如下:
<aop:aspectj-autoproxy/>
<context:component-scan base-package="cn.kuaishang"/>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<property name="initialSize" value="${initialSize}"/>
<property name="maxActive" value="${maxActive}"/>
<property name="maxIdle" value="${maxIdle}"/>
<property name="minIdle" value="${minIdle}"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
jdbc.properties)(略)
spring mvc 配置如下:
<!-- ①:對web包中的所有類進行掃描,以完成Bean建立和自動依賴注入的功能 -->
<context:component-scan base-package="cn.kuaishang"/>
<!-- ②:啟動Spring MVC的註解功能,完成請求和註解POJO的對映 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- ③:對模型檢視名稱的解析,即在模型檢視名稱新增前字尾 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
web.xml 配置如下:
<display-name>Spring Annotation MVC Sample</display-name>
<!-- Spring 服務層的配置檔案 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- Spring 容器啟動監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- Spring MVC 的Servlet,它將載入WEB-INF/annomvc-servlet.xml 的
配置檔案,以啟動Spring MVC模組-->
<servlet>
<servlet-name>annomvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>annomvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
[該貼被zhaome於2010-03-16 16:36修改過]
相關文章
- springboot專案-宣告式事務失效Spring Boot
- SpringMVC【開發Controller】詳解SpringMVCController
- 專案中Spring事務失效的場景問題排查Spring
- 降魔篇之springmvc專案開發SpringMVC
- 分散式事務(4)---RocketMQ實現分散式事務專案分散式MQ
- MySQL 事務和鎖MySql
- MySQL事務和鎖MySql
- 事務 - 失效的場景
- MySQL實現事務的提交和回滾MySql
- MySQL事務實現原理MySql
- MySQL事務與併發MySql
- mysql事務和鎖InnoDBMySql
- MySQL 的索引和事務MySql索引
- 遊戲開發專案管理那些事遊戲開發專案管理
- Springboot事務失效原因Spring Boot
- RESOURCE_LOCAL JPA 事務型別型別
- Rill:Go語言中併發+事務的批處理開源專案Go
- MySQL innodb 事務的實現MySql
- Spirng+SpringMVC+Maven+Mybatis+MySQL專案搭建SpringMVCMavenMyBatisMySql
- IDEA Maven專案開發注意事項IdeaMaven
- MySQL中的事務和MVCCMySqlMVC
- 基於springmvc+spring-data-jpa+dubbo開發web應用SpringMVCWeb
- Spring Data JPA中事務ReactiveTransactionManagerSpringReact
- Spring事務專題(三)事務的基本概念,Mysql事務處理原理SpringMySql
- 騰訊雲TDSQL MySQL版 - 開發指南 分散式事務MySql分散式
- 【MySQL】MySQL(三)儲存過程和函式、觸發器、事務MySql儲存過程函式觸發器
- 象慕世界卷軸零擼系統專案開發詳情
- 為開源專案 go-gin-api 增加後臺任務模組GoAPI
- 如何實現跨Mysql、Redis和Mongo分散式事務? - dongfuMySqlRedisGo分散式
- SpringMVC、MyBatis 宣告式事務管理SpringMVCMyBatis
- Oracle開發專題之:對事務的一些總結和理解Oracle
- 商務網站專案開發外包網站
- 軟體工程—思考專案開發那些事(一)軟體工程
- mysql 事務MySql
- mysql事務MySql
- MySQL基礎架構和事務MySql架構
- Kotlin + SpringBoot + JPA 服務端開發KotlinSpring Boot服務端
- MySQl事務建立,開始以及提交MySql