写此文的目的:以后在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