SSH整合

SSH是Struts2、Spring和hibernate三个框架的整合,在这里就不对3个框架的原理进行详细的描述,本文章主要是为了整理一下对这3个框架的整合起来的基本配置。

现在开始我们的配置吧。

文章声明:本篇文章主要是对SSH的实现进行的一个整理,并非是一个完整的项目,在这里的所有数据都是测试数据,把握这篇文章的核心就好了。配置文件,路径都会进行图片的展示,和代码案例。

1、导包(38个)

我这里提供这38个包,点击链接:SSH必备Jar包

由于包特别的多,就在这里图片显示了。

1

2、再就是我们在创建我们的类的时候需要创建的几个文件个配置信息

  • 在根目录下创建一个源文件夹config,和src文件夹同级别
  • 在config里面创建beans.xml(spring的配置)、struts2.xml、hibernate.cfg.xml
  • 在src下,我们有action、dao、service、po四个基本的包名
  • jsp文件放置于WEB-INF/JSP/中

2 

3、现在所有的准备都已经准备好了,开始写我们的配置了。

我们首先配置我们最常用的spring的配置文件beans.xml

配置文件是采用的IOC注解和aop的注解的开发。同时这里配置了hibernate的事务处理。

 

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    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">
    
    <!-- 注解扫描 -->
    <context:component-scan base-package="com.mepapa"/>
    
    <!-- 引入式: 整合hibernate -->
    <bean id="sf" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    </bean>
    
    <!-- 管理hibernate事务的类 TransactionManager -->
    <bean id="tx" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sf"></property>
    </bean>
    
    <!--通过注解管理:使用注解通过tx事务管理器管理hibernate事务 -->
    <tx:annotation-driven transaction-manager="tx"/>
    
    <!-- 开启@AspectJ注解支持 -->    
    <aop:aspectj-autoproxy />
</beans>

 

4、这一步配置我们ORM,也就是hibernate.cfg.xml

我们在po类还有一个hibernate的配置文件,我们下面会对另一个配置文件的详细列表

<?xml version="1.0" encoding="UTF-8"?>
<!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>
    <!-- 数据库 -->
    <property name="connection.driver_class">
        com.mysql.jdbc.Driver
    </property>
    <property name="connection.url">
        jdbc:mysql://localhost:3306/bdycdb
    </property>
    <property name="connection.username">root</property>
    <property name="connection.password">123456</property>
    <!-- 连接池 -->
    <property name="hibernate.c3p0.max_size">10</property>
    <property name="hibernate.c3p0.min_size">2</property>
    <property name="hibernate.c3p0.acquire_increment">5</property>
    <property name="hibernate.c3p0.timeout">600</property>

    <!-- Sql方言 -->
    <property name="dialect">
        org.hibernate.dialect.MySQLDialect
    </property>
    <!-- 是否格式化 -->
    <property name="format_sql">true</property>
    <!-- 是否开启显示语句 -->
    <property name="show_sql">true</property>

    <!-- 创建表的方式 -->
    <property name="hbm2ddl.auto">update</property>
    <!-- 映射 -->
    <mapping resource="com/mepapa/po/User.hbm.xml" />

</session-factory>

</hibernate-configuration>

这里是po类中的User.hbm.xml,对了user类这里就不累述了,自己看着写写就好了。

这里不是主要讲hibernate的用法,这里就不多进行解释了。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.mepapa.po">
    <class name="User" table="t_user">
        <id name="id" column="user_id">
            <generator class="native"></generator>    
        </id>
        <property name="username"/>
        <property name="password"/>
        <property name="birthday"/>
    </class>
</hibernate-mapping>

5、现在进行我们struts2的配置struts.xml

在这里最重要的就是struts对象交给spring进行管理,其他和struts的配置相差无几。

<?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>
    <constant name="struts.configuration.xml.reload" value="true"/>
    <!-- 让struts2的aciton对象交给spring管理 -->
    <constant name="struts.objectFactory" value="spring"></constant>    
    
    <package name="user" namespace="/user" extends="struts-default">        
        <action name="userAction_*" class="userAction" method="{1}">
            
            <result name="addUI">/WEB-INF/jsp/user/add.jsp</result>
            <result name="list" type="redirectAction">
                <param name="namespace">/user</param>
                <param name="actionName">userAction_list.action</param>
            </result>
            <result name="listUI">/WEB-INF/jsp/user/list.jsp</result>
        </action> 
    </package>
</struts>

6、现在我们已经对这3个框架都进行了配置,很多新手会认为现在已经配置完毕了,可以用了。这是错的,很多人在初学的时候忘掉了很重要的配置,这是我们经常犯的错误。一定要谨记。

我们的最后一步是web.xml进行配置。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:beans.xml</param-value>
  </context-param>
  
  <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>*.action</url-pattern>
  </filter-mapping>
  
   <display-name></display-name>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

7、到这里位置这就是我们的所有的SSH的配置了,在以后的使用中我们可以根据我们的使用需求进行配置,这里仅是我们使用SSH框架的一个基本配置,在以后的使用中进行调整就行了。

我们的文章就到这里了,希望大家能喜欢的我的博客。

刚刚来到这里,有不对的地方,欢迎大家指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值