一、环境

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. Sublime Text 3:自定义语法高亮

    (http://ilkinulas.github.io/programming/2016/02/05/sublime-text-syntax-highlighting.html) 要安装"P ...

  2. JavaWeb【三、Web程序编写】

    步骤 1.在WebApps创建项目目录,如myapp文件夹 2.编写index.jsp,若路径后不加具体文件名,则默认访问index 3.创建WEB-INF目录,并在其中添加classes.lib文件 ...

  3. Odoo的菜单项

    用户界面的入口是菜单项,菜单项形成一个层级结构,最顶级项为应用,其下一级为每个应用的主菜单.还可以添加更深的子菜单.可操作菜单与窗口操作关联,它告诉客户端在点击了菜单项后应执行什么操作. 菜单项存储在 ...

  4. python常用模块:模块练习

    今日作业: 1.简述 什么是模块 模块就将一些函数功能封装在一个文件内,以‘文件名.py’命名,以“import 文件名”方式调用 模块有哪些来源  自定义.内置.DLL编译器.包模块的格式要求有哪些 ...

  5. Hadoop_11_HDFS的流式 API 操作

    对于MapReduce等框架来说,需要有一套更底层的API来获取某个指定文件中的一部分数据,而不是一整个文件 因此使用流的方式来操作 HDFS上的文件,可以实现读取指定偏移量范围的数据 1.客户端测试 ...

  6. 从n个数里面找最大的两个数理论最少需要比较

    答案是:n+logn-2 过程是这样的: 甲乙比甲胜出,丙丁比丙胜出,最后甲丙比较,甲胜出...容易得出找出最大数为n-1次. 现在开始找出第二大的数字:明显,第二大的数字,一定和甲进行过比较.... ...

  7. Mark点

    MARK点是PCB应用于设计中的自动贴片机上的位置识别点,也被称为基准点.直径为1MM.钢网Mark点是电路板贴片加工中PCB印刷锡膏/红胶时的位置识别点.Mark点的选用直接影响钢网的印刷效率,确保 ...

  8. centos nginx https 配置

    1,如果想配置一个域名到指定目录咋弄呢?下面这个 server { listen ; server_name 这里换成你的域名,例如baidu.com; set $root_path '/home/w ...

  9. less 分页显示文件内容

    1.命令功能 less 是more的增强版,可以分页显示文件内容,而且less打开文件的速度要比vi,more更快.less支持搜索功能,显示行号. 2.语法格式 less  option  file ...

  10. PHP程序员要掌握的技能

    1. Composer 第一点就要提 Composer ,自从 Composer 出现后,PHP 的依赖管理可以变得非常简单.程序内依赖一些类库和框架,直接使用 Composer 引入即可,通过使用 ...