Struts2-Spring3-Hibernate3整合开发

本文详细介绍了如何将Struts2、Spring3及Hibernate3三大框架进行整合,包括各框架的导入配置、数据库连接设置、Hibernate映射等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Struts2-Spring3-Hibernate3整合开发

夹杂个人整合习惯

首先导入数据库驱动jar,如图:


导入我们的flyingstudio.jar包(这个包里有sqlhql方法)如图



一.导入Struts2

项目的lib下的5Struts2核心jar

Aspectjweaver.jar

javassist-3.7.ga.jar

。。。。。(几个关键包,如果你不知道导入什么包,当启动tomcat的时候会报错Java.lang.Nosuch.........这个提示你缺少什么方法,从而你可以知道自己还差了什么包,导入到lib文件夹就行了)

导入到工程下的lib文件夹下即可(复制粘贴就行)

web.xml中添加

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

如图:

在目录下新建一个包,名字可以命名为struts(每次的struts配置文件都放在这里),目录下新建一个struts2-StudentLogin.xml(这个配置文件是学生登录界面的struts2配置文件)的配置文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="studentLogin" namespace="/studentLogin" extends="struts-default">
<action name="studentLoginAction_*" class="studentLoginAction">
<result name="login">/StudentLogin.jsp</result>
<result name="index">/index.jsp</result>
</action>
</package>
</struts>

可以新建一个struts.xml综合配置文件(这个是用来综合管理struts2配置文件的,是为了防止配置文件过多而导致混乱分不清楚,每次在struts2包下添加一个struts配置文件的时候都要在这里加一行代码)如图(位置随意,方便即可):


内容如下:

 

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

<struts>
	<constant name="struts.ui.theme" value="simple"></constant>
	<constant name="struts.devMode" value="true"></constant>
	<constant name="struts.objectFactory" value="spring"></constant>
	
	<constant name="struts.action.extension" value="do"/>
	
	<constant name="struts.locale" value="UTF-8"></constant>
	<constant name="struts.i18n.encoding" value="UTF-8"></constant>
	<!--struts2-->
	<include file="struts2/struts2-StudentLogin.xml"></include>
	
	</struts>

如图:

其中:<include file="struts2/struts2-StudentLogin.xml"></include>,这语句是对

struts2目录下的struts2-StudentLogin.xml的配置

二.spring3的导入

导入下面几个包(按照你要的功能添加jar包,闲麻烦可以都倒入了):

 Commons-logging.jar

Spring.jar

放到lib文件夹下面即可

 

其他的包酌情添加

 

web.xml配置文件中输入:


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

如图:

在目录下新建一个spring包(为了方便管理相关spring文件),在spring包下新建几个配置:


applicationContext-action.xml 

applicationContext-common.xml 

applicationContext-hibernate-common.xml

applicationContext-hibernate-manager.xml

applicationContext-jdbc-common.xml

applicationContext-hql-configure.xml  //这里用hql不用sql

文件如图:


applicationContext-action.xml 中加入代码(这段代码是为了对action进行管理,每次添加完action之后都要进行添加):

<bean id="studentLoginAction" class="com.online.PFCMH.action.StudentLoginAction"
		scope="prototype" autowire="byName" />

如图:


applicationContext-common.xml(对数据库的一个管理)添加代码如下:

<bean id="queryProvider" class="org.onlineframework.commons.ListMapQueryProvider" autowire="byName">
		<property name="providers">
			<list>
				<ref bean="queryProviderMapping-user-sql"/>
		
			</list>
		</property>
	</bean>

每次添加hql之后都要在这里进行里list的添加,添加后如图:




applicationContext-hibernate-common.xml(对session生成的一个管理)配置文件代码输入:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
	default-autowire="byName" default-lazy-init="true">
		
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation">
			<value>classpath:hibernate.cfg.xml</value>
		</property>
	</bean>
	
	<!-- Transaction manager for a single Hibernate SessionFactory
		(alternative to JTA) -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<tx:advice id="txAdvice" transaction-manager="transactionManager">

    <!-- the transactional semantics... -->
    <tx:attributes>

      <!-- all methods starting with 'get' are read-only -->
      <tx:method name="get*" read-only="true"/>
      <tx:method name="find*" read-only="true"/>

      <!-- other methods use the default transaction settings (see below) -->
      <tx:method name="*"/>
    </tx:attributes>
  </tx:advice>

	<aop:config>
		<aop:pointcut id="serviceMethods" expression="execution(* com.online.PFCMH2.service..*(..))"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>
	</aop:config>
</beans>  

如图:

applicationContext-hibernate-manager.xml添加代码:

<bean id="studentManager" class="com.online.PFCMH.service.impl.StudentManager" autowire="byName">
    </bean>

每次添加一个Manager的时候都要进行添加,如图:


这个文件是为了如图:


applicationContext-jdbc-common.xml添加代码:

<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:db-conf.properties</value>
			</list>
		</property>
		<property name="fileEncoding" value="UTF-8" />
		<property name="placeholderPrefix" value="$[" />
		<property name="placeholderSuffix" value="]" />
	</bean>
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>$[db.driver]</value>
		</property>
		<property name="url">
			<value>$[db.url]</value>
		</property>
		<property name="username">
			<value>$[db.username]</value>
		</property>
		<property name="password">
			<value>$[db.password]</value>
		</property>
	</bean>

这是数据库连接文件信息配置如图:


applicationContext-sql-configure.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: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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
	default-autowire="byName" default-lazy-init="true">
	<!-- query provider wrapper -->
	<bean id="queryProviderMapping-user-hql" class="org.onlineframework.commons.QueryProviderMappingImpl">
		 <property name="queryMap">
			<map>
			   <entry key="UserManager.findUserByUserNP.query"><value><![CDATA[
				from Student where 1=1
				<#if sName?has_content>
					and s_name =:sName
				</#if>		
				<#if sPwd?has_content>
					and s_password =:sPwd
				</#if> 	
			]]></value></entry>
			 
			</map>			
		</property> 
	</bean>		
</beans>

即添加的是hql查询语句,如图:

在目录下新建一个db-conf.properties,然后添加代码:

db.username=root		
db.password=00000000
db.url=jdbc:mysql://localhost:3306/pfcmh?characterEncoding=UTF-8
db.driver=com.mysql.jdbc.Driver

即将数据库名以及用户名和密码放到这里进行统一管理:

三.搭建hibernate

开始跟前面差不多,都是倒入包

hibernate3.jar

      ejb3-persistence.jar

      antlr-2.7.6.jar

      commons-collections-3.1.jar

      dom4j-1.6.1.jar

      javassist-3.12.0.GA.jar

      jta-1.1.jar

      slf4j-api-1.6.1.jar

放到lib文件夹下面即可

MyEclipse Hibernate中进行数据库的映射,现在目录下新建一个存放映射model的包,如图:


然后切换到MyEclipse Hibernate的界面,如图:




在左侧右击“new”并填写:


 

然后finish即可,然后显示如图:


然后按下图步骤;


我们选择hibernate3.1,next,如图:


填好路径后next

 

 

找到你要放的目录,然后 next 如图:


然后finish


填写

 

然后回到MyEclipse Hibernate的界面,对每一个表右击选择Hibernate Reverse Engeering如图:


然后选择放置路径:


勾选两个选项,如图:


MyEclipse的工作界面就会出现:


Hibernate.cfg.xml中的内容就会就会出现

<mapping resource="com/online/PFCMH/model/Smessage.hbm.xml" />


对每一个数据库表进行上述的映射操作即可完成Hibernate的搭建










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值