SSH知识点回顾

本文详细介绍了如何搭建基于Struts2、Spring和Hibernate的SSM框架,并实现了它们之间的整合。主要内容包括项目的创建、配置文件的编写、包结构的设计以及各组件间的集成步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,创建web项目,引入相应架包(推荐使用maven管理)



2,引入相应配置文件

2.1struts2配置文件

web.xml

struts2核心过滤器的配置

<filter>
  <filter-name>struts</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>struts</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
   


</struts> 
2.2hibernate配置文件

hibernate.cfg.xml

映射文件

2.3spring配置文件

web.xml

spring核心监听器的配置

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
</context-param>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans  
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    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"  
    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/aop  
     http://www.springframework.org/schema/aop/spring-aop.xsd 
    http://www.springframework.org/schema/tx   
    http://www.springframework.org/schema/tx/spring-tx.xsd">  

   
</beans>

3、创建包结构

com.xzh

    action

        UserAction

    bean

        UserBean

        UserBean.cfg.xml

    dao

        impl

            UserDaoImpl

        UserDao

    service

        impl

            UserServiceImpl

        UserService


4,基本框架整合完毕,开始进行两两整合

4.1struts2整合spring框架

编写Action,service和DAO

4.2spring整合hibernate

jdbc.properties

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/shopping?useUnicode=true&characterEncoding=UTF-8
jdbc.username:root
jdbc.password:root

4.2.1引入外部的属性文件:jdbc.properties

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

4.2.2配置c3p0连接池

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

4.2.3配置hiberna相关属性sessionFactory

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

4.2.3.1注入连接池

<property name="dataSource" ref="dataSource"/>

4.2.3.2配置hibernate属性

<!-- 配置hibernate属性 -->
<property name="hibernateProperties">
   <props>
      <!-- 方言,这里指数据库使用的是MySQL -->
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
      <!-- 显示SQL语句 -->
      <prop key="hibernate.show_sql">true</prop>
      <!-- 对SQL语句进行排版,更方便理查看和理解 -->
      <prop key="hibernate.format_sql">true</prop>
      <!-- update 加载hibernate自动更新数据库 -->
      <prop key="hibernate.hbm2ddl.auto">update</prop>
   </props>
</property>

4.2.3.3加载hibernate的映射文件

<property name="mappingResources">
   <list>
      <value>com/xzh/bean/Collect.hbm.xml</value>
      <value>com/xzh/bean/CommodityDetails.hbm.xml</value>
      <value>com/xzh/bean/Configuration.hbm.xml</value>
      <value>com/xzh/bean/Footprint.hbm.xml</value>
      <value>com/xzh/bean/GoodsDetail.hbm.xml</value>
      <value>com/xzh/bean/MenuInfo.hbm.xml</value>
      <value>com/xzh/bean/OrderFrom.hbm.xml</value>
      <value>com/xzh/bean/OrderFromDetails.hbm.xml</value>
      <value>com/xzh/bean/ShoppingCart.hbm.xml</value>
      <value>com/xzh/bean/Specification.hbm.xml</value>
      <value>com/xzh/bean/User.hbm.xml</value>
   </list>
</property>

5,dao层注入session factory

<bean id="shoppingDao" class="com.xzh.dao.impl.ShoppingDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

通过继承hibernateDaoSupport来直接调用hibernate

6,添加事务管理

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
添加注解
<tx:annotation-driven transaction-manager="transactionManager"/>
@Transactional
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值