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

本文详细介绍了如何将Spring与Hibernate整合,同时使用C3P0数据库连接池进行配置,提高了系统的安全性和灵活性。通过配置jdbc.properties文件、修改applicationContext.xml和hibernate.cfg.xml文件,实现资源的有效管理和数据库操作的优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.youkuaiyun.com/source/3489150

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值