标签:xml dialect Hibernate cfg 数据库 hibernate org 属性
eclipse学习(第三章:ssh中的Hibernate)——2.Hibernate怎么用
前言
本文参考自https://www.w3cschool.cn/hibernate/b79t1iea.html做的一个学习记录。
Hibernate相关jar包的下载位置
hibernate4及以上的jar包http://hibernate.org/orm/releases/
hibernate3的相关jar包下载地址
https://sourceforge.net/projects/hibernate/files/hibernate3/
查看Hibernate 相关属性
Hibernate 也需要一套相关数据库和其它相关参数的配置设置。所有这些信息通常是作为一个标准的 Java 属性文件提供的,名叫 hibernate.properties。又或者是作为 XML 文件提供的,名叫 hibernate.cfg.xml。
我们将考虑 hibernate.cfg.xml 这个 XML 格式文件,来决定在我的例子里指定需要的 Hibernate 应用属性。这个 XML 文件中大多数的属性是不需要修改的。这个文件保存在应用程序的类路径的根目录里。
如果你要使用Hibernate 属性,那么这个时候你应该先把上面的jar包下载下来,然后查询hibernate.cfg.xml文件。这里我以5.4.31这个版本为例说明一下大致的属性。
解压缩之后对应的hibernate.cfg.xml 大概会在这里
内容如下:这里面其实你英文翻译一下就能知道大概用途了,并不是很难,当然为了方便我下面加一个说明吧。(温馨提示:3版本的似乎还要加个hibernate.作为前缀)。当然了其实这里面并不知这一个hibernate.cfg.xml,有很多个,其他的你可以自行去了解。
<?xml version='1.0' encoding='utf-8'?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- Names the annotated entity class -->
<mapping class="org.hibernate.tutorial.annotations.Event"/>
</session-factory>
</hibernate-configuration>
属性 | 描述 |
---|---|
connection.driver_class | JDBC 驱动程序类,跟你使用什么数据库有关系,不同数据库不一样的 |
connection.url | 数据库连接地址,这里数据库以参数形式传入,以;分隔不同参数 |
connection.username | 数据库账号 |
connection.pool_size | 限制在 Hibernate 应用数据库连接池中连接的数量。 |
dialect | 设置Hibernate SQL方言,根据被选择的数据库生成适当的 SQL。如果还不是很懂可以看下一个表格 |
cache.provider_class | 设置二级缓存实现类的全名 |
show_sql | 日志文件中是否展示sql,一般都会写true,方便看问题 |
hbm2ddl.auto | 自动创建/更新/验证数据库表结构,详情下面有说明 |
hibernate.connection.autocommit | 这个最好写为false,跟事务有关系。 如果写为true的话会自动提交事务,这样多个语句操作的时候回滚会可能回滚不了导致数据异常,所以自己设置成手动开启事务false会好点。 |
其实这个hbm2ddl.auto参数的作用主要用于:自动创建/更新/验证数据库表结构。如果不是此方面的需求建议set value=“none”。
create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。
create-drop : 每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除。
update:最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据 model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等 应用第一次运行起来后才会。
validate : 每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。
对dialect属性的说明,这里数据库主要是用mysql和Oracle为主,其他的了解就好了,以防万一用到嘛
数据库 | 方言属性 |
---|---|
DB2 | org.hibernate.dialect.DB2Dialect |
HSQLDB | org.hibernate.dialect.HSQLDialect |
HypersonicSQL | org.hibernate.dialect.HSQLDialect |
Informix | org.hibernate.dialect.InformixDialect |
Ingres | org.hibernate.dialect.IngresDialect |
Interbase | org.hibernate.dialect.InterbaseDialect |
Microsoft SQL Server 2000 | org.hibernate.dialect.SQLServerDialect |
Microsoft SQL Server 2005 | org.hibernate.dialect.SQLServer2005Dialect |
Microsoft SQL Server 2008 | org.hibernate.dialect.SQLServer2008Dialect |
MySQL | org.hibernate.dialect.MySQLDialect |
Oracle (any version) | org.hibernate.dialect.OracleDialect |
Oracle 11g | org.hibernate.dialect.Oracle10gDialect |
Oracle 10g | org.hibernate.dialect.Oracle10gDialect |
Oracle 9i | org.hibernate.dialect.Oracle9iDialect |
PostgreSQL | org.hibernate.dialect.PostgreSQLDialect |
Progress | org.hibernate.dialect.ProgressDialect |
SAP DB | org.hibernate.dialect.SAPDBDialect |
Sybase | org.hibernate.dialect.SybaseDialect |
Sybase Anywhere | org.hibernate.dialect.SybaseAnywhereDialec |
cache.provider_class 属性的属性值为:
- org.hibernate.cache.HashtableCacheProvide(Hashtable)
- org.hibernate.cache.EhCacheProvider(EHCache)
- org.hibernate.cache.OSCacheProvider(OSCache)
- org.hibernate.cache.SwarmCacheProvider(SwarmCache)
- org.hibernate.cache.TreeCacheProvider(JBoss TreeCache)
小提示
最后说一句吧,如果你自己要写hibernate.cfg.xml这个文件的话,从我上面找到的那个文件中取以下代码作为开头即可。
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
标签:xml,dialect,Hibernate,cfg,数据库,hibernate,org,属性 来源: https://blog.csdn.net/weixin_43987277/article/details/117162984
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。