hibernate.properties及hibernate.cfg.xml

本文详细介绍了Hibernate两种配置方式:hibernate.properties与hibernate.cfg.xml的内容配置样例,包括数据库连接参数、方言设置等关键信息,并对比了两者的使用场景与优缺点。

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

[quote]
hibernate.properties 和 hibernate.cfg.xml是hibernate的配置文件,对于初学者,可能存在一些配置疑难。本文给出了 hibernate.properties及hibernate.cfg.xml内容配置样例,二者选其一即可



hibernate.properties

##---------- this is the connection and hibernate.dialect fo mysql --------------------------------
hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=org.gjt.mm.mysql.Driver
hibernate.connection.url=jdbc:mysql://ip:3306/databasename?lastUpdateCount=true;useUnicode=true;characterEncoding=gb2312
hibernate.connection.username=root
hibernate.connection.password=

##---------- this is the connection and hibernate.dialect fo mssqlserver --------------------------------
#hibernate.dialect=net.sf.hibernate.dialect.SQLServerDialect
#hibernate.connection.driver_class=net.sourceforge.jtds.jdbc.Driver
#hibernate.connection.url=jdbc:jtds:sqlserver://IP:1433;DatabaseName=databasename;charset=cp936
#hibernate.connection.username=sa
#hibernate.connection.password=sa

##---------- this is the connection and hibernate.dialect fo ibmdb2 --------------------------------
#hibernate.dialect=net.sf.hibernate.dialect.DB2Dialect
#hibernate.connection.driver_class=COM.ibm.db2.jdbc.app.DB2Driver
#hibernate.connection.url=jdbc:db2:databasename
#hibernate.connection.username=sa
#hibernate.connection.password=sa

##---------- this is the connection and hibernate.dialect fo Oracle --------------------------------
#hibernate.dialect=net.sf.hibernate.dialect.Oracle9Dialect
#hibernate.dialect=net.sf.hibernate.dialect.OracleDialect
#hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
#hibernate.connection.url=jdbc:oracle:thin:@localhost:1521:databasename
#hibernate.connection.username=ora
#hibernate.connection.password=ora

##---------- after is hibernate config

hibernate.show_sql=true

hibernate.query.substitutions=true 1, false 0, yes 'Y', no 'N'
hibernate.jdbc.use_streams_for_binary=true
hibernate.cache.region_prefix=hibernate.test
hibernate.cache.use_query_cache=true
hibernate.cache.provider_class=net.sf.ehcache.hibernate.Provider

hibernate.connection.pool.size=20
hibernate.jdbc.batch_size=25
hibernate.statement_cache.size = 6
jdbc.fetch_size=50
jdbc.use_scrollable_resultset=false
jdbc.use_scrollable_resultset=false
hibernate.jdbc.use_streams_for_binary=true
hibernate.max_fetch_depth=1

##############################
### Proxool Connection Pool###
##############################

## Properties for external configuration of Proxool

hibernate.proxool.pool_alias netAuditing

## Only need one of the following

#hibernate.proxool.existing_pool true
#hibernate.proxool.xml cfg/proxool.xml
#hibernate.proxool.properties proxool.properties

##########################
### Second-level Cache ###
##########################

## optimize chache for minimal "puts" instead of minimal "gets" (good for clustered cache)

#hibernate.cache.use_minimal_puts true


## set a prefix for cache region names

hibernate.cache.region_prefix hibernate.test


## enable the query cache

hibernate.cache.use_query_cache true


## choose a cache implementation

hibernate.cache.provider_class net.sf.ehcache.hibernate.Provider
[/quote]


[quote]
hibernate.cfg.xml

<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="connection.url">jdbc:mysql://IP:3306/databasename?lastUpdateCount=true;useUnicode=true;characterEncoding=gb2312</property>
<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="show_sql">false</property>


<!-- Mapping files -->
<mapping resource="com/orm.hbm.xml"/>

</session-factory>

</hibernate-configuration>

[/quote]

[quote]
可以啊,现在的hibernate都是hibernate.cfg.xml这个文件了,hibernate.properties这个是资源文件,你新建个资源文件吧名字改成这个就行了
hibernate.cfg.xml是新版本的。
可以手写也可以用ide自动生成模板后填内容。
.properties不能自动生成,他是自己手动建立的,hibernate.properties文件和hibernate.xml文件可以同事出现在项目中其加载优先级是。xml文件优先于。property文件。不过在开发中用xml文件配置就足够了,property文件不怎么用,而且从他里面读取属性的时候是以流的方式读取会降低效率


hibernate.properties文件和hibernate.xml文件可以同时出现啊。。

反正hibernate只用一个。。老版本才用hibernate.properties这个吧,,

新一点的版本都是hibernate.cfg.xml这个了哈。。
[/quote]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值