阅读196 返回首页    go 阿里云 go 技术社区[云栖]


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="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:context="https://www.springframework.org/schema/context" xmlns:aop="https://www.springframework.org/schema/aop"
xmlns:tx="https://www.springframework.org/schema/tx"
xsi:schemaLocation="https://www.springframework.org/schema/beans 
      https://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
      https://www.springframework.org/schema/context 
      https://www.springframework.org/schema/context/spring-context-2.5.xsd 
      https://www.springframework.org/schema/aop 
      https://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
      https://www.springframework.org/schema/tx 
      https://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">
<context:property-placeholder location="classpath:jdbc.properties" />
<bean >
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="dataSource">
<bean >
<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 >
<property name="sessionFactory" ref="sessionFactory" />
</bean>






<bean >
<property name="sessionFactory" ref="sessionFactory" />
</bean>


<tx: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 expression="execution(* com.ty.spring2hibernate.service..*.*(..))" />
<aop:advisor advice-ref="advice" pointcut-ref="perform" />
</aop:config>


<bean >
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>


<bean >
<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"
"https://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数据库连接池的配置。大大的提高的安全性和灵活性。

源码下载地址:

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

最后更新:2017-04-02 06:51:49

  上一篇:go Android PreferenceActivity 学习笔记
  下一篇:go vimdiff的简单用法