struts1.x+spring2.5+JPA(hibernate)整合

1.在web.xml中加入如下配置

        <!-- 配置Struts ActionServlet -->
	<servlet>
		<servlet-name>ActionServlet</servlet-name>
		<servlet-class>
			org.apache.struts.action.ActionServlet
		</servlet-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>
				/WEB-INF/classes/struts-config.xml
			</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>ActionServlet</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

	<!-- Spring配置 指定spring配置文件位置-->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:ApplicationContext.xml</param-value>
	</context-param>


	<!-- 配置OpenEntityManagerInview -->
	<filter>
		<filter-name>openEntityManagerInView</filter-name>
		<filter-class>
			org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>openEntityManagerInView</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- 实例化Spring容器 -->
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>

2.Spring的配置文件ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">

	<!-- 启用支持Annotation注解方式的Bean管理 -->
	<context:component-scan base-package="com.kay"></context:component-scan>

	<!-- 启用支持Annotation注解方式的事务管理 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
	<!-- JPA事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory"
			ref="entityManagerFactory">
		</property>
	</bean>
	
	
	<!-- C3P0数据源配置 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver"/>
		<property name="jdbcUrl" value="jdbc:mysql:///jpa"/>
		<property name="maxIdleTime" value="30"/>
		<property name="properties">
			<props>
				<prop key="user">root</prop>
				<prop key="password">root</prop>
				<prop key="c3p0.acquire_increment">2</prop>
				<prop key="c3p0.max_size">20</prop>
				<prop key="c3p0.min_size">1</prop>
			</props>
		</property>
	</bean>
	
	<!-- 声明EntityManagerFactory -->
	<bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"></property>
		<property name="loadTimeWeaver">
			<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/> 
		</property>
	</bean>
</beans>

3.struts-config.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
	
	<form-beans></form-beans>
	<action-mappings>
		<action path="/user" parameter="method">
		</action>
	</action-mappings>
	<!-- 将Action交由Spring去管理 -->
	<controller		processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />
</struts-config>

4.jpa配置文件persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<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="myjpa" transaction-type="RESOURCE_LOCAL">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
  		<properties>
			<property name="hibernate.show_sql" value="true"/>
	  	</properties>
	</persistence-unit>
</persistence>

注意在DAO中获取EntityManager的方式:

package com.kay.dao;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import com.kay.pojo.User;

@Repository
public class UserDAO{

	@PersistenceContext
	private EntityManager manager;
	
	public EntityManager getManager()
	{
		return manager;
	}
	public User findById(int id)
	{
		return getManager().find(User.class, id);
	}
	public void add(User user)
	{
		getManager().persist(user);
		getManager().flush();
	}
}

在Service中加入事务支持:

package com.kay.service;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.kay.dao.UserDAO;
import com.kay.pojo.User;

@Service
@Transactional
public class UserService {

	@Resource
	private UserDAO userDAO;
	
	public void add(User user)
	{
		userDAO.add(user);
	}
	public User findById(int id)
	{
		return userDAO.findById(id);
	}

}
运行的容器为tomcat,并没有使用JBOSS等EJB容器,如果在DAO获取EntityManager或EntityManagerFactory的方式使用@Resource方式,
则会抛出javax.persistence.TransactionRequiredException: no transaction is in progress异常,所以各位必须注意EntityManager的获取方式。
 
 
posted @ 2009-06-16 13:15  樊凯  阅读(3324)  评论(0编辑  收藏  举报