spring.xml与hibernate数据源 整合

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">

<!-- 定义bean数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">

<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="user" value=" root "></property>
<property name="password" value="123456"></property>

<!-- 指定连接数据库连接池的最大连接数 -->
<property name="maxPoolSize" value="10"></property>
<!-- 指定连接数据库连接池的最小连接数 -->
<property name="minPoolSize" value="5"></property>
<!-- 指定连接数据库连接池的初始化连接数 -->
<property name="initialPoolSize" value="5"></property>
<!-- 指定连接数据库连接池的连接的最大空闲时间 -->
<property name="maxIdleTime" value="800"></property>
<!--每60秒检查所有连接池中的空闲连接。Default: 0 --> 
<property name="idleConnectionTestPeriod" value="60"></property>
<!-- JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。
          但由于预缓存的statements属于单个 connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
          如果maxStatements与 maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 --> 
<property name="maxStatements" value="5"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> 
<property name="acquireIncrement" value="1"></property>

</bean>

<!--配置数据库会话工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan">
<list>
<value>com.xg.hospital.Pojo</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<!-- 针对oracle数据库的方言,特定的关系数据库生成优化的SQL -->
<prop key="hibernate.dialect">
org.hibernate.dialect.OracleDialect
</prop>
<!-- 是否在控制台打印sql语句 -->
<prop key="hibernate.show_sql">true</prop>
<!-- 设置外连接抓取树的最大深度取值. 0禁用默认外连接提取。 建议设置为0到3之间 -->
<prop key="hibernate.max_fetch_depth">2</prop>
<!--connection.characterEncoding连接数据库时数据的传输字符集编码方式,最好设置为gbk,用gb2312有的字符不全 -->
<prop key="hibernate.connection.characterEncoding">gbk</prop>
<!--jdbc.batch_size是指Hibernate批量插入,删除和更新时每次操作的记录数。Batch Size越大,批量操作的向数据库发送Sql的次数越少,速度就越快,同样耗用内存就越大 -->
<prop key="hibernate.jdbc.batch_size">20</prop>
<!-- 配置二级缓存 默认是关闭的 -->
<prop key="hibernate.generate_statistics">true</prop>
<!-- 启动"查询缓存"如果想缓存使用findall()、list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集,必须配置此项 -->
<prop key="hibernate.cache.use_query_cache">true</prop>
<!-- 开启二级缓存 -->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<!-- 开启CGLIB来替代运行时反射机制(系统级属性). 反射机制有时在除错时比较有用. 注意即使关闭这个优化, Hibernate还是需要CGLIB. 
你不能在hibernate.cfg.xml中设置此属性.取值 true | false -->
<prop key="cglib.use_reflection_optimizer">false</prop>


<!-- 选择HQL解析器的实现 -->
<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory
</prop>
<!-- 连接释放配置, 默认: auto -->
<prop key="hibernate.connection.release_mode">auto</prop>
<!-- 连接数据库超时设置autoReconnect=true -->
<prop key="hibernate.autoReconnect">true</prop>


<!-- 常见的缓存组件 在默认情况下,Hibernate会使用EHCache作为二级缓存组件。但是,可以通过设置hibernate.cache.provider_class属性,指定其他的缓存策略,该缓存策略必须实现org.hibernate.cache.CacheProvider接口。 
通过实现org.hibernate.cache.CacheProvider接口可以提供对不同二级缓存组件的支持,此接口充当缓存插件与Hibernate之间的适配器。 
组件                                        Provider类      类型       集群          查询缓存
    Hashtable org.hibernate.cache.HashtableCacheProvider 内存 不支持 支持 
    EHCache org.hibernate.cache.EhCacheProvider 内存,硬盘 不支持 支持
    OSCache org.hibernate.cache.OSCacheProvider 内存,硬盘 支持 支持
    SwarmCache org.hibernate.cache.SwarmCacheProvider 集群 支持 不支持
    JBoss TreeCache org.hibernate.cache.TreeCacheProvider 集群 支持 支持 
 -->
<prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
</props>
</property>
</bean>
</beans>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值