`
yanfaguanli
  • 浏览: 658288 次
文章分类
社区版块
存档分类
最新评论

SSH整合学习笔记之spring与hibernate整合(二)--C3P0

 
阅读更多

SSH整合学习笔记

现在将之前的spring与hibernate整合修改一下,使用C3P0数据库连接池。基本的配置没有改变,只是增加了一个jdbc.properties文件和修改了applicationContext.xml和hibernate.cfg.xml文件的相关配置。

jdbc.properties

jdbcUrl= jdbc:mysql:///spring2hibernate
driverClass = com.mysql.jdbc.Driver
username = root
password =root

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="dataSource">
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
<!-- 其他配置 -->
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="5"></property>
<property name="minPoolSize" value="3"></property>
<property name="acquireIncrement" value="2"></property>
<property name="maxStatements" value="8"></property>
<property name="maxStatementsPerConnection" value="5"></property>
<property name="maxIdleTime" value="20"></property>
</bean>
</property>
</bean>


<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>






<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>


<tx:advice id="advice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" read-only="false" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>


<aop:config>
<aop:pointcut id="perform" expression="execution(* com.ty.spring2hibernate.service..*.*(..))" />
<aop:advisor advice-ref="advice" pointcut-ref="perform" />
</aop:config>


<bean id="personDao" class="com.ty.spring2hibernate.dao.impl.PersonDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>


<bean id="personService" class="com.ty.spring2hibernate.service.impl.PersonServiceImpl">
<property name="personDao" ref="personDao" />
</bean>
</beans>

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/ty/spring2hibernate/domain/Person.hbm.xml" />
</session-factory>
</hibernate-configuration>

只要做了如上所述的修改就完成了使用c3p0数据库连接池的配置。大大的提高的安全性和灵活性。

源码下载地址:

http://download.csdn.net/source/3489150

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics