【Spring】SSH三大框架整合

數(shù)據(jù)庫信息配置

*在src目錄下新建db.properties文件

jdbc.jdbcUrl=jdbc:mysql:///mycrm
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123

Spring集成Hibernate和C3P0

*在src目錄下新建applicationContext.xml文件

<!-- 讀取db.properties文件 -->
    <context:property-placeholder location="classpath:db.properties" />
    <!-- 配置c3p0連接池 -->
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
<!-- 核心事務(wù)管理器 -->
    <bean name="transactionManager"
        class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- 開啟注解事務(wù) -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- 將SessionFactory配置到spring容器中 -->
    <bean name="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- 將連接池注入到sessionFactory, hibernate會通過連接池獲得連接 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 配置hibernate基本信息 -->
        <property name="hibernateProperties">
            <props>
                <!-- 必選配置 -->
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
                </prop>

                <!-- 可選配置 -->
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <!-- 引入orm元數(shù)據(jù),指定orm元數(shù)據(jù)所在的包路徑,spring會自動讀取包中的所有配置 -->
        <property name="mappingDirectoryLocations" value="classpath:cn/itcast/domain"></property>
    </bean>

Spring與Struts2集成

*在src目錄下新建struts.xml文件

<struts>
    <!-- #  struts.objectFactory = spring   將action的創(chuàng)建交給spring容器    
            struts.objectFactory.spring.autoWire = name spring負(fù)責(zé)裝配Action依賴屬性
            -->
    <constant name="struts.objectFactory" value="spring"></constant>

    <package name="crm" namespace="/" extends="struts-default" >
        <global-exception-mappings>
            <exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
        </global-exception-mappings>
         
         <!-- 
            整合方案2:class屬性上填寫spring中action對象的BeanName
                完全由spring管理action生命周期,包括Action的創(chuàng)建
                注意:需要手動組裝依賴屬性
          -->
        <action name="UserAction_*" class="userAction" method="{1}" >
            <result name="toHome" type="redirect" >/index.html</result>
            <result name="error" >/login.jsp</result>
        </action>
    </package>
</struts>

Spring中注冊相關(guān)bean

<!-- action -->
    <bean name="userAction" class="cn.itcast.web.action.UserAction"
        scope="prototype">
        <property name="userService" ref="userService"></property>
    </bean>

    <!-- service -->
    <bean name="userService" class="cn.itcast.service.impl.UserServiceImpl">
        <property name="ud" ref="userDao"></property>
    </bean>

    <!-- dao -->
    <bean name="userDao" class="cn.itcast.dao.impl.UserDaoImpl">
        <!-- 注入sessionFactory -->
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容