Struts 2.1.8.1+spring 3.0.2+hibernate 3.5.0

关于整合工程(s2shstudy):

a demo of study project which can be used direct when needed. this is a project of struts2
, hibernate,spring.used hibernate annotation,use spring context test: there we use junit4.8.2,myEclipse junit4.4 is not useable。用到了spring和hibernate的annotation,struts2的实现用配置文件。

用到的jar包:
Struts 2.1.8.1 libraries
spring 3.0.2 libraries (copy all file in dist),add aopalliance.jar,aspectjrt.jar,aspectjweaver.jar
hibernate 3.5.0 libraries:required,hibernate3,jpa,cblib
add ajax used:dwr 2.0 which also used in my project s1shstudy
dbbase is mysql with jar: mysql-connector-java-5.0.8-bin.jar
hibernate log:slf4j-log4j12-1.5.8.jar(load),log4j-1.2.14.jar,slf4j-api-1.5.8.jar,cglib-2.2(aop)

说明:

一些包,在我从官网上下的库中并没有,必须自己下载加入。

useful note:
model pachage can be product by myEclipse IDE,may be config xml and dao,but you need to change them be useable.

配置文件:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- spring监听器,监听所有初始化spring容器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<welcome-file-list>
<welcome-file>/register/register.jsp</welcome-file>
</welcome-file-list>
<!-- struts2 filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 添加DWRServlet的映射 -->
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>
org.directwebremoting.servlet.DwrServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>classes</param-name>
<param-value>java.lang.Object</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>

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"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 定义使用annotation -->
<context:annotation-config />
<!-- sessionFactory定义 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
<property name="packagesToScan">
<list>
<value>cn.jiabeis.s2shstudy.model</value>
</list>
</property>
</bean>
<!-- 事务定义 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!--- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>

<!-- 切面配置 (那些类的哪些方法参与事务) -->
<aop:config>
<aop:pointcut
expression="execution(* cn.jiabeis.s2shstudy.service..*.*(..))"
id="transactionPointcut" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="transactionPointcut" />
</aop:config>

<!-- 让Spring通过自动扫描来查询和管理Bean -->
<context:component-scan base-package="cn.jiabeis.s2shstudy" />
<!-- 定义hibernateTemplate -->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 测试dwr-ajax -->
<bean id="testService"
class="cn.jiabeis.s2shstudy.service.impl.TestServiceImpl">
</bean>
</beans>

hibernate.cfg.xml:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/demo
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>

struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- action后缀改为do
<constant name="struts.action.extension" value="do"/>
-->
<!-- 和spring集成 -->
<constant name="struts.objectFactory" value="spring"/>
<!-- 默认本地化 -->
<constant name="struts.loacl" value="zh_CN" />
<!-- 默认的国家化编码,解决中文乱码 -->
<constant name="struts.il8n.encoding" value="UTF-8" />
<!-- 配置上传文件体积最大值10Mb -->
<constant name="struts.multipart.maxSize" value="10485760" />
<package name="default" namespace="/" extends="struts-default">
<global-results>
<result name="error">/error.jsp</result>
<result name="success">/main.jsp</result>
</global-results>
</package>
<include file="struts-default.xml"/>
<include file="struts-action.xml"/>
</struts>

struts-action.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

<package name="web" extends="default">
<!-- 学生管理 -->
<action name="studentAction" class="cn.jiabeis.s2shstudy.action.StudentAction" method="execute">
<result name="success">
/success.jsp
</result>
<result name="error">
/error.jsp
</result>
</action>
<!-- 用户注册 -->
<action name="registerAction" class="registerAction" method="execute">
<result name="success">
/success.jsp
</result>
<result name="error">
/error.jsp
</result>
</action>
</package>
</struts>

注意:容易错的就是一些jar包和配置文件,置于annotation的实现,这里没有列举。

你可能感兴趣的:(Hibernate)