1. 程式人生 > >Spring整合Struts2和Hibernate+Maven(二)之SSH的配置檔案

Spring整合Struts2和Hibernate+Maven(二)之SSH的配置檔案

上次講到的是maven專案的建立以及pom.xml的配置。
這裡推薦一個網站:maven整合jar包,這裡可以查詢並生成配置檔案中jar包匯入格式的文字,複製貼上到pom.xml中即可由idea自動下載並匯入專案。

resources資料夾

建立ssh的配置檔案至上章圖片中的resources資料夾中,分為三個檔案
hibernate.cfg.xml、spring-hibernate.xml、struts.xml。

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8" ?>
<!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> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:111/111?useUnicode=true&amp;characterEncoding=utf-8</property> <property
name="connection.username">
111</property> <property name="connection.password">111</property> <!--最大最小連線池--> <property name="hibernate.c3p0.max_size">20</property> <property name="hibernate.c3p0.min_size">1</property> <property
name="hibernate.c3p0.timeout">
5000</property> <property name="hbm2ddl.auto">update</property> </session-factory> </hibernate-configuration>

配置檔案連線時加characterEncoding=utf-8是解決編碼問題,同時

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

這個操作啟動的時候會去檢查schema是否一致,如果不一致會做scheme更新。

加入該配置檔案後,File->Project Structure ->Modules的專案名稱下加入Spring 、 Hibernate和在Web下加入Struts2,結構如圖所示: Modules結構
在配置好上面所說的3個配置檔案後,idea會提示匯入該配置,點選即可。

Spring整合Hibernate之spring-hibernate.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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       ">
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="configLocation"
                  value="hibernate.cfg.xml">
        </property>
        <property name="hibernateProperties" >
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
                hibernate.hbm2ddl.auto=update
            </value>
        </property>
    </bean>
</beans>

sessionFactory:Spring配置檔案的bean的名稱。
org.springframework.orm.hibernate4.LocalSessionFactoryBean:class檔案,之前已在pom.xml匯入該class檔案所在的jar包。
配置好後在以後需要使用Hibernate時只需在該工廠模式中‘拿’即可。

struts.xml

首先看配置檔案

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
        "http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>
    <constant name="struts.devMode" value="false"/>
    <include file="struts-default.xml"/>
    <package name="applyRoom" extends="struts-default" >
        <action name="Login_*" class="" method="{1}">
            <result name="success">index.jsp</result>
            <result name="error">index.jsp</result>
        </action>
    </package>
</struts>

該配置檔案並無其他,唯一區別即使用請求萬用字元。關於Spring整合Struts2之後會詳細介紹。