SSH-----------整合struts\spring\hibernate

 

  1. SSH整合

2.1 web 整合spring

配置tomcat加载spring的配置文件

第一步:需要添加SSH-----------整合struts\spring\hibernate

第二步:在web.xml配置spring的监听

SSH-----------整合struts\spring\hibernate

出现下面的错误是配置文件加载位置不对,在web.xml改成classpath目录下

SSH-----------整合struts\spring\hibernate

SSH-----------整合struts\spring\hibernate

 

第三步:创建Servlet获取Spring的应用上下文件ApplicationContext

SSH-----------整合struts\spring\hibernate

 

 

2.2 web整合struts+hibernate+spring

整合版本

struts-2.3.33-all

spring-framework-3.0.2.RELEASE

hibernate-distribution-3.6.10.Final-dist

第一步:jar包整合

Struts的jar包

SSH-----------整合struts\spring\hibernate

 

 

Spring的jar包

基础:4+1 , beans、core、context、expression , commons-logging (struts已经导入)

AOP:aop联盟(aopalliance)、spring aop 、aspect规范(aspect.weaver)、spring aspect

db:jdbc、tx

测试:test

web开发:spring web

驱动:mysql

连接池:c3p0

整合hibernate:spring orm

 

 

Hibernate的jar包

核心包

SSH-----------整合struts\spring\hibernate

SSH-----------整合struts\spring\hibernate

required包下的介绍

SSH-----------整合struts\spring\hibernate

jpa用于注解开发@Entity @Id

 

整合log4j

导入 log4j...jar (struts已经导入)

SSH-----------整合struts\spring\hibernate

整合(过渡):slf4j-log4j12-1.7.2.jar

SSH-----------整合struts\spring\hibernate

 

二级缓存

Commons-loggin.jar已经存在

SSH-----------整合struts\spring\hibernate

 

 

整合包

spring整合hibernate: spring orm

struts 整合spring:struts2-spring-plugin-2.3.15.3.jar

 

删除重复jar包

SSH-----------整合struts\spring\hibernate

第二步:spring整合hibernate的单元测试

创建表

create table t_user(

  id int primary key auto_increment,

  username varchar(50),

  password varchar(32),

  age int

);

 

po类和映射文件

public class User {

   private Integer id;

   private String username;

   private String password;

   private Integer age;

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

    <class name="com.gyf.borrowsys.domain.User" table="t_user">

        <id name="id">

            <generator class="native"></generator>

        </id>

       

        <property name="username"></property>

        <property name="password"></property>

        <property name="age"></property>

    </class>

</hibernate-mapping>

 

 

dao        

SSH-----------整合struts\spring\hibernate

 

Service

SSH-----------整合struts\spring\hibernate

 

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>

          <!-- 1、配置数据库连接的4个参数 -->

          <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

          <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/web_ssh</property>

          <property name="hibernate.connection.username">root</property>

          <property name="hibernate.connection.password">123456</property>

         

          <!-- 2、是否显示sql语句 -->

          <property name="show_sql">true</property>

         

          <!-- 3、是否格式化sql语句 -->

          <property name="format_sql">true</property>

         

          <!-- 4Hiberante映射与DDl语句的策略 update【常用】: 如果数据库没有表,会创建表

           hibernate.-->

          <property name="hibernate.hbm2ddl.auto">update</property>

    

          <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

              

          <!-- 配置JavaBean与表的映射文件 -->

          <mapping resource="com/gyf/borrowsys/domain/User.hbm.xml"/>

         

     </session-factory>

</hibernate-configuration>

 

applicationContext.xml

SSH-----------整合struts\spring\hibernate

 

单元测试

SSH-----------整合struts\spring\hibernate

 

配置Hibrenate的事务

SSH-----------整合struts\spring\hibernate

 

简化:去除hibernate.cfg.xml文件

<!-- dataSource -->

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

         <property name="driverClass" value="com.mysql.jdbc.Driver"></property>

         <property name="jdbcUrl" value="jdbc:mysql:///web_ssh"></property>

         <property name="user" value="root"></property>

         <property name="password" value="123456"></property>

    </bean>

   

    <!-- sessionFactory -->

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

         <!--configLocation:hibernate配置文件的位置  -->

         <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> -->

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

         <property name="hibernateProperties">

             <props>

                  <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>

                  <prop key="hibernate.show_sql">true</prop>

                  <prop key="hibernate.format_sql">true</prop>

                  <prop key="hibernate.hbm2ddl.auto">update</prop>

                  <prop key="hibernate.current_session_context_class">thread</prop>

             </props>

         </property>

         <!-- 映射文件位置 -->

         <property name="mappingLocations" value="classpath:com/gyf/borrowsys/domain/*.hbm.xml"></property>

    </bean>

 

第三步:spring整合struts

编写action类,并将其配置给spring ,spring可以注入service

编写struts.xml

表单jsp页面

web.xml 配置

1.确定配置文件contextConfigLocation

2.配置监听器 ContextLoaderListener

3.配置前端控制器 StrutsPrepareAndExecuteFitler

 

 <!-- 加载spring的配置文件,初始化相关的bean -->

  <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath:applicationContext.xml</param-value>

  </context-param>

  <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

 

  <!-- struts的过滤器 -->

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

 

action和spring配置文件

actionservice默认会根据名称注入

默认情况下框架使用的自动装配策略是name,也就是说框架会去 Spring中寻找与action属性名字相同的bean

SSH-----------整合struts\spring\hibernate

 

actoin对象由spring创建

SSH-----------整合struts\spring\hibernate