SSH OpenSessionInView

       SSH项目整合示例【OpenSessionInView】

        在使用ssh开发的项目中,hibernate的懒加载一直是让程序员很头疼的问题。当然,Spring作为一个优秀的框架已经为我们提出了解决方案,那便是大名鼎鼎的OpenSessionInView。接下来我将以一个简单的例子实现SSH的整合及OpenSessionInView。

        下面是项目源码的链接,当然如果你想手动操作一遍,那就不要下载源码了,跟着我魔鬼的步伐一步一步走下去吧,半小时后你会有很大的成就感哦。

        源码链接【点我下载源码


第一步:准备数据(数据库表)

        我是用的是mysql数据库,其中创建两张表。person和sex。建表语句如下:

       

/*
Navicat MySQL Data Transfer

Source Server         : localhost
Source Server Version : 50626
Source Host           : localhost:3306
Source Database       : sshtest

Target Server Type    : MYSQL
Target Server Version : 50626
File Encoding         : 65001

Date: 2016-10-10 13:50:09
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `person`
-- ----------------------------
DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
  `pid` bigint(20) NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  `description` varchar(50) DEFAULT NULL,
  `sid` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`pid`),
  KEY `sid` (`sid`),
  CONSTRAINT `sid` FOREIGN KEY (`sid`) REFERENCES `sex` (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of person
-- ----------------------------
INSERT INTO `person` VALUES ('1', '小涛涛', '这是一个小涛涛的测试', '1');

-- ----------------------------
-- Table structure for `sex`
-- ----------------------------
DROP TABLE IF EXISTS `sex`;
CREATE TABLE `sex` (
  `sid` bigint(20) NOT NULL DEFAULT '0',
  `name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of sex
-- ----------------------------
INSERT INTO `sex` VALUES ('1', '男');
INSERT INTO `sex` VALUES ('2', '女');
INSERT INTO `sex` VALUES ('3', '人妖');
        在此声明,我的数据库名是sshtest。person为人员表,有四个字段【主键pid,名字name,描述describe,与sex表关联的外键--性别sid】;

                                                                          sex表为性别表,有两个字段【主键sid,性别汉语名name】。

第二步:创建空项目,引入相关jar包

        我用的是MyEclipse2014,当然你也可以使用其他版本的myeclipse或者eclipse for javaee。

        新建一个javaee项目,如下图,此时这个项目是一个空web项目,还是一张白纸,接下来就让我们尽情的挥洒泼墨吧!!!

      

第三步:导入SSH相关jar包

        打开项目的WEB-INF/lib目录,将下方链接中的jar包全部复制进lib中。

        jar包链接【点我下载源码】,源码中有jar包哦

        至此,我们项目的白纸有了,画图的工具(jar)也有了,下一步就真的要开始实践了哦。

第四步:搭建包结构

        事先想好项目的结构,提前搭建好包的结构,有助于我们在开发过程中保持一个严谨的思路。

        在这个测试项目中,我使用MVC设计模式,主要有三层,service、dao、action。宝结构如下图所示,请各位读者依照下图创建起来这个包目录吧。

       

第五步:配置Hibernate表实体类及其映射文件【如下四个文件】

                                                                                                                        

        内容分别如下:

package com.zjt.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 * Person entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "person", catalog = "sshtest")
public class Person implements java.io.Serializable {

	// Fields

	private Long pid;
	private Sex sex;
	private String name;
	private String description;

	// Constructors

	/** default constructor */
	public Person() {
	}

	/** minimal constructor */
	public Person(Long pid) {
		this.pid = pid;
	}

	/** full constructor */
	public Person(Long pid, Sex sex, String name, String description) {
		this.pid = pid;
		this.sex = sex;
		this.name = name;
		this.description = description;
	}

	// Property accessors
	@Id
	@Column(name = "pid", unique = true, nullable = false)
	public Long getPid() {
		return this.pid;
	}

	public void setPid(Long pid) {
		this.pid = pid;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "sid")
	public Sex getSex() {
		return this.sex;
	}

	public void setSex(Sex sex) {
		this.sex = sex;
	}

	@Column(name = "name", length = 20)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "description", length = 50)
	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

}


package com.zjt.entity;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * Sex entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "sex", catalog = "sshtest")
public class Sex implements java.io.Serializable {

	// Fields

	private Long sid;
	private String name;
	private Set<Person> persons = new HashSet<Person>(0);

	// Constructors

	/** default constructor */
	public Sex() {
	}

	/** minimal constructor */
	public Sex(Long sid) {
		this.sid = sid;
	}

	/** full constructor */
	public Sex(Long sid, String name, Set<Person> persons) {
		this.sid = sid;
		this.name = name;
		this.persons = persons;
	}

	// Property accessors
	@Id
	@Column(name = "sid", unique = true, nullable = false)
	public Long getSid() {
		return this.sid;
	}

	public void setSid(Long sid) {
		this.sid = sid;
	}

	@Column(name = "name", length = 50)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "sex")
	public Set<Person> getPersons() {
		return this.persons;
	}

	public void setPersons(Set<Person> persons) {
		this.persons = persons;
	}

}


<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.zjt.entity.Person" table="person" catalog="sshtest" lazy="true">
        <id name="pid" type="java.lang.Long">
            <column name="pid" />
            <generator class="assigned" />
        </id>
        <many-to-one name="sex" class="com.zjt.entity.Sex" fetch="select">
            <column name="sid" />
        </many-to-one>
        <property name="name" type="java.lang.String">
            <column name="name" length="20" />
        </property>
        <property name="description" type="java.lang.String">
            <column name="description" length="50" />
        </property>
    </class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.zjt.entity.Sex" table="sex" catalog="sshtest">
        <id name="sid" type="java.lang.Long">
            <column name="sid" />
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="50" />
        </property>
        <set name="persons" inverse="true">
            <key>
                <column name="sid" />
            </key>
            <one-to-many class="com.zjt.entity.Person" />
        </set>
    </class>
</hibernate-mapping>

        至此hibernate的直接配置已经完成,可能你会好奇,之前的hibernate.cfg.xml文件去哪了呢,在此事先说明,我们使用spring管理了hibernate之后,便不再需要这个文件了。

第六步:编写dao层

        分别在com.zjt.dao和com.zjt.dao.impl中编写两个类,如下图所示

        可以看出,上方的PersonDao为接口,代码段如下

package com.zjt.dao;

import java.io.Serializable;

import com.zjt.entity.Person;

public interface PersonDao {
	public Person getPersonById(Serializable id);
}
        下方的PersonDao为对接口的实现,代码段如下

package com.zjt.dao.impl;

import java.io.Serializable;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.zjt.dao.PersonDao;
import com.zjt.entity.Person;

public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao{

	@Override
	public Person getPersonById(Serializable id) {
		Person person = this.getHibernateTemplate().load(Person.class, id);
		return person;
	}
}

第七步:编写service层

        分别在com.zjt.service和com.zjt.service.impl中编写两个类,如下图所示

        上方的PersonService为接口,代码段如下

package com.zjt.service;

import java.io.Serializable;

import com.zjt.entity.Person;

public interface PersonService {
	public Person getPersonById(Serializable id);
}

        下方的PersonServiceImpl为对接口的实现,代码段如下

package com.zjt.service.impl;

import java.io.Serializable;

import com.zjt.dao.PersonDao;
import com.zjt.entity.Person;
import com.zjt.service.PersonService;

public class PersonServiceImpl implements PersonService{
	private PersonDao personDao;

       public PersonDao getPersonDao() {
        return personDao;
    }

    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }

	@Override
	public Person getPersonById(Serializable id) {
		// TODO Auto-generated method stub
		Person person =  this.personDao.getPersonById(id);
		return person;
	}
}


第八步:编写action层

        创建下图所示类作为action

        

        其代码如下:

package com.zjt.action;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.zjt.entity.Person;
import com.zjt.service.PersonService;

public class PersonAction extends ActionSupport{
	private PersonService personService ;

       public PersonService getPersonService() {
        return personService;
    }
    
    public void setPersonService(PersonService personService) {
        this.personService = personService;
    }
    


      public String getPersonByID() {
		Person person = personService.getPersonById(1L);
		ServletActionContext.getRequest().setAttribute("person", person);
		System.out.println("PersonAction.getPersonByID()");
		return "index" ;
	}
}

第九步:至此,我们所有代码已经完成了,接下来就要编辑配置文件了,我们先新建一个源码包config,将配置文件都放在config下以便管理,如下图所示

第十步:配置struts配置文件

        首先在config目录下新建一个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>
	<include file="struts/struts-person.xml"></include>
</struts>

        可以看到我们上面xml中有一行引用:
<span style="color:#FF6666;"><include file="struts/struts-person.xml"></include></span>
        因此,我们再在config/struts下新建一个struts-person.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>
	<package name="person" namespace="/" extends="struts-default">
		<action name="personAction_*" method="{1}" class="personAction">
			<result name="index">index.jsp</result>
		</action>
	</package>
</struts>

第十一步:配置数据库配置文件

        之前已经说过,hibernate被spring管理起来了,因此现在我们先创建一个数据配置文件,如下图所示


         代码如下:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/sshtest
jdbc.username=mysql
jdbc.password=mysql

第十二步:配置Spring配置文件

         先新建如下图所示的三个文件

 

        其中,applicationContext.xml、applicationContext-db.xml、applicationContext-person.xml代码分别如下

       

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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">
  	<import resource="applicationContext-db.xml"/>
  	<import resource="applicationContext-person.xml"/>
</beans>

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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">
  	<!-- 
  		引入prperties配置文件
  	 -->
  	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:jdbc.properties</value>
		</property>
	</bean>
    
  	<bean id="dataSource" destroy-method="close"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- 
			映射文件所在的路径
		 -->
		<property name="mappingDirectoryLocations">
			<list>
				<!-- 
					spring容器会去该包及子包下搜索所有的映射文件
				 -->
				<value>classpath:com/zjt/entity</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
	</bean>
	
	
	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</property>
	</bean>
	
	<tx:advice transaction-manager="transactionManager" id="tx">
		<tx:attributes>
			<tx:method name="save*" read-only="false"/>
			<tx:method name="get*" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	
	<aop:config>
		<aop:pointcut 
			expression="execution(* com.zjt.service.impl.*.*(..))" id="perform"/>
		<aop:advisor advice-ref="tx" pointcut-ref="perform"/>
	</aop:config>
	
</beans>


第十三步:修改index页面如下

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    This is my JSP page. <br>
    	${requestScope.person.name}
    	${requestScope.person.sex.name}
  </body>
</html>
 

第十四步:修改web.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SSHTest</display-name>
  
   <listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/applicationContext.xml</param-value>
	</context-param>

	<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>
	
	<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>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

第十五步:运行项目

        在浏览器地址栏输入http://127.0.0.1:8080/SSHTest/personAction_getPersonByID

        你会看到如下界面,那么恭喜你,成功了,接下来你可以专心研究一下这个项目的配置文件和代码了。

       

===================================================================================================================================

===========================================================以下内容为 2016-10-17更新==================================================

===================================================================================================================================

在此附上我   2016-10-17日创建的一个新的  SSH整合项目(只是将Struts+Spring+Hibernate版本更新到了最新版),链接如下

http://download.csdn.net/download/jintaohahahaha/9656984

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值