MyEclipse搭建SSH(Struts2+Spring2+Hibernate3)框架项目教程

来源:互联网 发布:达内java培训具体安排 编辑:程序博客网 时间:2024/06/06 02:04

       对Struts、spring、hibernate大体上了解一遍后,就是针对这个几个框架的整合了。如何整合,请看下面:

第一:Struts2的jar和xml配置文件:

        jar包:

                 commons-fileupload-1.2.1.jar:文件上传

                 commons-io-1.3.2.jar:文件读取工具类

                 freemarker-2.3.15.jar:模板引擎,基于模板生成文本输出的通用工具。

                 ognl-2.7.3.jar:功能强大的表达式语言,替代EL表达式,进行数据绑定和显示

                 struts2-core-2.1.8.1.jar:struts2核心包

                 xwork-core-2.1.6.jar:xwork核心包,是struts2的底层核心


        xml文件有:web.xml  (配置struts2的核心过滤器) 

                            struts.xml (配置资源访问)


第二:Spring的jar和xml配置文件

        jar包:

                spring.jar:包含有完整发布模块的单个jar 包。但是不包括mock.jar, aspects.jar, spring-portlet.jar, and spring-hibernate2.jar

                commons-logging:针对日志处理的

                aspectjrt:支持aop的jar

                cglib-nodep-2.1_3:配合支持aop的jar

                aspectjweaver.jsr 和 aspectjrt.jar:springAOP需要的包


       xml文件有:applicationContext.xml


第三:Hibernate的jar和xml配置文件

       jar包:

               Hibernate3.jar:Hibernate的核心库

               antlr-2.7.6.jar:执行HQL语句的支持包

               cglib-asm.jar:CGLIB库,hibernate用它来实现PO字节码的动态生成

               dom4j.jar: dom4j:Java的XML API

               commons-collections.jar: Apache Commons包中的一个,包含了一些Apache开发的集合类,功能比java.util.*强大

              commons-logging.jar: Apache Commons包中的一个,包含了日志功能

              c3p0.jar: C3PO是一个数据库连接池,Hibernate可以配置为使用C3PO连接池。

              jta.jar: JTA规范,当Hibernate使用JTA的时候需要

              mysql-connector-java-5.1.5-bin.jar:链接mySql必须得包


       xml文件有:hibernate.cfg.xml :针对每个实体持久化所做的配置,数据库连接用户名密码等等。

                          xx.hbm.xml:每个实体对应一个


第四:Spring和Struts2、Spring和Hibernate整合时的XML配置和相关jar包

jar包:
         Struts2和Spring整合时的jar:struts2-spring-plugin-2.1.8.1.jar是strus2和spring的一个整合插件。

         Spring和Hibernate整合时不需要额外的jar包


xml配置:

     1)Web.xml配置

<?xml version="1.0" encoding="UTF-8"?>  <web-app version="2.5"       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_2_5.xsd">      <!--     配置spring的用于初始化容器对象的监听器 -->    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>     /WEB-INF/classes/applicationContext*.xml    </param-value></context-param><!-- ~~~~~~~~~~~struts2的配置  start~~~~~~~~~~~ -->  <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><!-- ~~~~~~~~~~~struts2的配置  end~~~~~~~~~~~ -->     <welcome-file-list>      <welcome-file>index.jsp</welcome-file>    </welcome-file-list>      </web-app>  


       2):Struts的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>  <!-- 设置为开发模式 --><constant name="struts.devMode" value="true" /><!-- 扩展名配置为action --><constant name="struts.action.extension" value="action"/>   <!-- 主题,将值设置为simple,即不使用UI模板。这将不会生成额外的html标签 --><constant name="struts.ui.theme" value="simple" /> </struts>   

        3)Spring的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-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">    <!--导入外部properties文件 -->     <context:property-placeholder location="classpath:jdbc.properties"/>             <!--  配置SessionFactory --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><!-- 指定hibernate配置文件的位置 --><property name="configLocation" value="classpath:hibernate.cfg.xml"></property><!-- 连接池 --><property name="dataSource" ><bean class="com.mchange.v2.c3p0.ComboPooledDataSource">  <!--mysql数据库驱动 -->          <property name="driverClass" value="${driverClass}"></property>          <!-- mysql数据库名称 -->          <property name="jdbcUrl" value="${jdbcUrl}"></property>          <!-- 数据库的登陆用户名 -->          <property name="user" value="${user}"></property>          <!-- 数据库的登陆密码 -->          <property name="password" value="${password}"></property>          <!-- 方言:为每一种数据库提供适配器,方便转换 -->  <!-- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>   -->        <!-- 其他配置 -->        <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 --><property name="initialPoolSize" value="3"></property><!--连接池中保留的最小连接数。Default: 3 --><property name="minPoolSize" value="3"></property><!--连接池中保留的最大连接数。Default: 15 --><property name="maxPoolSize" value="5"></property><!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --><property name="acquireIncrement" value="3"></property><!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 --><property name="maxStatements" value="8"></property><!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 --><property name="maxStatementsPerConnection" value="5"></property><!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --><property name="maxIdleTime" value="1800"></property></bean></property></bean> </beans>  


     4)Hibernate的hibernate.cfg.xml配置和xx.hbm.xml配置

hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">    <hibernate-configuration>  <session-factory >          <!-- 方言:为每一种数据库提供适配器,方便转换 -->          <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>                  <property name="show_sql">true</property>        <property name="hbm2ddl.auto">update</property>             </session-factory>  </hibernate-configuration>  
xx.hbm.xml配置,就不用说了。


总结:

       就这样,SSH框架整合完成了,通过这些配置理解这个框架的设计和思想,这样才是最好的。SSH框架整合好了,就通过一个实例检测下吧。见下篇博客。




1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 报销的车票丢了怎么办 快递写错一个字怎么办 外国人在中国护照过期怎么办 大学选课选漏了怎么办 高德地图不能琦跨城导航怎么办 水痘预防针间隔时间太久怎么办 车载导航被删了怎么办 高德地图gps信号弱怎么办 ai里面图片太多文件太大怎么办 ai文件太大怎么办1个G 文件写错了字怎么办 戒指弄不下来了怎么办 高德地图反应慢怎么办 白色印花t恤染色怎么办 印花t恤图案掉了怎么办 衣服上印花掉了怎么办 ps cs 3图标太小怎么办 ai cs6图标太小怎么办 su界面太小怎么办win10 华为p9手机gps信号弱怎么办 小米手机导航gps信号弱怎么办 安卓手机gps信号弱怎么办 苹果6导航gps信号弱怎么办 苹果6plus反应慢怎么办 手机文件打开是乱码怎么办 手机wps文件打开是乱码怎么办 腾讯视频vip账号被盗怎么办 附单据数错了 怎么办 橡胶的回弹性差怎么办 自己喷漆喷坏了怎么办 透明塑料磨花了怎么办 包包金属刮花了怎么办 鞋子刮了黑印子怎么办 黑色鞋跟磨白了怎么办 脚穿鞋子磨起泡怎么办 脚被鞋子磨红了怎么办 脚被鞋子磨黑了怎么办 白鞋皮鞋磨了皮怎么办 小脚趾磨肿了怎么办 穿鞋小拇指磨脚怎么办 高铁东西忘了怎么办