欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring+Hibernate+Struts(SSH)框架整合实战

程序员文章站 2023-11-12 14:50:16
ssh框架整合 前言:有人说,现在还是流行主流框架,ssm都出来很久了,更不要说ssh。我不以为然。现在许多公司所用的老项目还是ssh,如果改成主流框架,需要成本。再说比...

ssh框架整合

前言:有人说,现在还是流行主流框架,ssm都出来很久了,更不要说ssh。我不以为然。现在许多公司所用的老项目还是ssh,如果改成主流框架,需要成本。再说比如金融it这一块,数据库dao层还是推荐使用的是hibernate,因为能够快速开发上手,除非是互联网,因涉及到高并发,dao层用的是mybatis,数据交互效率较快。所以,ssh不容忽略。

一、什么是ssh

ssh是 struts+spring+hibernate的一个集成框架,是目前比较流行的一种web应用程序开源框架。

集成ssh框架的系统从职责上分为四层:表示层、业务逻辑层、数据持久层和域模块层,以帮助开发人员在短期内搭建结构清晰、可复用性好、维护方便的web应用程序。其中使用struts作为系统的整体基础架构,负责mvc的分离,在struts框架的模型部分,控制业务跳转,利用hibernate框架对持久层提供支持,spring做管理,管理struts和hibernate。具体做法是:用面向对象的分析方法根据需求提出一些模型,将这些模型实现为基本的java对象,然后编写基本的dao(data access objects)接口,并给出hibernate的dao实现,采用hibernate架构实现的dao类来实现java类与数据库之间的转换和访问,最后由spring做管理,管理struts和hibernate。

---------百度百科

二、ssh所涉及的部分

Spring+Hibernate+Struts(SSH)框架整合实战

Spring+Hibernate+Struts(SSH)框架整合实战

三、快速部署环境

这里利用保存客户的小demo来演示整合ssh

1.导入所需jar包

1). struts2框架

* struts-2.3.24\apps\struts2-blank\web-inf\lib\*.jar        -- struts2需要的所有jar包

* struts2-spring-plugin-2.3.24.jar                          ---struts2整合spring的插件包

Spring+Hibernate+Struts(SSH)框架整合实战

2). hibernate框架

* hibernate-release-5.0.7.final\lib\required\*.jar          -- hibernate框架需要的jar包

* slf4j-api-1.6.1.jar                                       -- 日志接口

* slf4j-log4j12-1.7.2.jar                                   -- 日志实现

* mysql-connector-java-5.1.7-bin.jar                        -- mysql的驱动包

Spring+Hibernate+Struts(SSH)框架整合实战

3). spring框架

* ioc核心包

* aop核心包

* jdbc模板和事务核心包

* spring整合junit测试包

* spring整合hibernate核心包

* spring整合struts2核心包

Spring+Hibernate+Struts(SSH)框架整合实战

2、在web.xml中配置spring与struts的相关代码

1)配置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>

2)配置spring的监听器

当服务启动时,就会先加载spring的配置文件

<!-- 配置spring框架整合web的监听器 -->
  <listener>
   <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
 </listener>

3)配置默认加载路径

 <!-- 监听器默认加载web-inf文件下,需要配置参数来加载指定文件 -->
 <context-param>
   <param-name>contextconfiglocation</param-name>
   <param-value>classpath:applicationcontext.xml</param-value>
</context-param>

总结:web.xml全部代码为

<!-- 配置spring框架整合web的监听器 -->
  <listener>
   <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
 </listener>
 <!-- 监听器默认加载web-inf文件下,需要配置参数来加载指定文件 -->
 <context-param>
   <param-name>contextconfiglocation</param-name>
   <param-value>classpath:applicationcontext.xml</param-value>
 </context-param>
 <!-- 配置核心过滤器 -->
 <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>

2、src下编写相关配置文件

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

2)hibernate:hibernate.cfg.xml

导入相关约束,并配置数据库

<?xml version="1.0" encoding="utf-8"?>
<!doctype hibernate-configuration public
 "-//hibernate/hibernate configuration dtd 3.0//en"
 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 
 <session-factory>
  <!-- 必须配置 -->
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://192.168.174.130:3306/ssh</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">root</property>
  <property name="hibernate.dialect">org.hibernate.dialect.mysqldialect</property>
  
  <!-- 可选配置 -->
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.format_sql">true</property>
  <property name="hibernate.hbm2ddl.auto">update</property>
  
  <!-- 配置c3p0的连接池 -->
  <property name="connection.provider_class">org.hibernate.connection.c3p0connectionprovider</property>
  
  <!-- 不能配置绑定当前的线程的操作 -->
  <!-- 映射配置文件 -->
  <mapping resource="com/clj/domain/customer.hbm.xml"/>
 </session-factory>
 
</hibernate-configuration> 

3)配置log4j.properties

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.consoleappender
log4j.appender.stdout.target=system.err
log4j.appender.stdout.layout=org.apache.log4j.patternlayout
log4j.appender.stdout.layout.conversionpattern=%d{absolute} %5p %c{1}:%l - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.fileappender
log4j.appender.file.file=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.patternlayout
log4j.appender.file.layout.conversionpattern=%d{absolute} %5p %c{1}:%l - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootlogger=info, stdout

4)struts2: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.3.dtd">
<struts>
</struts>

总结:src所需配置文件如图

Spring+Hibernate+Struts(SSH)框架整合实战

3、配置dao层

定义一个接口和其实现类

public interface customerdao {
 public void save(customer customer);
}
public class customerdaoimpl implements customerdao {
 public void save(customer customer) {
  
 }

}

4、定义业务层接口和实现类

package com.clj.service;

import com.clj.domain.customer;

public interface customerservice {
 public void save(customer customer);
}
package com.clj.service;

import org.springframework.transaction.annotation.transactional;

import com.clj.dao.customerdao;
import com.clj.domain.customer;
/**
 * 客户的业务层
 * @author administrator
 *
 */
public class customerserviceimpl implements customerservice{//用来保存客户
 public void save(customer customer) {
  
 }

}

5、定义pojo类

hibernate通过操作pojo类来操作数据库表,做到对象关系映射

package com.clj.domain;

public class customer {
 
 private long cust_id;
 private string cust_name;
 private long cust_user_id;
 private long cust_create_id;
 private string cust_source;
 private string cust_industry;
 private string cust_level;
 private string cust_linkman;
 private string cust_phone;
 private string cust_mobile;
 
 public long getcust_id() {
  return cust_id;
 }
 public void setcust_id(long cust_id) {
  this.cust_id = cust_id;
 }
 public string getcust_name() {
  return cust_name;
 }
 public void setcust_name(string cust_name) {
  this.cust_name = cust_name;
 }
 public long getcust_user_id() {
  return cust_user_id;
 }
 public void setcust_user_id(long cust_user_id) {
  this.cust_user_id = cust_user_id;
 }
 public long getcust_create_id() {
  return cust_create_id;
 }
 public void setcust_create_id(long cust_create_id) {
  this.cust_create_id = cust_create_id;
 }
 public string getcust_source() {
  return cust_source;
 }
 public void setcust_source(string cust_source) {
  this.cust_source = cust_source;
 }
 public string getcust_industry() {
  return cust_industry;
 }
 public void setcust_industry(string cust_industry) {
  this.cust_industry = cust_industry;
 }
 public string getcust_level() {
  return cust_level;
 }
 public void setcust_level(string cust_level) {
  this.cust_level = cust_level;
 }
 public string getcust_linkman() {
  return cust_linkman;
 }
 public void setcust_linkman(string cust_linkman) {
  this.cust_linkman = cust_linkman;
 }
 public string getcust_phone() {
  return cust_phone;
 }
 public void setcust_phone(string cust_phone) {
  this.cust_phone = cust_phone;
 }
 public string getcust_mobile() {
  return cust_mobile;
 }
 public void setcust_mobile(string cust_mobile) {
  this.cust_mobile = cust_mobile;
 }
 @override
 public string tostring() {
  return "customer [cust_id=" + cust_id + ", cust_name=" + cust_name
    + ", cust_user_id=" + cust_user_id + ", cust_create_id="
    + cust_create_id + ", cust_source=" + cust_source
    + ", cust_industry=" + cust_industry + ", cust_level="
    + cust_level + ", cust_linkman=" + cust_linkman
    + ", cust_phone=" + cust_phone + ", cust_mobile=" + cust_mobile
    + "]";
 }
 
}

6、定义customer.hbm.xml

此配置文件关乎customer这个pojo类,此文件需放在customer pojo类同个包下

<?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">
 
<hibernate-mapping>
 
 <class name="com.clj.domain.customer" table="cst_customer">
  <id name="cust_id" column="cust_id">
   <generator class="native"/>
  </id>
  
  <property name="cust_name" column="cust_name"/>
  <property name="cust_user_id" column="cust_user_id"/>
  <property name="cust_create_id" column="cust_create_id"/>
  <property name="cust_source" column="cust_source"/>
  <property name="cust_industry" column="cust_industry"/>
  <property name="cust_level" column="cust_level"/>
  <property name="cust_linkman" column="cust_linkman"/>
  <property name="cust_phone" column="cust_phone"/>
  <property name="cust_mobile" column="cust_mobile"/>
  
 </class>
 
</hibernate-mapping>

项目构建大致图

Spring+Hibernate+Struts(SSH)框架整合实战

四、demo之保存客户初步演示

这里先初略的定义持久层交给heibernate,业务层交个struts2,创建实例交给spring

1、定义一个保存客户的界面,利用form表单进行数据的提交

根据域名可知,这里利用的是struts2的通配符方式进行访问

<form id=form1 name=form1
  action="${pagecontext.request.contextpath }/customer_add.action"
  method=post>
   <!--table部分省略-->
</form>

2、在struts.xml中配置接受请求,根据action名和方法跳转指定的action,执行指定的方法

spring整合struts2方式一:action由struts2框架管理

* 因为导入的struts2-spring-plugin-2.3.24.jar 包自带一个配置文件 struts-plugin.xml ,该配置文件中有如下代码

* <constant name="struts.objectfactory" value="spring" />   开启一个常量,如果该常量开启,那么下面的常量就可以使用

* struts.objectfactory.spring.autowire = name,该常量是可以让action的类来自动装配bean对象!

<?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.3.dtd">
<struts>
 <!-- 配置包结构 -->
  <package name="crm" extends="struts-default" namespace="/">
   <!-- 配置客户的action -->
   <!-- 方式一:aciton由struts2框架管理-->
      <action name="customer_*" class="com.clj.web.action.customeraction" method="{1}"/> 
    </package>
 </struts>

3、在spring的applicationcontext.xml中配置相对应的bean以及事务

这里利用spring中ioc(控制反转)的特性,将创建实例的任务交给spring框架管理

 <bean id="customerservice" class="com.clj.service.customerserviceimpl">
  <property name="customerdao" ref="customerdao"></property>
 </bean>
 <bean id="customerdao" class="com.clj.dao.customerdaoimpl">
  <property name="hibernatetemplate" ref="hibernatetemplate"/>
 </bean>
 <bean id="hibernatetemplate" class="org.springframework.orm.hibernate5.hibernatetemplate">
  <!-- 注入sessionfactory -->
  <property name="sessionfactory"/>
 </bean>
</beans>

4、编写持久层实现类相关代码

这里利用hibernate提供的模板类,内部封转了session,从而可以调用session中的方法

/**
 * 持久层
 * 
 * @author administrator
 *
 */
public class customerdaoimpl implements customerdao {
 //将数据保存到数据库中(调用模板类(hibernate提供,内部封装了session))
 private hibernatetemplate hibernatetemplate;
 
 public void sethibernatetemplate(hibernatetemplate hibernatetemplate) {
  this.hibernatetemplate = hibernatetemplate;
 }

 /**
  * 保存客户
  */
 public void save(customer customer) {
  system.out.println("持久层:保存客户");
  hibernatetemplate().save(customer);
 }

}

5、编写业务层实现类相关代码

package com.clj.service;

import org.springframework.transaction.annotation.transactional;

import com.clj.dao.customerdao;
import com.clj.domain.customer;
/**
 * 客户的业务层
 * @author administrator
 *
 */
@transactional
public class customerserviceimpl implements customerservice{
 private customerdao customerdao;
 
 public void setcustomerdao(customerdao customerdao) {
  this.customerdao = customerdao;
 }

 //用来保存客户
 public void save(customer customer) {
  system.out.println("业务层,保存客户");
  customerdao.save(customer);
 }

}

6、编写action相关代码

这里通过struts2的模板类

package com.clj.web.action;

import org.apache.struts2.servletactioncontext;
import org.springframework.web.context.webapplicationcontext;
import org.springframework.web.context.support.webapplicationcontextutils;

import com.clj.domain.customer;
import com.clj.service.customerservice;
import com.opensymphony.xwork2.actionsupport;
import com.opensymphony.xwork2.modeldriven;

/**
 * 客户的控制层
 * @author administrator
 *
 */
public class customeraction extends actionsupport implements modeldriven<customer>{
 //不要忘记手动new
 private customer customer=new customer();
 public customer getmodel() {
  return customer;
 }
 //提供service成员属性,提供set方法
 private customerservice customerservice;
 
 

 public void setcustomerservice(customerservice customerservice) {
  this.customerservice = customerservice;
 }



 /**
  * 保存客户
  * @return
  */
 public string add(){
  system.out.println("web层,保存客户");
  //方式一:创建web的工厂(action由struts2创建)
  webapplicationcontext context=webapplicationcontextutils.getwebapplicationcontext(servletactioncontext.getservletcontext());
  customerservice cs=(customerservice) context.getbean("customerservice");
  //调用方法
  cs.save(customer);return none;
 }

 
}

五、项目优化之整合

1、 spring整合struts2方式二:action由spring框架管理

把具体的 action类配置文件applicatoncontext.xml的配置文件中,但是注意:struts.xml需要做修改

<struts>
 <!-- 配置包结构 -->
  <package name="crm" extends="struts-default" namespace="/">
   <!-- 配置客户的action -->
   <!-- 方式一:aciton由struts2框架管理
   <action name="customer_*" class="com.clj.web.action.customeraction" method="{1}"/>-->
   <!-- 方式二:action由spring管理,class标签上只需要编写srping配置bean的id值既可以-->
    <action name="customer_*" class="customeraction" method="{1}"></action>
  </package>
</struts>

2、在applicationcontext.xml中配置action类

注意:1)spring框架默认生成customeraction是单例的,而struts2框架是多例的。所以需要配置 scope="prototype"

2)此时没有struts2的自动装配,在action需要手动配置customerservice属性,并在action类中生成set方法

<!-- 配置客户模块 -->
 <!-- 强调:配置的aciton,必须是多列的 -->
 <bean id="customeraction" class="com.clj.web.action.customeraction" scope="prototype">
  <!--注意:struts管理action时,基于其中有个struts-plugin的jar包,其中更改了一个
  常量struts.objectfactory.spring.autowire = name将其打开了,可以自动装配,只需要提供set方法
  但是此时action由spring管理,自动装配失效,所以需要手动进行配置注入
  -->
  <property name="customerservice" ref="customerservice"></property>
</bean>

3、.配置事务

spring整合hibernate方式一: (带有 hibernate.cfg.xml的配置文件。强调:不能加绑定当前线程的配置)

以前玩hibernate时,hibernate.cfg.xml都是由hibernate框架管理,其配置文件能生成sessionfactory,持久层加载此配置文件获取sessionfactory,从而创建工厂生成session,进行数据的增删改成,此时其配置文件应该交给spring管理,充分利用spring的ioc特性

spring框架提供了一个hibernatedaosupport的工具类,以后dao都可以继承该类!!在引入hibernate核心配置文件之前,得让dao层继承一个父类hibernatedaosupport,此父类内部封装了事务模板

看源码:

Spring+Hibernate+Struts(SSH)框架整合实战

1)修改相对应的持久层实现类,让他继承hibernatedaosupport

package com.clj.dao;

import org.springframework.orm.hibernate5.hibernatetemplate;
import org.springframework.orm.hibernate5.support.hibernatedaosupport;

import com.clj.domain.customer;
/**
 * 持久层
 * 继承hibernatedaosupport,内部封装了hibernatetemplate
 * @author administrator
 *
 */
public class customerdaoimpl extends hibernatedaosupport implements customerdao {
 //将数据保存到数据库中(调用模板类(hibernate提供,内部封装了session))
 /*private hibernatetemplate hibernatetemplate;
 
 public void sethibernatetemplate(hibernatetemplate hibernatetemplate) {
  this.hibernatetemplate = hibernatetemplate;
 }*/

 /**
  * 保存客户
  */
 public void save(customer customer) {
  system.out.println("持久层:保存客户");
  this.gethibernatetemplate().save(customer);
 }

}

2)修改业务层让,开启事务注解

package com.clj.service;

import org.springframework.transaction.annotation.transactional;

import com.clj.dao.customerdao;
import com.clj.domain.customer;
/**
 * 客户的业务层
 * @author administrator
 *
 */
@transactional
public class customerserviceimpl implements customerservice{
 private customerdao customerdao;
 
 public void setcustomerdao(customerdao customerdao) {
  this.customerdao = customerdao;
 }

 //用来保存客户
 public void save(customer customer) {
  system.out.println("业务层,保存客户");
  customerdao.save(customer);
 }

}

3)修改applicationcontext.xml文件

先引入hibernate配置文件

<!-- 编写bean,名称都是固定的,由spring提供,用来加载hibernate.cfg.xml的配置文件-->
 <bean id="sessionfactory" class="org.springframework.orm.hibernate5.localsessionfactorybean">
 <!-- 配置路径:当启动服务器时 ,该对象就会被创建,从而加载hibernate.cfg.xml文件,从而生成sessionfactory对象-->
  <property name="configlocation" value="classpath:hibernate.cfg.xml"/>
</bean>

配置平台事务管理:用来管理事务, 注意现在使用的是 hibernate框架,所以需要使用hibernate框架的事务管理器

<!-- 先配置平台事务管理器 -->
 <bean id="transactionmanager" class="org.springframework.orm.hibernate5.hibernatetransactionmanager">
  <!-- 注入事务,session能够管理事务,工厂能够创建session -->
  <property name="sessionfactory" ref="sessionfactory"/>
 </bean>

开启事务注解

  <!-- 开启事务的注解 -->
 <tx:annotation-driven transaction-manager="transactionmanager"/>

去除模板类配置,并为持久层配置sessionfactory

<!-- 以后,dao都需要继承hibernatedaosupport,注入sessionfactory -->
 <bean id="customerdao" class="com.clj.dao.customerdaoimpl">
  <!--<property name="hibernatetemplate" ref="hibernatetemplate"/>-->
  <!-- 这里不注入模板类,而是注入sessionfactory,因为模板需要session(封装了session)-->
  <property name="sessionfactory" ref="sessionfactory"/>
 </bean>

全部代码如下

<?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: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/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
 http://www.springframework.org/schema/tx 
 http://www.springframework.org/schema/tx/spring-tx.xsd">
 <!-- 编写bean,名称都是固定的,有spring提供,用来加载hibernate.cfg.xml的配置文件-->
 <bean id="sessionfactory" class="org.springframework.orm.hibernate5.localsessionfactorybean">
 <!-- 配置路径:当启动服务器时 ,该对象就会被创建,从而加载hibernate.cfg.xml文件,从而生成sessionfactory对象-->
  <property name="configlocation" value="classpath:hibernate.cfg.xml"/>
 </bean>
 
 <!-- 先配置平台事务管理器 -->
 <bean id="transactionmanager" class="org.springframework.orm.hibernate5.hibernatetransactionmanager">
  <!-- 注入事务,session能够管理事务,工厂能够创建session -->
  <property name="sessionfactory" ref="sessionfactory"/>
 </bean>
 
 <!-- 开启事务的注解 -->
 <tx:annotation-driven transaction-manager="transactionmanager"/>
 
 <!-- 配置客户模块 -->
 <!-- 强调:配置的aciton,必须是多列的 -->
 <bean id="customeraction" class="com.clj.web.action.customeraction" scope="prototype">
  <!--注意:struts管理action时,基于其中有个struts-plugin的jar包,其中更改了一个
  常量struts.objectfactory.spring.autowire = name将其打开了,可以自动装配,只需要提供set方法
  但是此时action由spring管理,自动装配失效,所以需要手动进行配置注入
  -->
  <property name="customerservice" ref="customerservice"></property>
 </bean>
 <bean id="customerservice" class="com.clj.service.customerserviceimpl">
  <property name="customerdao" ref="customerdao"></property>
 </bean>
 <bean id="customerdao" class="com.clj.dao.customerdaoimpl">
  <!--<property name="hibernatetemplate" ref="hibernatetemplate"/>-->
  <!-- 这里不注入模板类,而是注入sessionfactory,因为模板需要session(封装了session)-->
  <property name="sessionfactory" ref="sessionfactory"/>
 </bean>
 <!-- 配置模板类(hibernate框架提供的,内部封装了session),此时交给spring管理,如果持久层继承了hibernatedaosupport,则无需配置-->
 <!-- <bean id="hibernatetemplate" class="org.springframework.orm.hibernate5.hibernatetemplate">
  注入sessionfactory 
  <property name="sessionfactory"/>
 </bean>-->
</beans>

4)修改action类

因为注入了业务层实现类,所以此时可以直接调用业务层方法,无须加载bean

package com.clj.web.action;

import org.apache.struts2.servletactioncontext;
import org.springframework.web.context.webapplicationcontext;
import org.springframework.web.context.support.webapplicationcontextutils;

import com.clj.domain.customer;
import com.clj.service.customerservice;
import com.opensymphony.xwork2.actionsupport;
import com.opensymphony.xwork2.modeldriven;

/**
 * 客户的控制层
 * @author administrator
 *
 */
public class customeraction extends actionsupport implements modeldriven<customer>{
 //不要忘记手动new
 private customer customer=new customer();
 public customer getmodel() {
  return customer;
 }
 //提供service成员属性,提供set方法
 private customerservice customerservice;
 public void setcustomerservice(customerservice customerservice) {
  this.customerservice = customerservice;
 }
 /**
  * 保存客户
  * @return
  */
 public string add(){
  system.out.println("web层,保存客户");
  //方式一:创建web的工厂(action由struts2创建)
  /*webapplicationcontext context=webapplicationcontextutils.getwebapplicationcontext(servletactioncontext.getservletcontext());
  customerservice cs=(customerservice) context.getbean("customerservice");
  //调用方法
  cs.save(customer);*/
  customerservice.save(customer);
  return none;
 }
}

spring整合hibernate方式二: (不带有 hibernate.cfg.xml的配置文件)

这里准备删除hibernate的核心配置文件,在删除之前,需要将其配置文件中的相关内容配置到spring的applicatioincontext.xml文件中取

1、查看hibernate.cfg.xml文件中的相关内容

* 数据库连接基本参数(4大参数)

* hibernate相关的属性

* 连接池

* 映射文件

2、引入配置

引入连接池

<!-- 先配置c3p0的连接池 -->
 <bean id="datasource" class="com.mchange.v2.c3p0.combopooleddatasource">
  <property name="driverclass" value="com.mysql.jdbc.driver"/>
  <property name="jdbcurl" value="jdbc:mysql://192.168.174.130:3306/ssh"/>
  <property name="user" value="root"/>
  <property name="password" value="root"/>
 </bean>

修改对应的sessionfactory: 因为已经没有了 hibernate.cfg.xml的配置文件,所以需要修改该配置,注入连接池

引入对象映射文件:因为已经没有了hibernate.cfg.xml的配置文件,不会扫描到该配置文件,需要注入

 <!-- 编写bean,名称都是固定的,有spring提供,用来加载hibernate.cfg.xml的配置文件-->
 <bean id="sessionfactory" class="org.springframework.orm.hibernate5.localsessionfactorybean">
  <!--先加载连接池 -->
  <property name="datasource" ref="datasource"/>
  <!-- 加载方言,加载可选项 -->
  <property name="hibernateproperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.mysqldialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.format_sql">true</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
   </props>
  </property>
  
  <!-- 引入映射的配置文件 -->
  <property name="mappingresources">
   <list>
    <value>com/clj/domain/customer.hbm.xml</value>
   </list>
  
  </property>
 </bean>

现在: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: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/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
 http://www.springframework.org/schema/tx 
 http://www.springframework.org/schema/tx/spring-tx.xsd">
 
 <!-- 先配置c3p0的连接池 -->
 <bean id="datasource" class="com.mchange.v2.c3p0.combopooleddatasource">
  <property name="driverclass" value="com.mysql.jdbc.driver"/>
  <property name="jdbcurl" value="jdbc:mysql://192.168.174.130:3306/ssh"/>
  <property name="user" value="root"/>
  <property name="password" value="root"/>
 </bean>
 
 <!-- 编写bean,名称都是固定的,有spring提供,用来加载hibernate.cfg.xml的配置文件-->
 <bean id="sessionfactory" class="org.springframework.orm.hibernate5.localsessionfactorybean">
  <!--先加载连接池 -->
  <property name="datasource" ref="datasource"/>
  <!-- 加载方言,加载可选项 -->
  <property name="hibernateproperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.mysqldialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.format_sql">true</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
   </props>
  </property>
  
  <!-- 引入映射的配置文件 -->
  <property name="mappingresources">
   <list>
    <value>com/clj/domain/customer.hbm.xml</value>
   </list>
  
  </property>
 </bean>
 
 <!-- 先配置平台事务管理器 -->
 <bean id="transactionmanager" class="org.springframework.orm.hibernate5.hibernatetransactionmanager">
  <!-- 注入事务,session能够管理事务,工厂能够创建session -->
  <property name="sessionfactory" ref="sessionfactory"/>
 </bean>
 
 <!-- 开启事务的注解 -->
 <tx:annotation-driven transaction-manager="transactionmanager"/>
 
 <!-- 配置客户模块 -->
 <!-- 强调:配置的aciton,必须是多列的 -->
 <bean id="customeraction" class="com.clj.web.action.customeraction" scope="prototype">
  <!--注意:struts管理action时,基于其中有个struts-plugin的jar包,其中更改了一个
  常量struts.objectfactory.spring.autowire = name将其打开了,可以自动装配,只需要提供set方法
  但是此时action由spring管理,自动装配失效,所以需要手动进行配置注入
  -->
  <property name="customerservice" ref="customerservice"></property>
 </bean>
 <bean id="customerservice" class="com.clj.service.customerserviceimpl">
  <property name="customerdao" ref="customerdao"></property>
 </bean>
 <!-- 以后,dao都需要继承hibernatedaosupport,注入sessionfactory -->
 <bean id="customerdao" class="com.clj.dao.customerdaoimpl">
  <!--<property name="hibernatetemplate" ref="hibernatetemplate"/>-->
  <!-- 这里不注入模板类,而是注入sessionfactory,因为模板需要session(封装了session)-->
  <property name="sessionfactory" ref="sessionfactory"/>
 </bean>
 <!-- 配置模板类(hibernate框架提供的,内部封装了session),此时交给spring管理,如果持久层继承了hibernatedaosupport,则无需配置-->
 <!-- <bean id="hibernatetemplate" class="org.springframework.orm.hibernate5.hibernatetemplate">
  注入sessionfactory 
  <property name="sessionfactory"/>
 </bean>-->
</beans>

此时可以安心的删除hibernate.cfg.xml文件了

这样ssh整合完毕

六、hibernate模板常用方法

注意:以下代码省略了接口中的演示(偷了个懒,相信初学者不会看不懂)

1)插入:

持久层

package com.clj.dao;

import java.util.list;

import org.hibernate.criterion.detachedcriteria;
import org.springframework.orm.hibernate5.hibernatetemplate;
import org.springframework.orm.hibernate5.support.hibernatedaosupport;

import com.clj.domain.customer;
/**
 * 持久层
 * 继承hibernatedaosupport,内部封装了hibernatetemplate
 * @author administrator
 *
 */
public class customerdaoimpl extends hibernatedaosupport implements customerdao {
 @override
 public void update(customer customer) {
  // todo auto-generated method stub
  this.gethibernatetemplate().update(customer);
 }
}

业务层

package com.clj.service;

import java.util.list;

import org.springframework.transaction.annotation.transactional;

import com.clj.dao.customerdao;
import com.clj.domain.customer;
/**
 * 客户的业务层
 * @author administrator
 *
 */
@transactional
public class customerserviceimpl implements customerservice{
 private customerdao customerdao;
 
 public void setcustomerdao(customerdao customerdao) {
  this.customerdao = customerdao;
 }
 @override
 public void update(customer customer) {
  // todo auto-generated method stub
  customerdao.update(customer);
 }
}

测试类

package com.clj.test;

import java.util.list;

import javax.annotation.resource;

import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.test.context.contextconfiguration;
import org.springframework.test.context.junit4.springjunit4classrunner;

import com.clj.domain.customer;
import com.clj.service.customerservice;

/**
 * 测试hiberante模板类的简单方法
 * @author administrator
 *
 */
@runwith(springjunit4classrunner.class)
@contextconfiguration("classpath:applicationcontext.xml")
public class demo1 {
 @resource(name="customerservice")
 private customerservice customerservice;
 /**
  * 测试插入
  */
 @test
 public void run1(){
  customer customer=new customer();
  customer.setcust_id(1l);
  customer.setcust_name("测试");
  customerservice.update(customer);
 }
 
}

2)以下为指定查询、查询所有、离线查询代码

持久层

package com.clj.dao;

import java.util.list;

import org.hibernate.criterion.detachedcriteria;
import org.springframework.orm.hibernate5.hibernatetemplate;
import org.springframework.orm.hibernate5.support.hibernatedaosupport;

import com.clj.domain.customer;
/**
 * 持久层
 * 继承hibernatedaosupport,内部封装了hibernatetemplate
 * @author administrator
 *
 */
public class customerdaoimpl extends hibernatedaosupport implements customerdao {
 //将数据保存到数据库中(调用模板类(hibernate提供,内部封装了session))
 /*private hibernatetemplate hibernatetemplate;
 
 public void sethibernatetemplate(hibernatetemplate hibernatetemplate) {
  this.hibernatetemplate = hibernatetemplate;
 }*/

 /**
  * 保存客户
  */
 public void save(customer customer) {
  system.out.println("持久层:保存客户");
  this.gethibernatetemplate().save(customer);
 }

 @override
 public void update(customer customer) {
  // todo auto-generated method stub
  this.gethibernatetemplate().update(customer);
 }

 /**
  * 通过主键查询
  */
 public customer getbyid(long id) {
  return this.gethibernatetemplate().get(customer.class, id);
 }
 /**
  * 查询所有
  */
 @override
 public list<customer> findall() {
  string sql="from customer";
  list<customer> list=(list<customer>) this.gethibernatetemplate().find(sql);
  return list;
 }
 /**
  * qbc离线查询
  */
 @override
 public list<customer> findallbyqbc() {
  detachedcriteria criteria=detachedcriteria.forclass(customer.class);
  list<customer> list=(list<customer>) this.gethibernatetemplate().findbycriteria(criteria);
  return list;
 }
}

业务层

package com.clj.service;

import java.util.list;

import org.springframework.transaction.annotation.transactional;

import com.clj.dao.customerdao;
import com.clj.domain.customer;
/**
 * 客户的业务层
 * @author administrator
 *
 */
@transactional
public class customerserviceimpl implements customerservice{
 private customerdao customerdao;
 
 public void setcustomerdao(customerdao customerdao) {
  this.customerdao = customerdao;
 }

 //用来保存客户
 public void save(customer customer) {
  system.out.println("业务层,保存客户");
  customerdao.save(customer);
 }

 @override
 public void update(customer customer) {
  // todo auto-generated method stub
  customerdao.update(customer);
 }

 @override
 public customer getbyid(long id) {
  // todo auto-generated method stub
  return customerdao.getbyid(id);
 }

 @override
 public list<customer> findall() {
  return customerdao.findall();
 }

 @override
 public list<customer> findallbyqbc() {
  // todo auto-generated method stub
  return customerdao.findallbyqbc();
 }
}

测试类

package com.clj.test;

import java.util.list;

import javax.annotation.resource;

import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.test.context.contextconfiguration;
import org.springframework.test.context.junit4.springjunit4classrunner;

import com.clj.domain.customer;
import com.clj.service.customerservice;

/**
 * 测试hiberante模板类的简单方法
 * @author administrator
 *
 */
@runwith(springjunit4classrunner.class)
@contextconfiguration("classpath:applicationcontext.xml")
public class demo1 {
 @resource(name="customerservice")
 private customerservice customerservice;
 /**
  * 测试插入
  */
 @test
 public void run1(){
  customer customer=new customer();
  customer.setcust_id(1l);
  customer.setcust_name("测试");
  customerservice.update(customer);
 }
 /**
  * 测试查询指定的客户
  */
 @test
 public void run2(){
  customer customer=customerservice.getbyid(2l);
  system.out.println(customer);
 }
 /**
  * 查询所有的客户
  */
 @test
 public void run3(){
  list<customer> list=customerservice.findall();
  system.out.println(list);
 }
 /**
  * qbc(离线查询)
  */
 @test
 public void run4(){
  list<customer> list=customerservice.findallbyqbc();
  system.out.println(list);
 }
}

七、关于ssh延迟加载问题

1. 使用延迟加载的时候,再web层查询对象的时候程序会抛出异常!

* 原因是延迟加载还没有发生sql语句,在业务层session对象就已经销毁了,所以查询到的javabean对象已经变成了托管态对象!

* 注意:一定要先删除javassist-3.11.0.ga.jar包(jar包冲突了)

2. 解决办法

spring框架提供了一个过滤器,让session对象在web层就创建,在web层销毁。只需要配置该过滤器即可

* 但是:要注意需要在struts2的核心过滤器之前进行,spring监听器之后配置

<!-- 解决延迟加载的问题 -->
   <filter>
   <filter-name>opensessioninviewfilter</filter-name>
   <filter-class>org.springframework.orm.hibernate5.support.opensessioninviewfilter</filter-class>
  </filter>
  <filter-mapping>
   <filter-name>opensessioninviewfilter</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>

3、演示延迟加载

持久层:调用load方法,此方法时延迟加载的

/**
  * 延迟加载
  */
 @override
 public customer loadbyid(long id) {
  // todo auto-generated method stub
  return this.gethibernatetemplate().load(customer.class, id);
 }

业务层

@override
 public customer loadbyid(long id) {
  // todo auto-generated method stub
  return customerdao.loadbyid(id);
 }

测试类

@test
 public void run5(){
  customer customer=customerservice.loadbyid(2l);
  system.out.println(customer);
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。