ssh中整合spring和hibernate之後的application.xml檔案的超詳細配置
<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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 引入外部jdbc.properties檔案 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置連線池的資訊 c3p0連線池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 資料庫連線的四個基本引數 -->
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 其他配置 -->
<!--初始化時獲取三個連線,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->
<property name="initialPoolSize" value="10"></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>
<!-- 如果使用的是繼承的 HbiernateDaoSupport( XXXDaoImpl extends HbiernateDaoSupport implements XXXDao)的方式來呼叫sesseionfactory,還需要配置HibernateTemplate模板, (注意:這裡注入的是sessionFactory)
<code class="hljs applescript has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><bean <span class="hljs-property" style="box-sizing: border-box;">id</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hibernateTemplate"</span> <span class="hljs-type" style="box-sizing: border-box;">class</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"org.springframework.orm.hibernate3.HibernateTemplate"</span>> <<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">property</span> <span class="hljs-property" style="box-sizing: border-box;">name</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sessionFactory"</span>> <<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ref</span> bean=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sessionFactory"</span> /> </<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">property</span>> </bean><span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);"> </span></code>
java程式碼中用法
1、j繼承父類 HbiernateDaoSupport( XXXDaoImpl extends HbiernateDaoSupport implements XXXDao)
2、方法中用this.getHibernateTemplate()獲得HibernateTemplate(自動開啟了session,不用再用sessionfactory建立session)。
3、另還可以用注入的方式用dao,在applicationContext中注入
<code class="hljs applescript has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><bean <span class="hljs-property" style="box-sizing: border-box;">id</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"XXXDao"</span> <span class="hljs-type" style="box-sizing: border-box;">class</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"spring.dao.impl.XXXDaoImpl"</span>> <<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">property</span> <span class="hljs-property" style="box-sizing: border-box;">name</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"XXXTemplate"</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ref</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"XXXTemplate"</span>></<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">property</span>> </bean></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
private HibernateTemplate XXXTemplate;
get。set方法
XXXTemplate.save(obj)
-->
<!-- 配置Hibernate的相關屬性 --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入連線池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置Hibernate的其他的屬性 -->
<property name="hibernateProperties">
<props>
<!-- Hibernate的方言 -->
<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>
<prop key="hibernate.connection.autocommit">false</prop>
</props>
</property>
<!-- 配置對映檔案 -->
<property name="mappingResources">
<list>
<value>cn/itcast/shop/user/User.hbm.xml</value>
<value>cn/itcast/shop/category/Category.hbm.xml</value>
<value>cn/itcast/shop/product/Product.hbm.xml</value>
<value>cn/itcast/shop/categorysecond/CategorySecond.hbm.xml</value>
<value>cn/itcast/shop/order/Order.hbm.xml</value>
<value>cn/itcast/shop/order/OrderItem.hbm.xml</value>
<value>cn/itcast/shop/adminuser/AdminUser.hbm.xml</value>
</list>
</property>
</bean>
<!-- 宣告式事務管理 -->
<!-- l利用註解的方式配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 注入 sessionFactory-->
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--
//非註解的方式建立事務管理器
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
配置事務傳播特性
<tx:advice id="TestAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="REQUIRED"/>
<tx:method name="get*" propagation="REQUIRED"/>
<tx:method name="t*" propagation="REQUIRED"/>
<tx:method name="in*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
配置參與事務的類
<aop:config>
<aop:pointcut id="allTestServiceMethod" expression="execution(* com.itcast.oa.service.*.*(..))"/>
<aop:advisor pointcut-ref="allTestServiceMethod" advice-ref="TestAdvice" />
</aop:config>
-->
<!--
<!-- 自動掃描與裝配bean,通過 註解的方式依賴注入-->
<context:component-scan base-package="com.itcast.oa"></context:component-scan>
-->
<!-- 配置Action================================== -->
<bean id="indexAction" class="cn.itcast.shop.index.IndexAction" scope="prototype">
<property name="categoryService" ref="categoryService"/>
<property name="productService" ref="productService"/>
</bean>
<bean id="userAction" class="cn.itcast.shop.user.UserAction" scope="prototype">
<property name="userService" ref="userService"/>
</bean>
<bean id="checkImg" class="cn.itcast.shop.user.CheckImgAction" scope="prototype"></bean>
<bean id="productAction" class="cn.itcast.shop.product.ProductAction" scope="prototype">
<property name="categoryService" ref="categoryService"/>
<property name="productService" ref="productService"/>
<property name="categorySecondService" ref="categorySecondService"/>
</bean>
<bean id="cartAction" class="cn.itcast.shop.cart.CartAction" scope="prototype">
<!-- 注入商品的Service -->
<property name="productService" ref="productService"/>
</bean>
<bean id="orderAction" class="cn.itcast.shop.order.OrderAction" scope="prototype">
<property name="orderService" ref="orderService"/>
</bean>
<bean id="adminUserAction" class="cn.itcast.shop.adminuser.AdminUserAction" scope="prototype">
<property name="adminUserService" ref="adminUserService"/>
</bean>
<bean id="categoryAction" class="cn.itcast.shop.category.CategoryAction" scope="prototype">
<property name="categoryService" ref="categoryService"/>
</bean>
<bean id="categorySecondAction" class="cn.itcast.shop.categorysecond.CategorySecondAction" scope="prototype">
<property name="categorySecondService" ref="categorySecondService"/>
<property name="categoryService" ref="categoryService"></property>
</bean>
<!-- 配置Service================================== -->
<bean id="userService" class="cn.itcast.shop.user.UserService">
<property name="userDao" ref="userDao"/>
</bean>
<bean id="categoryService" class="cn.itcast.shop.category.CategoryService">
<property name="categoryDao" ref="categoryDao"/>
</bean>
<bean id="productService" class="cn.itcast.shop.product.ProductService">
<property name="productDao" ref="productDao"/>
</bean>
<bean id="orderService" class="cn.itcast.shop.order.OrderService">
<property name="orderDao" ref="orderDao"/>
</bean>
<bean id="adminUserService" class="cn.itcast.shop.adminuser.AdminUserService">
<property name="adminUserDao" ref="adminUserDao"/>
</bean>
<bean id="categorySecondService" class="cn.itcast.shop.categorysecond.CategorySecondService">
<property name="categorySecondDao" ref="categorySecondDao"/>
</bean>
<!-- 配置Dao================================== -->
<bean id="userDao" class="cn.itcast.shop.user.UserDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="categoryDao" class="cn.itcast.shop.category.CategoryDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="productDao" class="cn.itcast.shop.product.ProductDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="orderDao" class="cn.itcast.shop.order.OrderDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="adminUserDao" class="cn.itcast.shop.adminuser.AdminUserDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="categorySecondDao" class="cn.itcast.shop.categorysecond.CategorySecondDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
配置好application.xml之後還要在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>classpath:applicationContext*.xml</param-value>
</context-param>
<!--
用於做初始化工作的監聽器,一定要配置到Spring的ContextLoaderListener之後,因為要用到Spring的容器物件
-->
<listener>
<listener-class>com.itcast.oa.util.IniteLisener</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>
<!-- 配置Spring的用於解決懶載入問題的過濾器 -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 解決的是延遲載入的問題
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class> org.springframework.orm.hibernate4.support.OpenSessionInViewFilter </filter-class>
指定在spring配置的sessionFactory
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
singleSession預設為true,若設為false則等於沒用OpenSessionInView
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter> -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
相關文章
- spring application.xml中載入配置檔案SpringAPPXML
- 【SSH框架】系列之 Spring 整合 Hibernate 框架框架Spring
- SSH配置檔案詳解
- Spring、Spring MVC、MyBatis 整合檔案配置詳解SpringMVCMyBatis
- Spring、Spring MVC、MyBatis整合檔案配置詳解SpringMVCMyBatis
- Hibernate配置檔案詳解
- Hibernate配置檔案中對映元素詳解
- FLEX和spring、hibernate的整合FlexSpring
- 基於Annotation註解整合SSH框架和基於XML檔案配置Bean整合SSH框架框架XMLBean
- resin配置檔案的詳細解釋
- SSM:spring+springmvc+mybatis框架中的XML配置檔案功能詳細解釋SSMSpringMVCMyBatis框架XML
- Hibernate的配置檔案解析
- Spring - 配置檔案中的classpath: 和 classpath*: 的區別Spring
- 菜鳥學SSH(十一)——Hibernate之SchemaExport+配置檔案生成表結構Export
- 學習Hibernate原始碼三_Hibernate中的配置檔案解析原始碼
- Spring 配置檔案詳解Spring
- spring配置檔案詳解Spring
- Spring - 配置檔案中的特殊字元Spring字元
- spring和Mybatis的核心配置檔案SpringMyBatis
- Spring與Hibernate整合中的session問題SpringSession
- Hibernate中flush機制的詳細理解
- mysql的配置檔案(調優之後)MySql
- Nginx配置檔案詳細說明Nginx
- Struts配置檔案詳細講解
- Springboot專案中需整合的依賴集和檔案配置Spring Boot
- SSH(STRUTS2+SPRING+HIBERNATE)實現檔案上傳功能Spring
- Linux系統中的/etc/shadow檔案超詳細內容解析Linux
- Github配置ssh key詳細步驟Github
- Spring MVC學習筆記和SSH的整合SpringMVC筆記
- Spring配置bean的詳細知識SpringBean
- MyBatis Generator 超詳細配置MyBatis
- Eclipse下整合spring mvc 配置檔案EclipseSpringMVC
- MyBatis 核心配置檔案詳細內容詳解MyBatis
- 分享Linux下的sudo及其配置檔案/etc/sudoers詳細配置Linux
- SSH開發實踐part1:Spring與Hibernate整合Spring
- 超詳細講解如何搭建自己的檔案伺服器伺服器
- 使用maven搭建hibernate的pom檔案配置Maven
- Spring 整合 HibernateSpring