數(shù)據(jù)庫(kù)信息配置
*在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會(huì)通過連接池獲得連接 -->
<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會(huì)自動(dòng)讀取包中的所有配置 -->
<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對(duì)象的BeanName
完全由spring管理action生命周期,包括Action的創(chuàng)建
注意:需要手動(dò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中注冊(cè)相關(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)容合作請(qǐng)聯(lián)系作者