日期:2014-05-16  浏览次数:26027 次

(转)spring + hibernate+ C3P0+MYSQL配置

看下面的几个文件:
可能贴的顺序上有点问题:



(1) applicationContext.xml 

---------------------

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
 <!-- 引入参数配置文件 -->
 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
   <list>
    <value>jdbc.properties</value>
   </list>
  </property>
 </bean>
 
 <!-- 配置数据源  -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <property name="driverClass"><value>${jdbc.driverClass}</value></property>
  <property name="jdbcUrl"><value>${jdbc.url}</value></property>
  <property name="user"><value>${jdbc.user}</value></property>
  <property name="password"><value>${jdbc.password}</value></property>
  <property name="initialPoolSize"><value>${jdbc.initialPoolSize}</value></property>
  <property name="minPoolSize"><value>${jdbc.minPoolSize}</value></property>
  <property name="maxPoolSize"><value>${jdbc.maxPoolSize}</value></property>
 </bean>

 
 <!-- spring Hibernate SessionFactory -->
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref bean="dataSource" />
  </property>
  <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.jdbc.fetch_size">80</prop>
    <prop key="hibernate.jdbc.batch_size">35</prop>
   </props>
  </property>
  <property name="mappingResources"> 
   <list>        
    <value>com/spring/demo/bean/mapping/Book.hbm.xml</value>                                                
   </list> 
  </property> 
 </bean>
 
 
 
 <!-- 事务管理器 -->
 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref local="sessionFactory" />
  </property>
 </bean>
 
 
 <!-- 事务代理拦截器的配置 -->
 <bean id="baseTransactionProxy" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager">
   <ref bean="transactionManager" />
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="insert*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
   </props>
  </property>
 </bean>
 
 
 
 
 <bean id="hibernate_BookDao" class="com.spring.demo.dao.Hibernate_BookDao" >
  <property name="sessionFactory">
   <ref bean="sessionFactory"/>
  </property>
 </bean>
 
 
 <bean id="hibernate_BookManager" parent="baseTransactionProxy">
  <property name="target">
   <bean class="com.spring.demo.manager.Hibernate_BookManager">
    <property name="dao">
     <ref bean="hibernate_BookDao"/>
    </property>
   </bean>
  </property>
 </bean>
 
 
</beans>

---------------------

(2) jdbc.properties
## 这个事连接mysql数据库的具体参数配置,
## 楼主要根据自己的数据来配置,不可直接使用
---------------------

jdbc.driverClass=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysq