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!!!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容