更新時間:2022-10-21 10:25:41 來源:動力節點 瀏覽1229次
相信大家對SSH框架原理已經有所了解,SSH框架配置時這幾個文件比較重要:Spring,Struts2,hibernate,web.xml。
開始配置前只要把SSH需要的所有jar復制到WebRoot下的WEB-INF中的lib目錄下。這里已經整合好的所有jar包,下載地址:點擊打開鏈接用這種方法的優點是:既可以在myeclipse用也可以在eclipse中使用,不會出現jar包沖突的事情。
hibernate.cfg.xml文件配置
<span style="font-size:14px;"><?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 配置Hibernate的基本屬性 -->
<!-- 1. 數據源配置到 IOC 容器中, 在這里不需再配置 -->
<!-- 2. 關聯的 .hbm.xml 也在 IOC 容器 配置 SessionFactory 實例時 進行配置 -->
<!-- 3. 配置 Hibernate 的基本屬性: 方言, SQL 顯示及格式化, 生成數據表的策略以及 二級緩存 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">false</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.temp.use_jdbc_metadata_defaults">fals </property>
<!-- 配置 Hibernate 二級緩存相關屬性 -->
</session-factory>
</hibernate-configuration>
</span>
db.properties文件配置
<span style="font-size:14px;">jdbc.user=root
jdbc.password=220316
jdbc.jdbcUrl=jdbc:mysql://127.0.0.1:3306
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.initPoolSize=5
jdbc.maxPoolSize=10</span>
這里和傳統的SSH配置不一樣,目前主流的是把hibernate中關于數據庫的相關配置信息給分離到db.properties文件配置中去。
這樣做的好處是后期維護比較方法,代碼比較簡潔不容易出錯。
applicationContext.xml文件配置
<span style="font-size:14px;"><?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.ge"></context:component-scan>
<!-- 導入外部資源文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- c3p0方式配置數據源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
<!-- 配置Hibernate的SessionFactory實例: 通過 Spring 提供的 LocalSessionFactoryBean
進行配置 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 配置數據源屬性 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置 Hibernate 配置文件位置 和 名稱 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- 配置 Hibernate 映射文件的位置和名稱, 可以使用通配符 -->
<property name="mappingLocations" value="classpath:com/ge/entity/*.hbm.xml"></property>
</bean>
<!-- 1. 配置Hibernate事務管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 2. 配置事務屬性, 需要事務管理器 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 3. 配置事務切點 -->
<aop:config>
<aop:pointcut expression="execution( * com.ge.biz.imp.*.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
</beans>
</span>
這里需要改的是:
1.映射文件的位置和名字,因為我測試用的是,這里需要修改的是:com/ge/entity也就是你映射文件所在的包位置。
2.還有就是配置事務切點的位置,我這里的位置是:,這里需要修改的是事務切點位置:com.ge.biz.imp也就是你biz層所在位置。
applicationContext-beans.xml文件配置
<span style="font-size:14px;"><?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean class="com.ge.dao.imp.EquipmentDaoImp" id="EquipmentDaoImp">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean class="com.ge.biz.imp.EquipmentBizImp" id="EquipmentBizImp">
<property name="equipmentDao" ref="EquipmentDaoImp"></property>
</bean>
<bean class="com.ge.action.TestAction" id="testAction" scope="prototype">
<property name="equipmentBiz" ref="EquipmentBizImp"></property>
</bean>
</beans>
</span>
applicationContext-beans.xml文件配置配置的重點是:
<bean class="com.ge.dao.imp.EquipmentDaoImp" id="EquipmentDaoImp">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
就是dao層中會話工廠的配置,這個如果不配置那hibernate的功能就都用不了了。
Struts2文件配置
<span style="font-size:14px;"><?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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.multipart.saveDir" value="C:/repository"/>
<constant name="struts.devMode" value="true" />
<package name="HY" namespace="/" extends="struts-default">
<action name="testAction" class="testAction">
<result name="index">index.jsp</result>
<result name="success">welcome.jsp</result>
<result name="fail">fail.jsp</result>
</action>
</package>
</struts>
</span>
Struts2中沒有什么難點,一般配置出錯都不在這里。
<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<!-- 配置 Spring 配置文件的名稱和位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<!-- 啟動 IOC 容器的 ServletContextListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app></span>
web.xml配置配置非常關鍵,如果配置出錯,tomcat啟動都啟動不了。如果大家對此比較感興趣,想了解更多相關知識,可以關注一下動力節點的SSH整合視頻教程,里面有更豐富的知識等著大家去學習,希望對大家能夠有所幫助。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習