寫此文的目的:以後在hibernate和spring 整合的時候就不用再走彎路了;
我這裏沒用到Struts所以spring的加載就由TOMCAT來完成;
web.xml配置如下:
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <!-- 多個配置文件用逗號隔開 -->
- <param-value>/WEB-INF/springDB.xml,/WEB-INF/FacadeContext.xml</param-value>
- </context-param>
在程序中獲取該spring容器的方式:
- //獲取ServletContext 對象引用,這裏的this對象是一個servlet實例。
- ServletContext sc = this.getServletContext();
- ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
springDB.xml配置文件文件,難點在於事務代理機制的理解,如文件中配置的beanNames屬性爲 *Dao,意思是在所有以Dao結尾的bean中的find*,delete*,。。。。自動進行代理,對事物進行管理,當你從容器中將該bean取出來時,要用該Dao實現的接口來進行類型轉換(jdk動態代理的理解),
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans default-autowire="no" default-dependency-check="none" default-lazy-init="false">
- <!-- ========================= Start of PERSISTENCE DEFINITIONS ========================= -->
- <!-- Hibernate SessionFactory Definition-->
- <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <!-- localhost 默認的配置-->
- <property name="location">
- <value>/hibernates.properties</value>
- </property>
- </bean>
- <!-- 需要引入proxool-0.9.0RC3.jar ehcache-1.2.jar(與二級緩存相關)-->
- <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
- <property name="driver">
- <value>${jdbc.driver}</value>
- </property>
- <property name="driverUrl">
- <value>${jdbc.url}</value>
- </property>
- <property name="user">
- <value>${jdbc.username}</value>
- </property>
- <property name="password">
- <value>${jdbc.password}</value>
- </property>
- <property name="alias">
- <value>${jdbc.alias}</value>
- </property>
- <property name="houseKeepingSleepTime">
- <value>${jdbc.houseKeepingSleepTime}</value>
- </property>
- <property name="prototypeCount">
- <value>${jdbc.prototypeCount}</value>
- </property>
- <property name="maximumConnectionCount">
- <value>${jdbc.maximumConnectionCount}</value>
- </property>
- <property name="minimumConnectionCount">
- <value>${jdbc.minimumConnectionCount}</value>
- </property>
- <property name="maximumConnectionLifetime">
- <value>${proxool.maximumConnectionLifetime}</value>
- </property>
- <property name="maximumActiveTime">
- <value>${proxool.maximumActiveTime}</value>
- </property>
- <property name="trace">
- <value>${jdbc.trace}</value>
- </property>
- <property name="verbose">
- <value>${jdbc.verbose}</value>
- </property>
- <property name="houseKeepingTestSql">
- <value>${proxool.houseKeepingTestSql}</value>
- </property>
- <property name="statistics">
- <value>${proxool.statistics}</value>
- </property>
- <property name="statisticsLogLevel">
- <value>${proxool.statisticsLogLevel}</value>
- </property>
- </bean>
- <!-- 第二中配置方式,單獨的類似dbcp的使用 ,配置結束-->
- <!-- Hibernate SessionFactory Definition:定義Hibernate的SessionFactory -->
- <!-- use spring sessionFactory of configurtion -->
- <!-- 定義了Hibernate的會話工廠,會話工廠類用Spring提供的LocalSessionFactoryBean維護,它注入了數據源dataSource和資源映射文件com/cfc/bo,此外還通過一些鍵值對設置了Hibernate所需的屬性 -->
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref local="dataSource" />
- </property>
- <property name="mappingDirectoryLocations">
- <!-- 在此可以匹配*.hbm.xml映射文件 -->
- <list>
- <value>classpath:/com/cfc/web/pojo</value>
- </list>
- </property>
- <!-- 定義Hibernate的SessionFactory屬性 -->
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- ${hibernate.dialect}
- </prop>
- <prop key="hibernate.show_sql">
- ${hibernate.show_sql}
- </prop>
- <prop key="hibernate.format_sql">
- ${hibernate.format_sql}
- </prop>
- <prop key="hibernate.jdbc.fetch_size">
- ${hibernate.jdbc.fetch_size}
- </prop>
- <prop key="hibernate.jdbc.batch_size">
- ${hibernate.jdbc.batch_size}
- </prop>
- <!-- second cache -->
- <!-- 使用ehcache,適合項目用
- <prop key="hibernate.cache.use_query_cache">
- ${hibernate.cache.use_query_cache}
- </prop>
- <prop key="hibernate.cache.use_second_level_cache">
- ${hibernate.cache.use_second_level_cache}
- </prop>
- <prop key="hibernate.cache.provider_class">
- ${hibernate.cache.provider_class}
- </prop>
- -->
- <!--
- 此處要注意因爲proxool自己釋放數據庫連接比慢,所以要在此給出釋放連接的模式,具體幾種模式對應的意思,可以Google一下hibernate.connection.release_mode,有很多說明,在此不多說
- -->
- <prop key="hibernate.connectionReleaseMode">
- ${hibernate.connectionReleaseMode}
- </prop>
- <prop key="simultaneous-build-throttle">
- ${simultaneous-build-throttle}
- </prop>
- <prop key="jdbc.maximumnewConnections">
- ${jdbc.maximumnewConnections}
- </prop>
- <prop key="hibernate.connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</prop>
- <prop key="hibernate.proxool.existing_pool">false</prop>
- <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
- </props>
- </property>
- </bean>
- <!-- Hibernate Transaction Manager Definition :配置Hibernate的事務管理器-->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <!-- HibernateTransactionManager Bean,它需要依賴注入一個SessionFactory
- Bean的引用 -->
- <property name="sessionFactory">
- <ref bean="sessionFactory" />
- </property>
- </bean>
- <!-- jdbcTemplate -->
- <!-- 這個基礎類來實戰連接Mysql數據庫.JdbcTemplate的使用需要有一個DataSource的支持,所以在配置文件中,
- 我們首先要配置一個Spring的DriverManagerDataSource,然後將這個DataSource配置到JdbcTemplate裏.
- 接着將JdbcTemplate配置到DAO層.最後將DAO配置到Model層 -->
- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
- <property name="dataSource">
- <ref bean="dataSource" />
- </property>
- </bean>
- <!-- Hibernate Template Defintion -->
- <bean id="hibernateTemplate"
- class="org.springframework.orm.hibernate3.HibernateTemplate">
- <property name="sessionFactory">
- <ref bean="sessionFactory" />
- </property>
- <property name="jdbcExceptionTranslator">
- <ref bean="jdbcExceptionTranslator" />
- </property>
- </bean>
- <!-- Spring Data Access Exception Translator Defintion -->
- <bean id="jdbcExceptionTranslator"
- class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
- <property name="dataSource">
- <ref bean="dataSource" />
- </property>
- </bean>
- <!-- define transaction interceptor -->
- <bean id="txInterceptor"
- class="org.springframework.transaction.interceptor.TransactionInterceptor">
- <property name="transactionManager">
- <ref bean="transactionManager" />
- </property>
- <property name="transactionAttributeSource">
- <ref bean="txAttributes" />
- </property>
- </bean>
- <bean id="autoProxyCreator"
- class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
- <property name="interceptorNames">
- <value>txInterceptor</value>
- </property>
- <!--這裏通過配置文件,引用bean有數據庫的操作。也可以直接在這個文件中配置。本項目中是分開配置,即FacadeContext.xml文件 -->
- <property name="beanNames">
- <value>*Dao,*Service</value>
- </property>
- </bean>
- <bean id="txAttributes"
- class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
- <property name="properties">
- <value>
- find*=PROPAGATION_REQUIRED,readOnly
- get*=PROPAGATION_REQUIRED,readOnly
- save*=PROPAGATION_REQUIRED
- add*=PROPAGATION_REQUIRED
- insert*=PROPAGATION_REQUIRED
- update*=PROPAGATION_REQUIRED
- delete*=PROPAGATION_REQUIRED
- create*=PROPAGATION_REQUIRED
- batch*=PROPAGATION_REQUIRED
- generate*=PROPAGATION_REQUIRED
- </value>
- </property>
- </bean>
- </beans>
hibernate.proterty文件代碼
- #database driver
- jdbc.driver=com.mysql.jdbc.Driver
- #url:這裏一定要這樣寫,否則報錯:Access denied for user 'root'@'localhost' (using password: NO)
- #jdbcjdbc.url=jdbc:mysql://192.168.1.54:3306/tdscdmanew?useUnicode=truecharacterEncoding=UTF-8user=rootpassword=root
- jdbcjdbc.url=jdbc:mysql://192.168.1.252:3306/tscdmaweb?useUnicode=truecharacterEncoding=UTF-8user=rootpassword=root
- #jdbcjdbc.url=jdbc:mysql://192.168.1.252:3306/tdscdmanew?useUnicode=truecharacterEncoding=UTF-8user=rootpassword=root
- #login database info
- #如果用proxool配置,這兩個屬性不用,但是必須這樣寫,這是個BUG
- jdbc.username=root
- jdbc.password=root
- #hibernate properties
- hibernate.dialect=org.hibernate.dialect.MySQLDialect
- #Batch Size is set to the database to delete bulk, bulk and bulk insert update when the batch size
- hibernate.jdbc.batch_size=25
- #Fetch Size is set to read Statement of JDBC data from the database each time out of the number of records
- hibernate.jdbc.fetch_size=50
- hibernate.show_sql=true
- hibernate.format_sql=true
- #second level cache open configuration
- #hibernate.cache.use_query_cache=true
- #hibernate.cache.use_second_level_cache=true
- #hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
- #pool configuration
- #pool alias
- jdbc.alias=dbcptmclnew
- #proxool自動偵察各個連接狀態的時間間隔(毫秒),偵察到空閒的連接就馬上回收,超時的銷燬
- jdbc.houseKeepingSleepTime=90000
- # 指因未有空閒連接可以分配而在隊列中等候的最大請求數,超過這個請求數的用戶連接就不會被接受 proxool.maximumnewconnections=20
- jdbc.maximumnewConnections=5
- #最少保持的空閒連接數(默認2個).一次產生連接的數量。 但不能超過最大連接數
- jdbc.prototypeCount=3
- #允許最大連接數,超過了這個連接,再有請求時,就排在隊列中等候,最大的等待請求數由maximum-new-connections決定 (默認5個)
- #如果超過最大連接數量則會拋出異常。連接數設置過多,服務器CPU和內存性能消耗很大。
- jdbc.maximumConnectionCount=100
- #最小連接數 (默認2個).建議設置0以上,保證第一次連接時間
- jdbc.minimumConnectionCount=5
- #最大活動時間(超過此時間線程將被kill,默認爲5分鐘,10)
- proxool.maximumActiveTime = 60000000
- #連接最長時間(默認爲8小時:28000s),一個線程的最大壽命
- proxool.maximumConnectionLifetime=18000000
- #
- #hibernate.bytecode.use_reflection_optimizer=true
- #允許被緩存的JDBC連接開啓
- #因爲proxool自己釋放數據庫連接比慢,所以要在此給出釋放連接的模式.AFTER_STATEMENT (也被稱做積極釋放) - 在每一條語句被執行後就釋放連接。但假若語句留下了與session相關的資源,那就不會被釋放
- #hibernate.connectionReleaseMode=after_statement
- hibernate.connectionReleaseMode=auto
- #
- simultaneous-build-throttle=10
- #trace爲true,記錄數據庫每一步操作
- jdbc.trace=true
- #verbose:If this is true then we start logging a lot of stuff everytime we serve a connection and everytime the house keeper and prototyper run. Be prepared for a lot of debug!
- #記錄執行的詳細信息
- jdbc.verbose=true
- #
- proxool.houseKeepingTestSql=select CURRENT_DATE
- #統計的時隔可以分為s(econds),、m(inutes)、h(ours)與d(ays),就下面的設定來說,就是15秒、10分鐘、1天分別作統計
- proxool.statistics=15s,10m,1d
- #INFO,ERROR
- proxool.statisticsLogLevel=INFO