一、环境

SSH使用的版本:struts2.3.14、spring3.2.2、hibernate4.2.0

数据库:MYSQL

tomcat版本:apache-tomcat-7.0.42

 

二、所需要导入的jar包

2.1 struts2的jar包

  同时,整合hibernate还需要一下jar包:

2.2 spring3的jar包

2.3 hibernate4的jar包

  导入/lib/required文件夹下的所有jar包

  配置连接池需要导入/lib/optional/c3p0文件夹中的所有jar包

  数据库使用的是MYSQL,因此还需要mysql的连接驱动:mysql-connector-java-5.1.22-bin.jar

2.4 用于日志的jar包

log4j-1.2.17.jar,slf4j-api-1.7.5.jar,slf4j-log4j12-1.7.5.jar

2.5 AspectJ相关的jar包

aopalliance-1.0.jar,aspectjrt.jar,aspectjweaver.jar

三、web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>BillLogger</display-name> <!-- WebApp Root -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param> <!-- Struts2 Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,com/billLogger/resources/struts.xml</param-value>
</init-param>
</filter>
<!-- Struts2 Filter Mapping -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- Log4j ConfigurationFile Location -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:com/billLogger/resources/log4j.properties</param-value>
</context-param> <!-- Spring Log4j Listener -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener> <!-- Spring Web Request Listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

  struts的配置文件struts.xml默认存放路径在/WEB-INF/classes目录下,即将struts.xml放在src的目录下。我这里把配置文件放在com.billLogger.resources包内,因此要在init-param标签设置config的值。

  需要注意的是,若设置了<param-name>config</param-name>参数,则struts-default.xml和struts-plugin.xml原来struts2默认加载的文件也要手动指定,否则不会自动加载。

四、配置struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true"></constant><!-- default set
to false for prod --> <package name="bills" namespace="/bills" extends="struts-default">
<action name="*" class="com.billLogger.actions.bill.{1}">
<result>/views/bills/{1}.jsp</result>
<result name="redirect" type="redirect">${redirectUrl}</result>
</action>
</package> <package name="root-redirect" namespace="/" extends="struts-default">
<action name="">
<result type="redirect">bills/Listing.action</result>
</action>
</package>
</struts>

五、配置hibernate.cfg.xml

<!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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/moneymanagement</property>
<property name="connection.username">root</property>
<property name="connection.password">tyc1234</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable c3p0 connection pooling,beacause hibernate pooling is not prod-ready.
Apparently connection.provider_class is needed in hibernate 3+ -->
<!-- <property name="connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property> -->
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.max_size">100</property>
<property name="hibernate.c3p0.idle_test_period">30</property> <!-- Echo all ececuted SQLto stdout for debugging -->
<property name="show_sql">true</property> <!-- 对象与数据库表格映像文件 -->
<mapping resource="com/billLogger/mode/bill.hbm.xml" />
<mapping resource="com/billLogger/mode/category.hbm.xml" />
</session-factory>
</hibernate-configuration>

  在配置c3p0连接池的时候,hibernate3一定要写上

<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>这句话是用于指定hibernate的连接方式,如果没有的话,将不会指定c3p0为hibernate的连接池,c3p0的连接类直接在核心jar包中。在hibernate4中c3p0有专用jar包,这里不需要设置connection.provider_class。

六、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:p="http://www.springframework.org/schema/p" 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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- SessionFactory -->
<bean id="sessionFactory" scope="singleton" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"<!-- 和hibernate3导入的不同 -->
    p:configLocation="classpath:/com/billLogger/resources/hibernate.cfg.xml" /> <!-- TransactionManager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"<!-- 和hibernate3导入的不同 -->
    p:sessionFactory-ref="sessionFactory" />
<!-- Spring Advice -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"></tx:method>
<tx:method name="*"></tx:method>
</tx:attributes>
</tx:advice>
<!-- Spring Aop Config -->
<aop:config>
<aop:pointcut id="pointcut" expression="
execution(* com.billLogger.services.*Impl.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
</aop:config> <!-- Dao -->
<bean id="billDao" class="com.billLogger.dao.BillDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean> <!-- Service -->
<bean id="billService" class="com.billLogger.services.BillService">
<property name="billDao">
<ref bean="billDao" />
</property>
</bean> <!-- Action -->
<bean id="billListAction" class="com.billLogger.actions.bill.Listing" scope="session">
<property name="billService">
<ref bean="billService" />
</property>
</bean>
</beans>

  这里想说下spring在对hibernate4的支持上和3是有区别的。在Hibernate4.0以上的版本,session已经自己封装了事务处理,所以在spring3.1以上的版本把HibernateTemplate去掉了。在hibernate4中获取session的时候,不需要再继承HibernateDaoSupport类,可以直接baseDao类中添加一个SessionFactory属性,再添上它的setter方法。我们使用的DAO层的实现层是要添加到applicationContext.xml中的(其实也就是spring的依赖注入)。

七、java代码

7.1 mode层

package com.billLogger.mode;

import java.sql.Date;

public class Bill {
Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
} Float money;
public Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
} Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
} Integer account;
public Integer getAccount() {
return account;
}
public void setAccount(Integer account) {
this.account = account;
}
}

7.2 DAO层

package com.billLogger.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory; import com.billLogger.mode.Bill; /*
*@author Jeniss 2013-10-14 下午4:09:26
*@tag
*/
public class BillDao{
protected SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
} public Session getSession() {
return sessionFactory.getCurrentSession();
} public List<Bill> getAllBills() {
String hql = "from Bill";
Query query = getSession().createQuery(hql);
List<Bill> bills = query.list();
return bills;
}
}

7.3 service层

package com.billLogger.services;

import java.util.List;

import com.billLogger.dao.BillDao;
import com.billLogger.mode.Bill; /*
*@author Jeniss 2013-10-14 下午4:13:32
*@tag
*/
public class BillService {
private BillDao billDao;
public void setBillDao(BillDao billDao) {
this.billDao = billDao;
} public List<Bill> getAllBills() {
List<Bill> bills = billDao.getAllBills();
return bills;
}
}

7.4 action层

package com.billLogger.actions.bill;

import java.util.Map;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.ActionSupport; /*
*@author Jeniss 2013-10-8 下午5:23:37
*@Tag
*/
public class ActionBase extends ActionSupport implements RequestAware, SessionAware, ApplicationAware {
private static final long serialVersionUID = 1L; protected Map<String, Object> request;
protected Map<String, Object> session;
protected Map<String, Object> application;
public Map<String, Object> getRequest() {
return request;
} public Map<String, Object> getSession() {
return session;
} public Map<String, Object> getApplication() {
return application;
} @Override
public void setApplication(Map<String, Object> application) {
this.application = application;
} @Override
public void setSession(Map<String, Object> session) {
this.session = session;
} @Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}
}
package com.billLogger.actions.bill;

import java.util.List;

import com.billLogger.mode.Bill;
import com.billLogger.services.BillService; public class Listing extends ActionBase{
private static final long serialVersionUID = 1L; private BillService billService;
public void setBillService(BillService billService) {
this.billService = billService;
} List<Bill> bills;
public List<Bill> getBills() {
return bills;
} public String execute() throws Exception{
bills = billService.getAllBills();
return SUCCESS;
}
}

  环境搭建成功。这次搭建环境花了两天的时间,在搭的过程中,主要是对打印的日志不太熟悉,造成了查错能力降低。通过搭建这个环境,对SSH各个层的作用认识更形象了。

  hibernate通过Session接口实现了对数据的操作(例如:对数据库的数据进行增删改查操作),其SessionFactory接口可以调用Session。

  spring主要特性是DI和AOP。在SSH中的作用是连接hibernate和spring。在applicationContext文件中定义sessionFactory,通过设置配置文件路径,实现对hibernate配置文件的注入和解析。定义sessionManager(事务声明),通过注入sessionFactory,得到hibernate session,实现hibernate的事务管理。把sessionFactory注入DAO层,在到daoImpl类中可以使用hibernate session。再通过依赖注入,实现service层和action。

  struts则是控制action和jsp页面的连接。

Struts2.3+Spring3.2+Hibernate4.2框架搭建的更多相关文章

  1. struts2 spring3.2 hibernate4.1 框架搭建 整合

    ssh是企业开发中常遇到的框架组合,现将框架的搭建过程记录下来,以便以后查看.我的搭建过程是,首先struts,然后spring,最后hibernate.struts2的最新版本为2.3.8,我下载的 ...

  2. 基于全注解的SpringMVC+Spring4.2+hibernate4.3框架搭建

    概述 从0到1教你搭建spring+springMVC+hibernate整合框架,基于注解. 本教程框架为基于全注解的SpringMVC+Spring4.2+hibernate4.3,开发工具为my ...

  3. struts2.3+spring3.2+hibernate4.2例子

    有些教程比较老,可是版本更新不等人,基于马士兵老师小例子,自己重新引用了新的包,调试确实有点烦人,但是通过英文文档和google解决问题.官网的更新超快,struts2.3+spring3.2+hib ...

  4. SSH的简单入门体验(Struts2.1+Spring3.1+Hibernate4.1)- 查询系统(上)

    所谓SSH,指的是struts+spring+hibernate的一个集成框架,它是目前较流行的一种Web应用程序的开源框架. 集成SSH框架的系统从职责上分为四层:表示层.业务逻辑层.数据持久层和域 ...

  5. SSH的简单入门体验(Struts2.1+Spring3.1+Hibernate4.1)- 查询系统(下)

    我们继续吧,SSH最大的优点就是实现的系统的松耦合,能够将后台和前台有机的分离开来. 一.目录结构 一个好的程序要有一个好的开始.我们先来看看整个目录结构吧 主要的是三层架构概念,或者说是mvc的概念 ...

  6. 最新版本的Struts2+Spring4+Hibernate4三大框架整合(截止2014-10-15,提供源码下载)

    一. 项目名称:S2316S411H436 项目原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6 + Quartz2.2.1 源代码下载地址: 基本版:ht ...

  7. Struts2+Hibernate4+Spring4框架整合搭建Java项目原型

    收藏 http://www.cnblogs.com/mageguoshi/p/5850956.html Struts2+Hibernate4+Spring4框架整合搭建Java项目原型

  8. struts2+hibernate+spring注解版框架搭建以及简单测试(方便脑补)

    为了之后学习的日子里加深对框架的理解和使用,这里将搭建步奏简单写一下,目的主要是方便以后自己回来脑补: 1:File--->New--->Other--->Maven--->M ...

  9. struts2+hibernate+spring配置版框架搭建以及简单测试(方便脑补)

    为了之后学习的日子里加深对框架的理解和使用,这里将搭建步奏简单写一下,目的主要是方便以后自己回来脑补: 1:File--->New--->Other--->Maven--->M ...

随机推荐

  1. MongoDB 各个位版本下载地址

    官网首页下载需要填写资料 windows版本 Linux版本

  2. 使用Django开发简单接口:文章增删改查

    目录 1.一些准备工作 安装django 创建django项目 创建博客应用(app) 2.models.py 3.django admin 登录 创建超级用户 4.修改urls.py 5.新增文章接 ...

  3. 制作linux云主机镜像

    目录 制作linux云主机镜像 1.物理机环境准备 2.安装kvm虚拟机 3.操作虚拟机 4.在物理机上处理镜像 5.拷贝制作好的raw格式的镜像 6.发布镜像到云平台 制作linux云主机镜像 1. ...

  4. AWK程序设计语言

    一. AWK入门指南 Awk是一种便于使用且表达能力强的程序设计语言,可应用于各种计算和数据处理任务.本章是个入门指南,让你能够尽快地开始编写你自己的程序.第二章将描述整个语言,而剩下的章节将向你展示 ...

  5. facenet中pairs文件制作

    1.对图片进行重命名 """Rename the image based on the folder name""" import os i ...

  6. httpleaks及url的小技巧(http:evil)

    HTTP Leak攻击简介 当前Web技术下包含了大量HTML元素和属性,这些HTML元素和属性会请求一些外部资源,而在它们的HTTP请求过程中,可能存在潜在的敏感信息泄露.为此,德国著名网络安全公司 ...

  7. YII2.0.12兼容PHP7.2版本升级

    YII2.0.12兼容PHP7.2版本升级 报错信息: FastCGI sent in stderr: "PHP message: PHP Fatal error:  Cannot use ...

  8. nodejs研发环境

    https://blog.csdn.net/angl129/article/details/90696251

  9. vs调试时,不显示局部变量

    为了测试一个函数的返回值,就在某个函数里加了一个局部变量,调试却不显示所添加变量的信息. 你一定设置成了release 模式.改为debug就可以了. 比较弱智的问题,mark一下.

  10. SSH框架整合报错org.springframework.web.context.ContextLoaderListener

    是因为在导入的jar包中缺少了一个 org.springframework.web_3.1.4.release.jar 在网上下载即可 下载地址: http://www.java2s.com/Code ...