Hibernate简单入门

             大三小菜鸟,找工作没找到,正好趁这段时间整理一下学习的资料,首先从Hibernate 开始。Hibernate是目前最流行的ORM(Object/Relation Mapping)框架产品,因此要了解Hibernate,首先必需要介绍一下ORM。

              ORM提供了对象到关系数据库的映射,对于目前所有流行的编程语言几乎都是面向对象的语言,而数据库目前还是以关系型数据库为主,并不是面向对象,所以很有必要出现一种工具,用来把针对对象的操作,对应的映射到数据库的相关操作上,因此出现了ORM。

              那么我们在应用程序中针对实体对象的操作,到底要映射到什么样的数据库,或者是怎么映射呢。这就是配置文件的作用了,java里面有大量的xml配置文件(xml的格式通过各种 dtd有严格的控制),官方文档有相关的DTD的介绍,摘录一段:

Hibernate DTD is sophisticated. You can use it for auto-completion of XML mapping elements and attributes in your editor or IDE. Opening up the DTD file in your text editor is the easiest way to get an overview of all elements and attributes, and to view the defaults, as well as some comments. Hibernate will not load the DTD file from the web, but first look it up from the classpath of the application. The DTD file is included in hibernate-core.jar (it is also included in the hibernate3.jar, if using the distribution bundle).具体的配置文件也有摘录:

 

<?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>

        <!-- Database connection settings -->
        <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="connection.url">jdbc:hsqldb:hsql://localhost</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.HSQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.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">update</property>

        <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

可见配置文件中指明了数据库的连接的各种信息,当热还有常用的一下属性,也列举出来:

hibernate.show_sql: 是否在控制台显示Hibernate生成的sql语句,true显示,false不显示

hibernate.format_sql:是否格式化显示sql语句,true显示,false不显示

hibernate_use_sql_comments:  是否在HIbernate生成的sql语句中添加有助于调试的注解  true显示,false不显示

hibernate.hbm2ddl.auto:当创建SessionFactory时,是否根据映射文件自动建立数据库表   可以为 create  update create-drop三个值。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值