SSH框架整合

SSH整合:

Spring和Struts的整合

Spring和Hibernate的整合


整合步骤:

1) 引入SSH Jar文件

        Struts核心jar

        Hibernate核心jar

        Spring

               Core  核心功能

               Web  对web模块支持

                Aop   aop支持

                Orm   对hibernate支持

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

2)配置

        Web.xml

                初始化struts功能、spring容器

        Struts.xml   配置请求路径与映射action的关系

        Spring.xml  IOC容器配置

                bean-base.xml     【公用信息】

                bean-service.xml

                bean-dao.xml

                bean-action.xml

先写好实体类Employee

[java]  view plain  copy
 print ?
  1. <span style="font-size:18px;">package cn.qblank.entity;  
  2.   
  3. public class Employee {  
  4.     private int id;  
  5.     private String empName;  
  6.     private double salary;  
  7.     private Dept dept;  
  8.     public int getId() {  
  9.         return id;  
  10.     }  
  11.     public void setId(int id) {  
  12.         this.id = id;  
  13.     }  
  14.     public String getEmpName() {  
  15.         return empName;  
  16.     }  
  17.     public void setEmpName(String empName) {  
  18.         this.empName = empName;  
  19.     }  
  20.     public double getSalary() {  
  21.         return salary;  
  22.     }  
  23.     public void setSalary(double salary) {  
  24.         this.salary = salary;  
  25.     }  
  26.     public Dept getDept() {  
  27.         return dept;  
  28.     }  
  29.     public void setDept(Dept dept) {  
  30.         this.dept = dept;  
  31.     }  
  32.       
  33. }  
  34. </span>  

实体类Dept

[java]  view plain  copy
 print ?
  1. <span style="font-size:18px;">package cn.qblank.entity;  
  2.   
  3. public class Dept {  
  4.     private int id;  
  5.     private String name;  
  6.     public int getId() {  
  7.         return id;  
  8.     }  
  9.     public void setId(int id) {  
  10.         this.id = id;  
  11.     }  
  12.     public String getName() {  
  13.         return name;  
  14.     }  
  15.     public void setName(String name) {  
  16.         this.name = name;  
  17.     }  
  18. }  
  19. </span>  

然后配置对应的hibernate配置文件Employee.hbm.xml

[html]  view plain  copy
 print ?
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC   
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
  5.       
  6. <hibernate-mapping package="cn.qblank.entity">  
  7.     <!-- 配置多对一 -->  
  8.     <class name="Employee" table="t_employee">  
  9.         <id name="id" column="id">  
  10.             <generator class="native"></generator>  
  11.         </id>  
  12.         <property name="empName"></property>  
  13.         <property name="salary"></property>  
  14.         <!-- 多对一 -->  
  15.         <many-to-one name="dept" column="dept_id" class="Dept"></many-to-one>  
  16.     </class>  
  17. </hibernate-mapping></span>  

以及Dept.hbm.xml

[html]  view plain  copy
 print ?
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC   
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
  5.   
  6. <hibernate-mapping package="cn.qblank.entity">  
  7.     <class name="Dept" table="t_dept">  
  8.         <id name="id" column="deptId">  
  9.             <generator class="native"></generator>  
  10.         </id>  
  11.         <property name="name" column="deptName" type="string"></property>  
  12.     </class>  
  13. </hibernate-mapping></span>  

然后在数据库中创建相应的表,表结构如下:

[sql]  view plain  copy
 print ?
  1. <span style="font-size:18px;">CREATE TABLE t_employee(  
  2.     id INT PRIMARY KEY AUTO_INCREMENT,  
  3.     empName VARCHAR(20),  
  4.     salary DOUBLE,  
  5.     dept_id INT  
  6. );  
  7.   
  8. CREATE TABLE t_dept(  
  9.     deptId INT PRIMARY KEY AUTO_INCREMENT,  
  10.     deptName VARCHAR(20)  
  11. )</span>  

插入如下数据:




然后建立一个EmployeeDao

[java]  view plain  copy
 print ?
  1. <span style="font-size:18px;">package cn.qblank.dao;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. import org.hibernate.SessionFactory;  
  6.   
  7. import cn.qblank.entity.Employee;  
  8.   
  9. public class EmployeeDao {  
  10.     //注入sessionFactory对象  
  11.     private SessionFactory sessionFactory;  
  12.     public void setSessionFactory(SessionFactory sessionFactory) {  
  13.         this.sessionFactory = sessionFactory;  
  14.     }  
  15.     public Employee findById(Serializable id){  
  16.         return (Employee) sessionFactory.getCurrentSession().get(Employee.class, id);  
  17.     }  
  18. }  
  19. </span>  

创建对应的EmployeeService

[java]  view plain  copy
 print ?
  1. <span style="font-size:18px;">package cn.qblank.service;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. import cn.qblank.dao.EmployeeDao;  
  6. import cn.qblank.entity.Employee;  
  7.   
  8. public class EmployeeService {  
  9.     //注入dao对象  
  10.     private EmployeeDao employeeDao;  
  11.     public void setEmployeeDao(EmployeeDao employeeDao) {  
  12.         this.employeeDao = employeeDao;  
  13.     }  
  14.     public Employee findById(Serializable id){  
  15.         return employeeDao.findById(id);  
  16.     }  
  17. }  
  18. </span>  

然后将数据库连接信息写入db.properties中

[html]  view plain  copy
 print ?
  1. <span style="font-size:18px;">jdbc.user=root  
  2. jdbc.password=root  
  3. jdbc.driverClass=com.mysql.jdbc.Driver  
  4. jdbc.jdbcUrl=jdbc:mysql:///day09  
  5. jdbc.initPoolSize=5  
  6. jdbc.maxPoolSize=10  
  7. jdbc.maxStatements=100  
  8. jdbc.acquireIncrement=2</span>  

编写bean-base.xml加载配置信息,以及事务管理,aop配置(将实例放入IOC容器交给其他的文件,这里只写通用配置)

[html]  view plain  copy
 print ?
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:aop="http://www.springframework.org/schema/aop"  
  7.     xmlns:tx="http://www.springframework.org/schema/tx"  
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  9.          http://www.springframework.org/schema/beans/spring-beans.xsd  
  10.          http://www.springframework.org/schema/context  
  11.          http://www.springframework.org/schema/context/spring-context.xsd  
  12.          http://www.springframework.org/schema/aop  
  13.          http://www.springframework.org/schema/aop/spring-aop.xsd  
  14.          http://www.springframework.org/schema/tx  
  15.          http://www.springframework.org/schema/tx/spring-tx.xsd">  
  16.        
  17.       <!-- 加载文件 -->  
  18.       <context:property-placeholder location="classpath:db.properties"/>  
  19.       <!-- 配置数据源 -->  
  20.       <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  21.         <property name="user" value="${jdbc.user}"></property>  
  22.         <property name="password" value="${jdbc.password}"></property>  
  23.         <property name="driverClass" value="${jdbc.driverClass}"></property>  
  24.         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>  
  25.         <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>  
  26.         <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>  
  27.         <property name="maxStatements" value="${jdbc.maxStatements}"></property>  
  28.         <property name="acquireIncrement" value="${jdbc.acquireIncrement}"></property>  
  29.       </bean>  
  30.         
  31.       <!-- 配置sessionFactory -->  
  32.       <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  33.         <!-- 1.连接池 -->  
  34.         <property name="dataSource" ref="dataSource"></property>  
  35.         <!-- 2.hibernate常用配置:方言,显示sql,自动建表 -->  
  36.         <property name="hibernateProperties">  
  37.             <props>  
  38.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
  39.                 <prop key="hibernate.show_sql">true</prop>  
  40.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
  41.             </props>  
  42.         </property>  
  43.         <!-- hibernate映射配置:拦截hibernate文件 -->  
  44.         <property name="mappingLocations">  
  45.             <list>  
  46.                 <value>classpath:cn/qblank/entity/*.hbm.xml</value>  
  47.             </list>  
  48.         </property>  
  49.       </bean>  
  50.       <!-- 事务配置 -->  
  51.       <!-- 1.事务管理类配置 -->  
  52.       <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  53.         <property name="sessionFactory" ref="sessionFactory"></property>  
  54.       </bean>  
  55.         
  56.       <!-- 2.事务增强类 -->  
  57.       <tx:advice id="txAdvice" transaction-manager="txManager">  
  58.         <tx:attributes>  
  59.             <tx:method name="*" read-only="false"/>  
  60.         </tx:attributes>  
  61.       </tx:advice>  
  62.         
  63.       <!-- AOP配置 -->  
  64.       <aop:config>  
  65.         <aop:pointcut expression="execution(* cn.qblank.service.*.*(..))" id="pt"/>  
  66.         <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>  
  67.       </aop:config>  
  68. </beans></span>  

配置web.xml,分别配置懒加载OpenSessionInView(拦截*.action请求)、Struts核心过滤器、以及Spring配置

[html]  view plain  copy
 print ?
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  
  3.       
  4.     <!-- 配置spring的OpenSessionInView模式 【目的:JSp页面访问懒加载数据】 -->  
  5.     <!-- 注意:访问struts时候需要带上*.action后缀 -->  
  6.     <filter>  
  7.         <filter-name>OpenSessionInView</filter-name>  
  8.         <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>  
  9.     </filter>  
  10.     <filter-mapping>  
  11.         <filter-name>OpenSessionInView</filter-name>  
  12.         <url-pattern>*.action</url-pattern>  
  13.     </filter-mapping>  
  14.       
  15.     <!-- 核心过滤器 -->  
  16.       <filter>  
  17.         <filter-name>struts2</filter-name>  
  18.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>        
  19.       </filter>  
  20.       <filter-mapping>  
  21.         <filter-name>struts2</filter-name>  
  22.         <url-pattern>/*</url-pattern>  
  23.       </filter-mapping>  
  24.       
  25.     <!-- Spring配置 -->  
  26.     <context-param>  
  27.         <param-name>contextConfigLocation</param-name>  
  28.         <param-value>classpath:bean*.xml</param-value>  
  29.     </context-param>  
  30.     <listener>  
  31.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  32.     </listener>  
  33. </web-app></span>  


然后编写struts.xml,配置页面的跳转

[html]  view plain  copy
 print ?
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
  5.   
  6. <struts>  
  7.     <package name="emp" extends="struts-default">  
  8.         <!-- action实例交给spring容器创建 -->  
  9.         <action name="*.action" class="employeeAction" method="execute">  
  10.             <result name="success">/index.jsp</result>  
  11.         </action>  
  12.     </package>  
  13. </struts></span>  


然后分别编写bean-dao.xml、bean-service.xml以及bean-action加载各类的实例

bean-dao.xml配置

[html]  view plain  copy
 print ?
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:aop="http://www.springframework.org/schema/aop"  
  7.     xmlns:tx="http://www.springframework.org/schema/tx"  
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  9.          http://www.springframework.org/schema/beans/spring-beans.xsd  
  10.          http://www.springframework.org/schema/context  
  11.          http://www.springframework.org/schema/context/spring-context.xsd  
  12.          http://www.springframework.org/schema/aop  
  13.          http://www.springframework.org/schema/aop/spring-aop.xsd  
  14.          http://www.springframework.org/schema/tx  
  15.          http://www.springframework.org/schema/tx/spring-tx.xsd">  
  16.      <!-- dao -->  
  17.      <bean id="employeeDao" class="cn.qblank.dao.EmployeeDao">  
  18.         <property name="sessionFactory" ref="sessionFactory"></property>  
  19.      </bean>  
  20. </beans></span>  

bean-service.xml配置

[html]  view plain  copy
 print ?
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:aop="http://www.springframework.org/schema/aop"  
  7.     xmlns:tx="http://www.springframework.org/schema/tx"  
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  9.          http://www.springframework.org/schema/beans/spring-beans.xsd  
  10.          http://www.springframework.org/schema/context  
  11.          http://www.springframework.org/schema/context/spring-context.xsd  
  12.          http://www.springframework.org/schema/aop  
  13.          http://www.springframework.org/schema/aop/spring-aop.xsd  
  14.          http://www.springframework.org/schema/tx  
  15.          http://www.springframework.org/schema/tx/spring-tx.xsd">  
  16.      <!-- service -->  
  17.      <bean id="employeeService" class="cn.qblank.service.EmployeeService">  
  18.         <property name="employeeDao" ref="employeeDao"></property>  
  19.      </bean>  
  20. </beans></span>  

bean-action.xml配置

[html]  view plain  copy
 print ?
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:aop="http://www.springframework.org/schema/aop"  
  7.     xmlns:tx="http://www.springframework.org/schema/tx"  
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  9.          http://www.springframework.org/schema/beans/spring-beans.xsd  
  10.          http://www.springframework.org/schema/context  
  11.          http://www.springframework.org/schema/context/spring-context.xsd  
  12.          http://www.springframework.org/schema/aop  
  13.          http://www.springframework.org/schema/aop/spring-aop.xsd  
  14.          http://www.springframework.org/schema/tx  
  15.          http://www.springframework.org/schema/tx/spring-tx.xsd">  
  16.      <!-- action -->  
  17.      <bean id="employeeAction" class="cn.qblank.action.EmployeeAction" scope="prototype">  
  18.         <property name="employeeService" ref="employeeService"></property>  
  19.      </bean>  
  20.        
  21. </beans></span>  

最后我们编写EmployeeAction以及跳转也index.jsp进行测试

EmployeeAction类:

[java]  view plain  copy
 print ?
  1. <span style="font-size:18px;">package cn.qblank.action;  
  2.   
  3. import java.util.Map;  
  4.   
  5. import com.opensymphony.xwork2.ActionContext;  
  6. import com.opensymphony.xwork2.ActionSupport;  
  7.   
  8. import cn.qblank.entity.Employee;  
  9. import cn.qblank.service.EmployeeService;  
  10.   
  11. public class EmployeeAction extends ActionSupport{  
  12.     // 容器注入Service  
  13.     private EmployeeService employeeService;  
  14.     public void setEmployeeService(EmployeeService employeeService) {  
  15.         this.employeeService = employeeService;  
  16.     }  
  17.     @Override  
  18.     public String execute() throws Exception {  
  19.         int empId = 1;  
  20.         // 调用Service  
  21.         Employee employee = employeeService.findById(empId);  
  22.         // 保存到request  
  23.         Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");  
  24.         request.put("emp", employee);  
  25.         System.out.println(employee);  
  26.         return SUCCESS;  
  27.     }  
  28. }  
  29. </span>  

index.jsp显示action存入域中的数据

[html]  view plain  copy
 print ?
  1. <span style="font-size:18px;"><%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>显示页面</title>  
  8. </head>  
  9. <body>  
  10.     <h3>显示数据</h3>  
  11.     姓名:${emp.empName}<br/>  
  12.     部门:${emp.dept.name}<br/>  
  13. </body>  
  14. </html></span>  

在地址栏中输入: http://localhost:8081/Spring04-hibernate-struts/show.action 运行结果如下:



原文地址:http://blog.csdn.net/Evan_QB/article/details/78397081


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值