读书人

Struts+Spring+JPA调整

发布时间: 2012-12-18 12:43:41 作者: rapoo

Struts+Spring+JPA整合

本文基于Struts1.3.10+Spring2.5.6+Hibernate3.3.2

一、下载相关jar

Struts:struts.apache.org

Spring:www.springsource.org/download

Hibernate:www.hibernate.org/downloads

?

二、搭建环境

1、struts需要的jar

(1)必需的jar

struts-core-1.3.10.jar,struts-extras-1.3.10.jar,struts-taglib-1.3.10.jar,antlr-2.7.2.jar,commons-beanutils-1.8.0.jar,commons-chain-1.2.jar,commons-digester-1.8.jar,commons-logging-1.0.4.jar

(2)文件上传需要的jar

commons-fileupload-1.1.1.jar,commons-io-1.1.jar

(3)验证时需要的jar

commons-validator-1.3.1.jar

2、Spring需要的jar

spring.jar,spring-webmvc-struts.jar

3、Hibernate需要的jar

(1)hibernate-distribution-3.3.2.GA-dist.zip中的以下jar

hibernate3.jar,lib\required文件夹下的所有jar(其中antlr-2.7.6.jar与struts中的antlr-2.7.2.jar冲突,建议选择高版本)

(2)hibernate-entitymanager-3.4.0.GA.zip中的以下jar

hibernate-entitymanager.jar,hibernate-annotations.jar,hibernate-commons-annotations.jar,ejb3-persistence.jar,lib/test/log4j.jar,lib/test/slf4j-log4j12.jar

?

三、在web.xml中对Spring容器和Struts进行实例化

?<listener>
??? ?<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
??? </listener>
?<context-param>
??<param-name>contextConfigLocation</param-name>
??<param-value>
???classpath*:\config\**\*applicationContext.xml,classpath*:\config\**\*applicationContext-*.xml,classpath*:**\applicationContext.xml,classpath*:**\*-applicationContext-*.xml
??</param-value>
?</context-param>?

?

?<servlet>
??<servlet-name>struts</servlet-name>
??<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
??<init-param>
???<param-name>config</param-name>
???<param-value>/WEB-INF/struts-config.xml,/WEB-INF/struts-config-test.xml</param-value>
??</init-param>
?</servlet>
?<servlet-mapping>
??<servlet-name>struts</servlet-name>
??<url-pattern>*.do</url-pattern>
?</servlet-mapping>

?

四、配置persistence.xml?

JPA规范要求在类路径的META-INF目录下放置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="yourUnitName" transaction-type="RESOURCE_LOCAL">
????? <properties>
???????? <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
???????? <property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
???????? <property name="hibernate.connection.username" value="root"/>
???????? <property name="hibernate.connection.password" value="123456"/>
???????? <property name="hibernate.connection.url" value="jdbc:mysql://{database_address}?useUnicode=true&characterEncoding=UTF-8"/>
???????? <property name="hibernate.max_fetch_depth" value="3"/>
???????? <property name="hibernate.hbm2ddl.auto" value="update"/>
????? </properties>
? </persistence-unit>
</persistence>?

?

五、在spring中配置EntityManagerFactory,配置模板如下:

<?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"
?????? 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/aop

?????????? http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
?????????? http://www.springframework.org/schema/tx

?????????? http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

?

???<!--使用扫描注解的方式管理bean-->

?? <context:component-scan base-package="packageName"/>?

?

?? <bean id="entityManagerFactory"??

????????????? value="yourUnitName"/>

?? </bean>??
?? <bean id="transactionManager" ref="entityManagerFactory"/>
?? </bean>

?

?? <!--使用注解方式管理事务-->
???<tx:annotation-driven transaction-manager="transactionManager"/>
?
</beans>

?

六、使用spring解决hibernate因session关闭导致延迟加载例外的问题。放在Web.xml文件中即可。

<filter>
???<filter-name>OpenSessionInViewFilter</filter-name>
?? <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
??????? <filter-name>OpenSessionInViewFilter</filter-name>
??????? <url-pattern>/*</url-pattern>
</filter-mapping>

?

七、persistence.xml相关配置参数的解释(网络总结)
<?xml version="1.0" encoding="UTF-8"?>???
<persistence version="1.0"??
??? xmlns:persistence="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 persistence_1_0.xsd ">???
???

<!--???
????? Name属性用于定义持久化单元的名字 (name必选,空值也合法);???
????? transaction-type 指定事务类型(可选)???
-->??
<persistence-unit name="unitName" transaction-type="JTA">??
??
?? <!-- 描述信息(可选) -->??
?? <description> </description>??
??
?? <!-- javax.persistence.PersistenceProvider接口的一个实现类(可选) -->??
?? <provider></provider>??
??
?? <!-- Jta-data-source和non-jta-data-source用于分别指定持久化提供商使用的JTA和/或non-JTA数据源的全局JNDI名称(可选) -->??
?? <jta-data-source>java:/MySqlDS</jta-data-source>??
?? <non-jta-data-source> </non-jta-data-source>??
??
?? <!-- 声明orm.xml所在位置.(可选) -->??
?? <mapping-file>product.xml</mapping-file>??
??
?? <!-- 以包含persistence.xml的jar文件为基准的相对路径,添加额外的jar文件.(可选) -->??
?? <jar-file>../lib/model.jar</jar-file>??
??
?? <!-- 显式列出实体类,在Java SE 环境中应该显式列出.(可选) -->??
?? <class>com.domain.User</class>??
?? <class>com.domain.Product</class>??
??
?? <!-- 声明是否扫描jar文件中标注了@Enity类加入到上下文.若不扫描,则如下:(可选) -->??
?? <exclude-unlisted-classes/>??
??
?? <!--??? 厂商专有属性(可选)??? -->??
?? <properties>???
?????? <!-- hibernate.hbm2ddl.auto= create-drop / create / update -->??
?????? <property name="hibernate.hbm2ddl.auto" value="update" />???
???????<property name="hibernate.show_sql" value="true" />??
?? </properties>??
??
</persistence-unit>???
??
通常在企业开发中,有两种做法:

1.先建数据库,后再根据数据库来编写配置文件和实体bean。使用这种方案的开发人员受到了传统数据库建模的影响。
2.先编写配置文件和实体bean,然后再生成数据库,使用这种方案的开发人员采用的是领域建模思想,这种思想相对前一种思想更加OOP。

????? 建议使用第二种(领域建模思想),从软件开发来想,这种思想比第一种思想更加面向对象。 领域建模思想也是目前比较新的一门建模思想,第一种是传统的建模思想,已经有10来年的发展历程了,而领域建模思想是近几年才兴起的,这种思想更加的面向对象。

?

读书人网 >编程

热点推荐