整合原理
整合步骤
1.添加struts框架
struts所需jar包:
2.添加spring框架
spring所需jar包:
3.添加hibernate框架
4.目录结构
添加完框架的目录结构
整合项目的目录结构
5.hibernate.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="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:orcl
</property>
<property name="connection.username">Scott</property>
<property name="connection.password">tiger</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="myeclipse.connection.profile">SCOTT</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="pojo/Project.hbm.xml" />
<mapping resource="pojo/Vote.hbm.xml" />
<mapping resource="pojo/Tuser.hbm.xml" />
</session-factory>
</hibernate-configuration>6.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- 装配HibernateTemplate -->
<bean id="hibernateTemplateId" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="tuserDao" class="dao.impl.TuserDao">
<property name="hibernateTemplate" ref="hibernateTemplateId"></property>
</bean>
<bean id="voteDao" class="dao.impl.VoteDao">
<property name="hibernateTemplate" ref="hibernateTemplateId"></property>
</bean>
<bean id="pojectDao" class="dao.impl.ProjectDao">
<property name="hibernateTemplate" ref="hibernateTemplateId"></property>
</bean>
</beans>7.struts.xml配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="false" />
<constant name="struts.i18n.encoding" value="UTF-8" />
<package name="sysPack" extends="struts-default" namespace="/system">
<action name="tuserAction" class="action.TuserAction">
<result name="adminLogin" type="redirect">/admin.jsp</result>
<result name="userLogin" type="redirect">/index.jsp</result>
<result name="loginFail" type="redirect">/loginFail.jsp</result>
<result name="register" type="redirect">/index.jsp</result>
<result name="exit" type="redirect">/index.jsp</result>
</action>
<action name="voteAction" class="action.VoteAction">
<result name="selectList">/adminVoteList.jsp</result>
<result name="updateVote">/updateVote.jsp</result>
<result name="execute">/index.jsp</result>
<result name="userSelectList">/userVoteList.jsp</result>
</action>
<action name="projectAction" class="action.ProjectAction">
<result name="selectList">/adminProjectList.jsp</result>
<result name="addProject">/addProject.jsp</result>
<result name="userSelectList">/userProjectList.jsp</result>
</action>
</package>
</struts>
8.代码实现
TuserDao中的部分代码展示
package dao.impl;
import hibernate.HibernateSessionFactory;
import java.util.Date;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import pojo.Tuser;
import dao.inf.TuserDaoInf;
public class TuserDao extends HibernateDaoSupport implements TuserDaoInf {
@Override
public void addUser(Tuser tuser) {
Session session = HibernateSessionFactory.getSession();
Transaction transaction = session.beginTransaction();
session.save(tuser);
transaction.commit();
}
@Override
public Tuser selectUser(Tuser tuser) {
Session session = HibernateSessionFactory.getSession();
String hql = "from Tuser where name = '" + tuser.getName() + "'";
Query query = session.createQuery(hql);
Tuser tuser2 = (Tuser) query.uniqueResult();
session.close();
return tuser2;
}
@Override
public void deleteUser(Tuser tuser) {
}
@Override
public void updateUser(Tuser tuser) {
}
}
说明:这种整合方法简单易懂,适合初学者理解SSH整合中,各个框架在项目中起到的作用
本文介绍了一个简单的SSH(Struts-Spring-Hibernate)框架整合案例,包括各组件的配置过程及核心代码示例,有助于初学者理解各框架在项目中的作用。

1万+

被折叠的 条评论
为什么被折叠?



