`
lizhenbin2010
  • 浏览: 99644 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

集成hibernate+spring+struts2框架

 
阅读更多

      从大学的时候就开始用SSH(hibernate+spring+struts2)开发,但是始终没有整理出怎么集成这个框架的,最近所做的网站不是很忙,抽个时间写了出来,也是当学习怎么样搭建,温故而知新吧。集成SSH当然少不了去下载他们各自的jar包了,我的jar包列表在附件的图片中,当然有部分包是不需要的,我为了简便就全部加进来,有时间的可以慢慢调试,看看少什么包,自己在相应的加就行了。

      当然这个只是一个很简单的demo。

      SSH集成,我的方法是先Hibernate连接数据调试通了,在集成Spring,Hibernate连接数据库这里就不做分析了,很简单加入相应的包,配置一下就行。Hibernate集成Spring就是把hibernate的相应配置文件交给Spring处理,或者托管之类的。我的在applicationContext.xml配置文件托管配置如下:

<!-- 配置dbcp形式的数据连接池 -->  
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
	<property name="driverClassName" value="com.mysql.jdbc.Driver"/>  
	<property name="url" value="jdbc:mysql://localhost:3306/SSH"/>  
	<property name="username" value="root"/>  
	<property name="password" value="123456"/>	  	  
 </bean>
<!-- 配置Hibernate托管到Spring中 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
 	<property name="dataSource">  
 		<ref bean="dataSource" />  
	</property>  
	<property name="hibernateProperties">  
		<props>  
			<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
			<prop key="hibernate.show_sql">true</prop>
			<prop key="hibernate.format_sql">true</prop>  
        		</props>  
	   </property>
	  <property name="mappingResources">  
		<list> 
			<value>com/lzb/hibernate/entity/xml/TUser.hbm.xml</value>  
		</list>  
	</property>  
</bean>

    上面那样基本就集成了hibernate和spring,以为hibernate和spring并不一定要在web工程中使用,所以这里没有必要配置web.xml。而要是集成SSH,一般都是用于web工程,当然就有需要配置web.xml了。

    接下来就要介绍集成SSH了,集成struts和加入spring的监听器。

 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext*.xml</param-value>
 </context-param>
 
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 <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>

   这样就集成SSH了。

   说到这里,我将我的demo,集成SSH模拟用户登录展示出来。
   POJO

package com.lzb.hibernate.entity;

import java.util.Date;

public class TUser {
	
	private int userID;
	private String userName;
	private String passWord;
	private Date birthday;
                 // get/set		
}

 

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
	<class name="com.lzb.hibernate.entity.TUser" table="t_user">
		<id name="userID" column="id">
			<generator class="native"></generator>
		</id>
		<property name="userName" column="username"></property>
		<property name="passWord" column="password"></property>
		<property name="birthday" column="birthday"></property>
	</class>	
</hibernate-mapping>

 

Dao

package com.lzb.hibernate.dao;

import java.util.List;

import com.lzb.hibernate.entity.TUser;

public interface TUserDao {
	
	public void insertUser(TUser user);
	
	public List<TUser> findAllUsers();

}

 

DaoImpl

package com.lzb.hibernate.dao.impl;

import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.lzb.hibernate.dao.TUserDao;
import com.lzb.hibernate.entity.TUser;

public class TUserDaoImpl extends HibernateDaoSupport implements TUserDao {

	public List<TUser> findAllUsers() {
		String hql = "FROM TUser";
		return this.getHibernateTemplate().find(hql);
	}

	public void insertUser(TUser user) {
		this.getHibernateTemplate().save(user);
	}

}

 

Service

package com.lzb.hibernate.service;

import java.util.List;

import com.lzb.hibernate.entity.TUser;

public interface TUserService {
	
public void insertUser(TUser user);
	
	public List<TUser> findAllUsers();


}

 

ServiceImpl

package com.lzb.hibernate.service.impl;

import java.util.List;

import com.lzb.hibernate.dao.TUserDao;
import com.lzb.hibernate.dao.impl.TUserDaoImpl;
import com.lzb.hibernate.entity.TUser;
import com.lzb.hibernate.service.TUserService;

public class TUserServiceImpl implements TUserService {
	
	private TUserDao tuserDao = new TUserDaoImpl();

	public TUserDao getTuserDao() {
		return tuserDao;
	}

	public void setTuserDao(TUserDao tuserDao) {
		this.tuserDao = tuserDao;
	}

	public List<TUser> findAllUsers() {
		// TODO Auto-generated method stub
		return this.tuserDao.findAllUsers();
	}

	public void insertUser(TUser user) {
		// TODO Auto-generated method stub
		this.tuserDao.insertUser(user);
	}

}

 

Action

package com.lzb.hibernate.action;

import java.util.List;

import com.lzb.hibernate.entity.TUser;
import com.lzb.hibernate.service.TUserService;
import com.opensymphony.xwork2.ActionSupport;

public class TUserAction extends ActionSupport {
	
	private TUser user;
	private List<TUser> userList;
	private TUserService tuserServce;
	
	public TUser getUser() {
		return user;
	}
	public void setUser(TUser user) {
		this.user = user;
	}
	public List<TUser> getUserList() {
		return userList;
	}
	public void setUserList(List<TUser> userList) {
		this.userList = userList;
	}
	public TUserService getTuserServce() {
		return tuserServce;
	}
	public void setTuserServce(TUserService tuserServce) {
		this.tuserServce = tuserServce;
	}
	
	public String userLogin() {
		
		System.out.println("===============>JSP UserName: "+this.user.getUserName());
		System.out.println("===============>JSP PassWord: "+this.user.getPassWord());
		List<TUser> tempUser = this.tuserServce.findAllUsers();
		for(TUser user : tempUser) {
			if(user.getUserName().equals(this.user.getUserName()) && user.getPassWord().equals(this.user.getPassWord())) {
				return "success";
			}
		}
		return "input";
	}
}

 

Structs.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>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />
<!--  
    <package name="default" namespace="/" extends="struts-default">
        <default-action-ref name="index" />
        <global-results>
            <result name="error">/error.jsp</result>
        </global-results>
        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>
        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package>
    <include file="example.xml"/>
-->
	<package name="default" namespace="/" extends="struts-default">
		<action name="user_userlogin" method="userLogin" class="tuserAction">
			<result name="success">/main.jsp</result>
			<result name="input">/index.jsp</result>
		</action>
	</package>
  

</struts>

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext*.xml</param-value>
 </context-param>
 
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 <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>
 
 <welcome-file-list>
 	<welcome-file>/index.jsp</welcome-file>
 </welcome-file-list>
 
 
</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: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/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 id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation"
			value="classpath:hibernate.cfg.xml">
		</property>
	</bean>
-->
  	<!-- 配置dbcp形式的数据连接池 -->  
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
	  	<property name="driverClassName" value="com.mysql.jdbc.Driver"/>  
	  	<property name="url" value="jdbc:mysql://localhost:3306/SSH"/>  
	  	<property name="username" value="root"/>  
	  	<property name="password" value="123456"/>	  	  
 	</bean>
 	
 	<!-- 配置Hibernate托管到Spring中 -->
 	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
 		<property name="dataSource">  
 			<ref bean="dataSource" />  
		</property>  
		<property name="hibernateProperties">  
			<props>  
				<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>  
        	</props>  
	   	</property>
		<property name="mappingResources">  
			<list> 
				<value>com/lzb/hibernate/entity/xml/TUser.hbm.xml</value>  
			</list>  
		</property>  
	</bean>
	
	<!-- 业务逻辑层开始配置 -->
	<bean name="tuserDaoImpl" class="com.lzb.hibernate.dao.impl.TUserDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<bean name="tuserServiceImpl" class="com.lzb.hibernate.service.impl.TUserServiceImpl">
		<property name="tuserDao" ref="tuserDaoImpl"></property>
	</bean>
	
	<bean name="tuserAction" class="com.lzb.hibernate.action.TUserAction">
		<property name="tuserServce" ref="tuserServiceImpl"></property>
	</bean>
			
</beans>

发布到tomcat就可以实现登录了。

  • 大小: 106.7 KB
  • SSH.rar (19.4 KB)
  • 下载次数: 8
  • 大小: 45 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics