Hibernate学习笔记(2)——入门

下载

下载地址:
下载的版本是:
hibernate-distribution-3.6.10.Final.zip
解压文件:


hibernate3.jar是:Hibernate的核心jar包
lib文件夹下有如下文件夹


其中required文件夹中是Hibernate必须的第三方类库

project文件夹中的etc文件夹下包含了相关的  xml文件



使用

相关配置文件
  • hibernate.cfg.xml(hibernate配置文件,现在一般都是采用xml文件来进行配置的,但是当hibernate与spring整合时,我们通常是不需要hibernate的配置文件了,因为我们会在spring的配置文件ApplicationContext.xml中直接将hibernate的相关配置配置成Spring容器所管理的bean)
  • hibernate.properties(不会采用该文件来配置,当时其中提供了相应的hibernate的属性,在我们配置hibernate.cfg.xml或ApplicationContext.xml时时会用到的)
  • domain.hbm.xml(domain对象映射到数据中表的映射文件)
hibernate.cfg.xml要配置哪些东西
先来看下下载文件中提供的文件有哪些东写:
hibernate.cfg.xml

<!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 name="foo">
          <property name="show_sql">true</property>
          <mapping resource="org/hibernate/test/legacy/Simple.hbm.xml"/>
          <class-cache
               class="org.hibernate.test.legacy.Simple"
               region="Simple"
               usage="read-write"/>
     </session-factory>
</hibernate-configuration>


注意该文件中没有

<?xml version="1.0" encoding="utf-8"?>


从该文件中可得到信息:

一个dtd文件:hibernate-configuration-3.0.dtd(作用:是hibernate.cfg.xml的语法约束文件。可以将hibernate3.jar文件解压后到org\hibernate文件夹下找)
相关的标签:
  • <hibernate-configuration>(只有一个)
  • <session-factory>(只有一个,用于配置SessionFactory对象)
  • <property>(多个,用于配置SessionFactory对象的属性)
  •  <mapping>(多个,用于映射:domain.hbm.xml文件)
  • 等等



hibernate.properties中一些常用的属性:

## MySQL (MySQL的相关映射)
#hibernate.dialect org.hibernate.dialect.MySQLDialect (设置方言)
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password


## Oracle (Oracle 的相关映射
#hibernate.dialect org.hibernate.dialect.Oracle8iDialect
#hibernate.dialect org.hibernate.dialect.Oracle9iDialect
#hibernate.dialect org.hibernate.dialect.Oracle10gDialect
#hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
#hibernate.connection.username ora
#hibernate.connection.password ora
#hibernate.connection.url jdbc:oracle:thin:@localhost:1521:orcl
#hibernate.connection.url jdbc:oracle:thin:@localhost:1522:XE

## print all generated SQL to the console (将所有的生成SQL语句打印到控制台)
hibernate.show_sql true

## format SQL in log and console (在控制台和日志中格式化SQL语句)
hibernate.format_sql true

## auto schema export (自动根据domain.hbm.xml文件在数据库中创建表)
#hibernate.hbm2ddl.auto create-drop
#hibernate.hbm2ddl.auto create
#hibernate.hbm2ddl.auto update
#hibernate.hbm2ddl.auto validate


###########################
### C3P0 Connection Pool###
###########################

#hibernate.c3p0.max_size 2
#hibernate.c3p0.min_size 2
#hibernate.c3p0.timeout 5000
#hibernate.c3p0.max_statements 100
#hibernate.c3p0.idle_test_period 3000
#hibernate.c3p0.acquire_increment 2
#hibernate.c3p0.validate false



即:
  • hibernate.show_sql
  • hibernate.format_sql
  • hibernate.hbm2ddl.auto
  • hibernate.dialect(设置方言)
说明:我们一般会使用C3P0作为数据源来访问数据库,所以我们应该先去了解下 C3P0数据源的相关概念



domain.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.jast.blog.domain">
        <class name="Article">
                <id name="id" column="article_id" type="integer">
                        <generator class="identity"></generator>
                </id>
                <property name="title" type="string" />
                <property name="content" type="string"/>
        </class>
</hibernate-mapping>
说明:hibernate-mapping-3.0.dtd文件(作用:domain.hbm.xml是的语法约束文件。可以将hibernate3.jar文件解压后到org\hibernate文件夹下找)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值