许久没更新博客了!

spring还有一章aop(面向切面),我就没讲述了,你们可以去看下代理模式。

那么我们开始整合:struts2  2.3.4 ,hibernate 5.2.10 ,spring 4.3.10 ,一直以来用到的xml式,那么整合也不例外,就是有些麻烦。另外注解式想了解请留言(虽然ssh已过时)。

首先建立一个maven项目,导入以下依赖:

    <!-- javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.</version>
<scope>provided</scope>
</dependency> <!-- hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2..Final</version>
</dependency> <!-- spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3..RELEASE</version>
</dependency> <!-- spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3..RELEASE</version>
</dependency> <!-- spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3..RELEASE</version>
</dependency> <!-- struts2-core -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.</version>
</dependency> <!-- struts2-spring-plugin -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.</version>
</dependency> <!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.</version>
</dependency> <!-- c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>

由于整个用xml文件,所以分开配置,比较容易理解:

在resources文件夹下建立以下xml

1.applicationContext-public.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" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 配置数据库连接池 c3p0 -->
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"></property>
<property name="password" value="sasa"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="initialPoolSize" value=""></property>
<property name="maxPoolSize" value=""></property> </bean> <!-- 配置sessionFactory -->
<bean id="sessionFactoryBean"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="datasource"></property>
<!--可以不用-->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="mappingLocations" value="classpath:com/entity/*.hbm.xml"></property>
</bean> <!-- 配置事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactoryBean"></property>
</bean> <!-- 配置事务属性 -->
<tx:advice id="myAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="*" />
</tx:attributes>
</tx:advice> <!-- 配置事务切点 -->
<aop:config>
<aop:pointcut expression="execution(* com.dao.*.*(..))"
id="myPointcut" />
<aop:advisor advice-ref="myAdvice" pointcut-ref="myPointcut" />
</aop:config>
</beans>

建立basedao,用户dao层获取seesion来获取数据

package com.dao.impl;

import org.hibernate.Session;
import org.hibernate.SessionFactory; public class BaseDao { private static SessionFactory sessionFactory; public static SessionFactory getSessionFactory() {
return sessionFactory;
} public static void setSessionFactory(SessionFactory sessionFactory) {
BaseDao.sessionFactory = sessionFactory;
} public static Session getSession() {
return sessionFactory.getCurrentSession(); } }

  建立dao类继承basedao

package com.dao.impl;

import java.util.List;

import com.dao.IMenuDao;
import com.entity.Menu; public class MenuDao extends BaseDao implements IMenuDao { public List<Menu> getAll(String rid, String title) {
//引用basedao getSession()方法
getSession().createQuery(sql) .....
      return null;
}
}

2.applicationContext-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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

<!--此为公共类 获取session-->
<bean id="baseDao" class="com.dao.impl.BaseDao">
<!--name为basedao中的引用对象名 其实是set方法名 因为对象的set方法一般与对象名一致 ref为applicationContext-public.xml中配置sessionFactory 的id-->
<property name="sessionFactory" ref="sessionFactoryBean"></property> </bean>

<!--id 自己写 class为类全路径 parent意为父类,即继承了上面的basedao-->
</bean>

<bean id="menuDao" class="com.dao.impl.MenuDao" parent="baseDao">
</bean>



</beans>

建立biz类引用dao类

package com.biz.impl;

import java.util.List;

import com.biz.IMenuBiz;
import com.dao.IMenuDao;
import com.entity.Menu; public class MenuBiz implements IMenuBiz {

//引用dao 提供getter and setter
private IMenuDao iMenuDao; public IMenuDao getiMenuDao() {
return iMenuDao;
} public void setiMenuDao(IMenuDao iMenuDao) {
this.iMenuDao = iMenuDao;
} public List<Menu> getAll(String rid,String title) { return iMenuDao.getAll(rid,title);
} }

3.applicationContext-biz.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" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

<bean id="menuBiz" class="com.biz.impl.MenuBiz">

<!--name biz类中引用对象名  ref为applicationContext-dao.xml中对象dao的id-->

<property name="iMenuDao" ref="menuDao"></property>
</bean>

</beans>

建立Action类引用biz

package com.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.biz.IMenuBiz;
import com.entity.Menu;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; import net.sf.json.JSONArray; public class MenuAction extends ActionSupport implements ModelDriven<Menu> { private static final long serialVersionUID = 1L; private Menu menu = new Menu(); //引用的biz
private IMenuBiz iMenuBiz; public IMenuBiz getiMenuBiz() {
return iMenuBiz;
} public void setiMenuBiz(IMenuBiz iMenuBiz) {
this.iMenuBiz = iMenuBiz;
} public Menu getModel() { return menu;
} }

4.建立applicationContext-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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <bean id="menuAction" class="com.action.MenuAction" scope="prototype">
//配置与dao,biz意思一样
<property name="iMenuBiz" ref="menuBiz"></property>
</bean> </beans>

最后建立struts.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>
<package name="mypackage" extends="struts-default">
<!--calss为applicationContext-action.xml对应的id-->
<action name="menu*" class="menuAction" method="{1}">
<result name="success">/index.jsp</result>
<result name="error">/login.jsp</result>
</action>
</package>
</struts>

web.xml配置

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app>
<display-name>Archetype Created Web Application</display-name> <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>*.action</url-pattern>
</filter-mapping> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> </web-app>

整个下来很容易配错,要细心。关于在其中的许多解释就没说了,附图一张:  ssh完

初学者易上手的SSH-整合的更多相关文章

  1. 初学者易上手的SSH-struts2 01环境搭建

    首先,SSH不是一个框架,而是多个框架(struts+spring+hibernate)的集成,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层Web应用程序. 集成SSH框 ...

  2. 初学者易上手的SSH-struts2 05拦截器与自定义拦截器

    因为自己对于struts2也不是很了解,这章将是struts2的最后一章了.那么这一章主要介绍的是拦截器以及怎么样来自定义一个拦截器. struts2的拦截器位于struts2-core(核心包)-& ...

  3. 初学者易上手的SSH-hibernate01环境搭建

    这里我们继续学习SSH框架中的另一框架-hibernate.那么hibernate是什么?Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序 ...

  4. 2017年11月1日 初学者易上手的SSH-spring 01控制反转(IOC)

    这章开始学习SSH中最后的一个框架spring.Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用. 首先就来学习一下I ...

  5. 初学者易上手的SSH-spring 01控制反转(IOC)

    这章开始学习SSH中最后的一个框架spring.Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用. 首先就来学习一下I ...

  6. 初学者易上手的SSH-hibernate04 一对一 一对多 多对多

    这章我们就来学习下hibernate的关系关联,即一对一(one-to-one),一对多(one-to-many),多对多(many-to-many).这章也将是hibernate的最后一章了,用于初 ...

  7. 初学者易上手的SSH-struts2 02Action获取表单数据-通配符

    在上一章中,我们已经搭建好了struts2的一个开发环境,那么这一章就来做一个简单的登录功能,以及介绍和使用struts2里面一个重要的东西-通配符. 第一步,在WebContent下面新建一个log ...

  8. 初学者易上手的SSH-struts2 04值栈与ognl表达式

    什么是值栈?struts2里面本身提供的一种存储机制,类似于域对象,值栈,可以存值和取值.,特点:先进后出.如果将它当做一个容器的话,而这个容器有两个元素,那么最上面的元素叫做栈顶元素,也就是所说的压 ...

  9. 初学者易上手的SSH-hibernate02 三种查询方式

    在上一章中已经搭建好了一个hibernate的环境,那么这一章我们就使用这个环境来进行基本CRUD.在这之前我们先了解一个东西:主键生成策略.就是当向数据库表中插入记录的时候,这个记录的主键该如何生成 ...

随机推荐

  1. .Net Web开发技术栈

    有很多朋友有的因为兴趣,有的因为生计而走向了.Net中,有很多朋友想学,但是又不知道怎么学,学什么,怎么系统的学,为此我以我微薄之力总结归纳写了一篇.Net web开发技术栈,以此帮助那些想学,却不知 ...

  2. 【20171025早】alert(1) to win 练习

    本人黑绝楼,自称老黑,男,25岁,曾经在BAT工作过两年,但是一直都是底层人员,整天做重复性工作,甚敢无趣,曾和工作十年之久的同事聊天,发现对方回首过往,生活是寡淡如水,只有机械性工作.旋即老黑毅然决 ...

  3. 我搞zabbix的那两天

    摘要:在生产环境上对服务器进行网络参数(比如CPU.内存等)的监控是很必要的,比如当服务器网络参数如内存不够用.磁盘空间快要占满时及时通知运维人员进行处理,保证服务器系统的安全.而zabbix就是这么 ...

  4. Android开发中的OpenCV霍夫直线检测(Imgproc.HoughLines()&Imgproc.HoughLinesP())

    本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃   //2017-04-21更新: 很多网友希望能得到源码,由于在公司做的,所以不太方便传出来 ...

  5. RabbitMQ-Windows单机集群搭建

    1.先安装Erlang http://www.erlang.org/downloads,安装完成后,设置环境变量: 变量名:ERLANG_HOME 变量值:D:\Program Files\erl9. ...

  6. git使用教程之git分支

    1 分支简介 让我们来看一个简单的分支新建与分支合并的例子,实际工作中你可能会用到类似的工作流. 你将经历如下步骤: 开发某个网站. 为实现某个新的需求,创建一个分支. 在这个分支上开展工作. 正在此 ...

  7. defaultView and parentWindow

    defaultView     只读的 which is used to represent the currently rendered view of the document 返回的值通常是包含 ...

  8. ruby 异常处理 begin rescue end

    begin 代码1 rescue 代码 end 如果执行 代码1 发生异常 则转至 代码2 若正常,则执行完跳出

  9. jQuery_事件学习

    一.click事件 click事件----鼠标单击事件 $('.bt').click(function() { alert("本身的事件");}) 当class为bt的div被但单 ...

  10. PHP代码审计---基础

    PHP伪协议 PHP伪协议事实上是其支持的协议与封装协议,支持的种类有以下12种. * file:// - 访问本地文件系统 * http:// - 访问 HTTP(s) 网址 * ftp:// - ...