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

       对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 :针对每一个实体持久化所做的配置,数据库连接usernamepassword等等。

                          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.xsd
				http://www.springframework.org/schema/context 
				http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://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>  
		        <!-- 数据库的登陆username -->  
		        <property name="user" value="${user}"></property>  
		        <!-- 数据库的登录password -->  
		        <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框架整合好了,就通过一个实例检測下吧。见下篇博客。





原文地址:https://www.cnblogs.com/yutingliuyl/p/6820979.html