ssh整合

1、新建工程,把工程编码改为utf-8

2.把JSP的编码格式改为utf-8

3.把所需jar包放入到lib下

4、建立三个src folder

Src:存放源代码

Config:存放配置文件

Test:存放测试文件

5、在src下建立package包

domain

dao

daoImpl

service

serviceImpl

view

util


在WebRoot文件夹下建立几个新的文件夹

script :存放JavaScript文件

style:存放style文件

-----img:存放图片资源

6.在WEB-INF文件夹下建立JSP文件夹,目录如下:

Jsp:存放jsp文件

7.在WebRoot—>WEB-INFàlib文件夹下放置所需的jar包




8、整合spring和hibernate

8.1 添加配置文件

在config目录中添加spring配置文件application.xml

hibernate配置文件hibernate.cfg.xml和jdbc.properties

在domain包中新建POJO类Users和其对应的映射文件Users.hbm.xml

8.2 具体文件代码

具体文件的代码如下:

Users类代码为:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.oa.domain;  
  2.   
  3. public class Users  
  4. {  
  5.     private long uid;  
  6.   
  7.     public long getUid()  
  8.     {  
  9.         return uid;  
  10.     }  
  11.   
  12.     public void setUid(long uid)  
  13.     {  
  14.         this.uid = uid;  
  15.     }  
  16.       
  17. }  

Users.hbm.xml文件代码为:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  4.   
  5. <hibernate-mapping>  
  6.  <!-- 用来描述一个持久化类   
  7.       name 类的全名  
  8.       table 表名,可以不写,默认值和类名一样  
  9.       catalog 数据库名称,一般不写  
  10.  -->  
  11.  <class name="com.oa.domain.Users" table="Users" lazy="true">  
  12.     <!-- 标示属性,和数据库主键对应 ,将Users类的pid映射为表中的主键pid  
  13.          column 列名称  
  14.          length 数据库的字段长度,默认数据库最高的长度  
  15.          type 类型  
  16.     -->  
  17.     <id name="uid" column="uid" length="5" type="java.lang.Long">  
  18.         <!-- 主键产生器  
  19.              告诉hibernate容器用什么样的方式产生主键  
  20.              用数据库本身的自动增长,不用hibernate的              
  21.          -->  
  22.         <generator class="native"></generator>  
  23.     </id>  
  24.    
  25.  </class>  
  26. </hibernate-mapping>  

Jdbc.properties中代码为:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. driverClass=com.mysql.jdbc.Driver  
  2. jdbcUrl=jdbc\:mysql\://localhost\:3306/oa  
  3. username=root  
  4. password=340621  

hibernate配置文件hibernate.cfg.xml代码为:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version='1.0' encoding='utf-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5. <hibernate-configuration>  
  6.     <!--  
  7.         一个session-factory只能连接一个数据库 
  8.     -->  
  9. <session-factory>  
  10.   
  11.     <!--  
  12.         设置数据库方言  
  13.     -->  
  14.     <property name="dialect">  
  15.         org.hibernate.dialect.MySQLDialect  
  16.     </property>  
  17.   
  18.     <!--   
  19.         作用:根据持久化类和映射文件生成表  
  20.         validate 只验证不生成  
  21.         create-drop 当hibernate启动时生成表,hibernate结束时删除表  
  22.         create 只要启动hibernate时生成表  
  23.         update 在启动hibernate容器时检查持久化类和映射文件是不是对应,不对应则创建  
  24.     -->  
  25.     <property name="hbm2ddl.auto">update</property>  
  26.     <!--  
  27.         显示hibernate内部生成的sql语句 
  28.     -->  
  29.     <property name="show_sql">true</property>  
  30.     <property name="connection.pool_size">1</property>  
  31.   
  32.     <!-- 声明映射文件 -->  
  33.     <mapping resource="com/oa/domain/Users.hbm.xml" />  
  34.     <mapping resource="com/oa/domain/Role.hbm.xml" />  
  35.   
  36. </session-factory>  
  37. </hibernate-configuration>  

Spring配置文件代码为:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"     
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"   
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:context="http://www.springframework.org/schema/context"       
  7.       
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  9.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  10.            http://www.springframework.org/schema/aop   
  11.            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  12.            http://www.springframework.org/schema/tx   
  13.            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  14.            http://www.springframework.org/schema/context  
  15.            http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  16.            ">  
  17.     
  18. <!-- 0,配置bean的自动扫描与装配 -->  
  19.     <context:component-scan base-package="com.oa"></context:component-scan>  
  20.   
  21.     <!-- 1,配置数据源 -->  
  22.     <!-- 1.1,导入 jdbc.properties 配置文件 -->  
  23.     <context:property-placeholder location="classpath:jdbc.properties" />  
  24.     <!-- 1.2,配置数据源(c3p0) -->  
  25.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  26.         <!-- 数据库连接信息 -->  
  27.         <property name="jdbcUrl" value="${jdbcUrl}"></property>  
  28.         <property name="user" value="${username}"></property>  
  29.         <property name="password" value="${password}"></property>  
  30.         <property name="driverClass" value="${driverClass}"></property>  
  31.   
  32.         <!-- 其他配置 -->  
  33.         <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->  
  34.         <property name="initialPoolSize" value="3"></property>  
  35.         <!--连接池中保留的最小连接数。Default: 3 -->  
  36.         <property name="minPoolSize" value="3"></property>  
  37.         <!--连接池中保留的最大连接数。Default: 15 -->  
  38.         <property name="maxPoolSize" value="5"></property>  
  39.         <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->  
  40.         <property name="acquireIncrement" value="3"></property>  
  41.         <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->  
  42.         <property name="maxStatements" value="8"></property>  
  43.         <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->  
  44.         <property name="maxStatementsPerConnection" value="5"></property>  
  45.         <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->  
  46.         <property name="maxIdleTime" value="1800"></property>  
  47.     </bean>  
  48.   
  49.   
  50.     <!-- 2,配置SessionFactory(整合Hibernate) -->  
  51.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  52.         <property name="dataSource" ref="dataSource"></property>  
  53.         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>  
  54.     </bean>  
  55.   
  56.   
  57.     <!-- 3,配置声明式事务 -->  
  58.     <!-- 3.1,配置事务管理器 -->  
  59.     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  60.         <property name="sessionFactory" ref="sessionFactory"></property>  
  61.     </bean>  
  62.     <!-- 3.2,配置基于注解的事务支持-->  
  63.     <tx:annotation-driven transaction-manager="transactionManager"/>  
  64. </beans>  

8.3编写测试文件:

一般测试spring和hibernate是否整合是通过sessionFactory来进行,具体方法为:

在test文件夹中新建SessionFactoryTest类,具体代码为:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.oa.test;  
  2.    
  3. import org.hibernate.SessionFactory;  
  4. import org.junit.Test;  
  5. import org.springframework.context.ApplicationContext;  
  6. importorg.springframework.context.support.ClassPathXmlApplicationContext;  
  7.    
  8. public class SessionFactoryTest  
  9. {  
  10.          privateApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  
  11.          @Test  
  12.          publicvoid testSession()  
  13.          {  
  14.                     
  15.                     
  16.                    SessionFactorysf = (SessionFactory) context.getBean("sessionFactory");  
  17.                     
  18.                    //System.out.println(sf.);  
  19.          }                  
  20. }  

执行测试文件,没有出错,说明spring和hibernate成功整合

9、整合spring和Struts2

9.1添加配置文件

在config目录中添加Struts2配置文件struts.xml

在view包中新建UserAction

9.2添加代码

UserAction类代码如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.oa.view;  
  2.   
  3. import org.springframework.context.annotation.Scope;  
  4. import org.springframework.stereotype.Controller;  
  5.   
  6. import com.opensymphony.xwork2.ActionSupport;  
  7.   
  8. //基于注解的配置  
  9. @Controller  
  10. @Scope("prototype")  
  11. // Scope为prototype,保证action的多实例  
  12. public class RoleAction extends ActionSupport  
  13. {  
  14.     public String list()  
  15.     {  
  16.           
  17.         return "list";  
  18.     }  
  19.       
  20.     public String delete()  
  21.     {  
  22.           
  23.         return "toList";  
  24.     }  
  25.       
  26.       
  27.     public String addUI()  
  28.     {  
  29.           
  30.         return "addUI";  
  31.     }  
  32.     public String add()  
  33.     {  
  34.           
  35.         return "toList";  
  36.     }  
  37.       
  38.     public String editUI()  
  39.     {  
  40.           
  41.         return "editUI";  
  42.     }  
  43.       
  44.     public String edit()  
  45.     {  
  46.           
  47.         return "toList";  
  48.     }  
  49. }  

Struts.xml配置代码如下:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
  5.   
  6. <struts>     
  7.     <constant name="struts.devMode" value="true" />  
  8.     <!-- 把action扩展名改为.do -->  
  9.     <constant name="struts.action.extension" value="do"/>  
  10.     <!-- 把主题设为simple -->  
  11.     <constant name="struts.ui.theme" value="simple" />  
  12.     <package name="default" namespace="/" extends="struts-default">  
  13.         <action name="test" class="com.oa.test.ActionTest" method="execute">  
  14.             <result name="success" type="dispatcher">/test.jsp</result>  
  15.         </action>     
  16.           
  17.         <!-- 配置roleAction -->  
  18.         <action name="role_*" class="roleAction" method="{1}">  
  19.             <result name="list">/WEB-INF/jsp/roleAction/list.jsp</result>  
  20.             <result name="addUI">/WEB-INF/jsp/roleAction/addUI.jsp</result>  
  21.             <result name="editUI">/WEB-INF/jsp/roleAction/editUI.jsp</result>  
  22.             <result name="toList" >/WEB-INF/jsp/roleAction/list.jsp</result>  
  23.         </action>     
  24.     </package>    
  25.   
  26.     <!-- Add packages here -->  
  27.   
  28. </struts>  

9.3 编写测试文件

 

10.配置web.xml

  加入spring的监听器

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!-- 配置Spring的用于初始化ApplicationContext对象的监听器 -->  
  2.     <listener>  
  3.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  4.     </listener>  
  5.     <context-param>  
  6.         <param-name>contextConfigLocation</param-name>  
  7.         <param-value>classpath:applicationContext.xml</param-value>  
  8.     </context-param>  

 加入struts2的过滤器

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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值