SpringMvc Jpa mysql開發專案,事務失效,現象:Controller增加和修

zhaome發表於2010-03-16
收藏 不顯示刪除回覆顯示所有回覆顯示星級回覆顯示得分回覆 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>

[該貼被zhaome於2010-03-16 16:36修改過]

相關文章