Spring+struts2+hibernate整合篇(ssh)

创建ssh工程

我的电脑装的是MySql5.7的版本
使用的Spring的版本是:2.3.5(只要是2.3的都行的了),Struts2的版本:2.3.24,hibernate版本:3.6

导入包

包目录:(一共38个包)

所有包集合.png

Struts的开发包:

struts2基础包

struts2-convention-plugin-2.3.xx.jar--struts2的注解开发包
struts2-spring-plugin-2.3.xx.jar--struts2用于整合spring的包(注意,在spring里面也有类似的包,只需要导入其中一个即可。)

hibernate开发包:

hibernate基础包

slf4j整合log4j的jar包(用于日志记录)
jdbc---数据库驱动包

Spring开发包:

  • ioc:
    spring-beans-3.xxx.jar
    spring-context-3.xxx.jar
    spring-core-3.xxx.jar
    spring-expression-3.xxx.jar
    log4j-xxx.jar
    commons-logging-xxx.jar

  • aop:
    spring-aop-xxx.jar
    spring-aspects-xxx.jar
    aopalliance-xxx.jar
    aspectj.weaver-xxx.jar

  • 其他
    spring-tx-xxx.jar
    spring-jdbc-xxx.jar
    spring-orm-xxx.jar
    spring-web-xxx.jar
    spring-test-xxx.jar
    springsource.com.mchange.v2.c3p0-xxx.jar

配置文件

配置文件目录:

配置文件目录

(lib是用来放依赖的jar包的)

基础类的目录

基础类

其中hbm.xml的配置根据个人而异。

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!--Spring框架核心监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--装载Spring文件配置-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!--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>
    
</web-app>

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

    <!--引入外部的属性文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--配置连接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--配置hibernate的相关属性-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <!--注入连接池-->
        <property name="dataSource" ref="dataSource"/>
        <!--配置hibernate属性-->
        <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>
        <!--加载hibernate的映射文件-->
        <property name="mappingResources">
            <list>
                <value>com/demo/ssh/domain/Product.hbm.xml</value>
            </list>
        </property>
    </bean>

    <!--Action类-->
    <bean class="com.demo.ssh.action.ProductAction" id="productAction" scope="prototype">
        <property name="productService" ref="productService"/>
    </bean>

    <!--Service类-->
    <bean class="com.demo.ssh.service.ProductService" id="productService">
        <property name="productDao" ref="productDao"/>
    </bean>

    <!--Dao类-->
    <bean class="com.demo.ssh.dao.ProductDao" id="productDao">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!--配置事务管理器-->
    <bean class="org.springframework.orm.hibernate3.HibernateTransactionManager" name="transactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

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

</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="ssh" extends="struts-default" namespace="/">
        <action name="product_*" class="com.demo.ssh.action.ProductAction" method="{1}">
            <result name="error">index.jsp</result>
            <result name="success"></result>
        </action>
    </package>

</struts>

大家如果学过这三个框架的话应该都能看懂这些东西了,看不到先分开学一下这三个框架。其中spring的applicationContext.xml代替hibernate.cfg.xml配置了数据库的连接和事务了,不需要hibernate.cfg.xml这个配置文件了。Spring整合了hibernate。

基础类的部分代码

//继承了HibernateDaoSupport后能直接使用工程模式,省去麻烦
public class ProductDao extends HibernateDaoSupport {
    
    public void save(Product product){
        System.out.println ("Product Dao work!" );
        //调用模版来保存对象到数据库
        this.getHibernateTemplate ().save (product);
    }
}
//这个注解是事务用的
@Transactional
public class ProductService {
    
    private ProductDao productDao;
    
    public void setProductDao (ProductDao productDao) {
        this.productDao = productDao;
    }
    
    public void save(Product product){
        System.out.println ("Product Service work!" );
        productDao.save (product);
    }
    
}

省略了action类的方法,省点空间,自行脑补。

jsp页面代码

<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>保存商品的页面</h1>
<s:form action="product_save" method="POST" namespace="/" theme="simple">
    <table border="1" width="400">
        <tr>
            <td>商品名称</td>
            <td><s:textfield name="pname"/> </td>
        </tr>
        <tr>
            <td>商品价格</td>
            <td><s:textfield name="price"/> </td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="添加" /></td>
        </tr>
    </table>
</s:form>
</body>
</html>

载入jsp界面,

jsp页面.png

点击添加后,后台成功把数据入库。


后台.png

成功整合ssh!!!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,539评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,594评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,871评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,963评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,984评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,763评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,468评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,850评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,002评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,144评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,823评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,483评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,026评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,150评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,415评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,092评论 2 355

推荐阅读更多精彩内容