SSh整合

一,概述

1)SSH整合即Spring与Struts2整合,Spring与Hibernate整合.

2)需求:jsp页面显示员工信息.

二,环境准备

2.1准备相关jar包

Struts核心jar

Hibernate核心jar

Spring

    core  核心功能

    aop   aop支持

    web   对web模块的支持

    orm   对Hibernate的支持

    Jdbc/tx jdbc支持包/事务相关包 

2.2配置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">
	<!-- 配置Spring的OpenSessionInview模式(目的:jsp页面访问懒加载数据) -->        
        <!-- 注意:访问Action的时候要带上*.action -->
 <filter><filter-name>OpenSessionInview</filter-name><filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class></filter><filter-mapping><filter-name>OpenSessionInview</filter-name><url-pattern>*.action</url-pattern></filter-mapping><!-- struts配置:引入struts2核心过滤器 --><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><!-- Spring配置 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:bean-*.xml</param-value><!-- <param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value> --></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><display-name></display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>


a)web.xml主要配置了三个块内容,分别是struts配置/Spring配置/Spring的OpenSessionInView模式配置.

b)第一二块内容是一定要配置的,第三块内容并不是一定要配置,配置的目的就是为了使jsp页面访问懒加载数据.

c)配置Struts的核心过滤器的目的就是为了引入Struts2的核心功能.

d)Spring配置是为了在启动服务器的时候就初始化一些基本配置(数据库连接,连接池配置,事务配置等等)与对象(如dao对象,service对象等等).

2.3实体类及映射文件

a)Dept.java(setter和getter方法就不粘贴出来了,下面也是如此)

public class Dept {

	public Dept(){
		
	}
	private int deptId;
	private String deptName;
	/** 
     * 一对多,一个部门对应多个员工 
     */  
    private Set<Employee> emps=new HashSet<Employee>();
} 
b)Employee.java

public class Employee {

	public Employee(){
		
	}
	
	private int empId;
	private String empName;
	private double salary;
	private Dept dept;
}
c)Dept.hbm.xml

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

<hibernate-mapping package="com.bighuan.entity"
	auto-import="true">

	<class name="Dept" table="t_dept">
		<id name="deptId" column="deptId">
			<generator class="native"></generator>
		</id>
		<property name="deptName" column="deptName" length="20"></property>
		<!-- 一对多 -->
		<set name="emps" table="t_employee">
			<key column="dept_id"></key>
			<one-to-many class="Employee"/>
		</set>
	</class>
	
</hibernate-mapping>
d)Employee.hbm.xml

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

<hibernate-mapping package="com.bighuan.entity"
	auto-import="true">

	<class name="Employee" table="t_employee">
		<id name="empId" column="empId">
			<generator class="native"></generator>
		</id>
		<property name="empName" column="empName" length="20"></property>
		<property name="salary" column="salary" type="double"></property>
		<!-- 多对一 -->
		<many-to-one name="dept" column="dept_id" class="Dept">
		</many-to-one>
		
	</class>
	
</hibernate-mapping>

2.4持久层及业务逻辑层开发

1)EmployeeDao.java(因为比较简单,所有没写接口,下面的service也是这样,实际开发中要按规范写)

package com.bighuan.dao;

import java.io.Serializable;

import org.hibernate.SessionFactory;

import com.bighuan.entity.Employee;
/**
 * 持久层开发
 * @author bighuan
 *
 */
public class EmployeeDao {
	//IOC容器注入SessionFactory对象
	private SessionFactory sessionFactory;
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	/**
	 * 查询
	 * @param id
	 * @return
	 */
	public Employee findById(Serializable id){
		return (Employee) sessionFactory.getCurrentSession().get(Employee.class, id);
	}
}
2)EmployeeService.java

package com.bighuan.service;

import java.io.Serializable;

import com.bighuan.dao.EmployeeDao;
import com.bighuan.entity.Employee;

public class EmployeeService {

	// IOC容器注入EmployeeDao对象
	private EmployeeDao employeeDao;

	public void setEmployeeDao(EmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}

	/**
	 * 查询
	 * @param id
	 * @return
	 */
	public Employee findById(Serializable id) {
		Employee emp = employeeDao.findById(id);
		return emp;
	}
}

2.5控制层开发

1)EmployeeAction.java

package com.bighuan.action;

import java.util.Map;

import com.bighuan.entity.Employee;
import com.bighuan.service.EmployeeService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 控制层开发
 * @author bighuan
 *
 */
public class EmployeeAction extends ActionSupport{
	
	//IOC容器注入EmployeeService对象
	private EmployeeService employeeServie;
	public void setEmployeeServie(EmployeeService employeeServie) {
		this.employeeServie = employeeServie;
	}

	@Override
	public String execute() throws Exception {
		int empId=20;
		//调用service
		Employee emp = employeeServie.findById(empId);
		Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
		request.put("emp", emp);
		return SUCCESS;
	}
}
模拟操作,查询工号为20的员工,将查询到的员工对象保存到request域中,返回SUCCESS!

2.6bean-*的配置

1)bean-base.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"
	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.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

	<!-- 所有配置的公共部分 -->
	
	<!-- 1,连接池实例 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hib_demo"></property>
		<property name="user" value="root"></property>
		<property name="password" value="abc"></property>
		<property name="initialPoolSize" value="3"></property>
		<property name="maxPoolSize" value="6"></property>
	</bean>
	
	
	<!-- 2,SessionFactory实例创建 -->
	<!-- 所有的配置都由Spring维护(项目中不需要hibernate.cfg.xml) -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
			<!-- a,连接池 -->
			<property name="dataSource" ref="dataSource"></property>
			
			<!-- b,hibernate常用配置:方言 自动建表 显示SQL等 -->
			<property name="hibernateProperties">
				<props>
					<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
					<prop key="hibernate.show_sql">true</prop>
					<prop key="hibernate.hbm2ddl.auto">update</prop>
				</props>
			</property>
			
			<!-- c,映射配置 -->
			<property name="mappingLocations">
				<list>
					<value>classpath:com/bighuan/entity/*.hbm.xml</value>
				</list>
			</property>
	</bean>
	
	
	<!-- 3,事务配置 -->
	<!-- 3.1,事务管理器 -->
	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<!-- 注入SessionFactory -->
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 3.2,事务增强 -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="*" read-only="false"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- 3.3,AOP配置 -->
	<aop:config>
		<aop:pointcut expression="execution(* com.bighuan.service.*.*(..))" id="pt"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
	</aop:config>
	
	
</beans>
2)bean-dao.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"
	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.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

	<!-- IOC容器的配置:要创建的所有对象都在这里配置 -->

	<bean id="employeeDao" class="com.bighuan.dao.EmployeeDao">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
</beans>
3)bean-service.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"
	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.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

	<!-- IOC容器的配置:要创建的所有对象都在这里配置 -->

	<bean id="employeeServie" class="com.bighuan.service.EmployeeService">
		<property name="employeeDao" ref="employeeDao"></property>
	</bean>
	
</beans>
4)bean-action.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"
	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.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

	<!-- IOC容器的配置:要创建的所有对象都在这里配置 -->
	<bean id="employeeAction" class="com.bighuan.action.EmployeeAction" scope="prototype">
		<property name="employeeServie" ref="employeeServie"></property>
	</bean>
	
</beans>
action的配置中让scope="prototype",表示当要使用时才创建,上面的dao/service是在服务器启动时就创建好了对象.

2.7struts.xml的配置

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

<struts>

	<constant name="struts.enable.DynamicMethodInvocation" value="false" />
	<constant name="struts.devMode" value="false" />
	<package name="emp" extends="struts-default">
	
		<!-- 全局视图 -->
		<global-results>
			<result name="error">/error/error.jsp</result>
		</global-results>
		
		<!-- Action实例交给Spring容器创建 -->
		<action name="show" class="employeeAction" method="execute">
			<result name="success">/index.jsp</result>
		</action>

	</package>


	<!-- Add packages here -->

</struts>
返回成功,就转发到index.jsp

2.8index.jsp

	EL表达式:${requestScope.emp.empName }
   	部门:${emp.dept.deptName }

在jsp页面的<body>标签内,通过两行代码将员工名及其所属的部门显示出来.

2.9测试

在浏览器访问:http://localhost:8080/day38_ssh/show.action,得到页面如下:

数据库中数据读取出来了.

三,总结

再来一天时间,SSH的学习就告一段落了!坚持吧!







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值