[Hibernate系列—] 1. 下载与试用Hibernate(MySQL与Oracle 配置)

Hibernate是什么?

对于学习Java的开发者来说,这个问题不应该是一个问题。

一句话: Hibernate 是针对Java环境的对象/关系映射的解决方案,或者说是第三方库。

它的作用就是省去了开发者花在Java类对应数据库表,Java数据对应到SQL数据类型的时间。减少了开发者花在处理SQL和JDBC的时间。

在开发角度来说,最大的不同是:

使用 JDBC开发时,基本还是组出SQL,通过JDBC  API来执行SQL。

而使用 Hibernate,对Java 对象进行save 或其他操作,就会自动保存到数据库中,也就是使用Hibernate  更体现了面向对象的概念, 当然, hibernae  的好处远不止于此。



如何获取hibernate

下载:

到hibernate 的官方网站下载。目前的最新版是 4.3.5.Final, 下载地址:

http://sourceforge.net/projects/hibernate/files/hibernate4/4.3.5.Final/hibernate-release-4.3.5.Final.zip/download

也可以到 :

https://onedrive.live.com/redir?resid=5B4EDBCD9EF1AB6B!191&authkey=!ABaYyXT8zlybvig&ithint=file%2c.zip

下载


目录结构

解压下载后的 zip 档, 解压后的目录结构如下:


documentation -- 存放了hibernate 的快速入门文档,开发手册,API文档等英文文档

project -- 存放了一些测试的例子

lib - 存放了jar 包,里面又分成了几个子目录。

   lib/required -- 一些核心包, 开发时需要把这个目录下的所有文件放到项目的classpath 下

   lib/jpa  - 主要是hibernate-entitymanager.jar , 依赖于  required里的jar 档。主要是JPA用的(Java Persistence API)

   lib/events

   lib/Optional



在Eclipse中建立测试项目

这里使用mysql 数据库测试, 要下载一个 Mysql JDBC 的jar 档。

到 http://dev.mysql.com/downloads/connector/j/ 下载

下载页面如下:

不过下载这个, 需要登录oralce 网站。

也可以直接到 https://onedrive.live.com/redir?resid=5B4EDBCD9EF1AB6B!193&authkey=!AHJhNgziTxpiRGg&ithint=file%2c.jar 下载



在Eclipse 中建立一个Java Project



1.  建立lib 目录, 将 hibernate  lib\required 目录下所有的jar 当和mysql jdbc 档(mysql-connector-java-5.1.30-bin.jar)放入lib 目录。 并导入



2.  在src 根目录下建立 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">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect-->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</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 resource="com/oscar999/Usr.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

3. 在src 下, 新建 com.oscar999 包, 加入Usr.hbm.xml, 内容如下:

<?xml version="1.0"?>

<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ Copyright (c) 2010, Red Hat Inc. or third-party contributors as
  ~ indicated by the @author tags or express copyright attribution
  ~ statements applied by the authors.  All third-party contributions are
  ~ distributed under license by Red Hat Inc.
  ~
  ~ This copyrighted material is made available to anyone wishing to use, modify,
  ~ copy, or redistribute it subject to the terms and conditions of the GNU
  ~ Lesser General Public License, as published by the Free Software Foundation.
  ~
  ~ This program is distributed in the hope that it will be useful,
  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  ~ or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
  ~ for more details.
  ~
  ~ You should have received a copy of the GNU Lesser General Public License
  ~ along with this distribution; if not, write to:
  ~ Free Software Foundation, Inc.
  ~ 51 Franklin Street, Fifth Floor
  ~ Boston, MA  02110-1301  USA
  -->

<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.oscar999">

    <class name="Usr" table="D_USER">
         <id name="emp_id" column="EMP_ID">
            <generator class="assigned"/>
        </id>
		<property name="user_name" column="USER_NAME"/>
		<property name="email_addr" column="EMAIL_ADDR"/>
		<property name="location" column="LOCATION"/>
		<property name="title" column="TITLE"/>
		<property name="mobile" column="MOBILE"/>
		<property name="extnum" column="EXTNUM"/>
        <property name="hire_date" type="timestamp" column="HIRE_DATE"/>
        <property name="resign_date" type="timestamp" column="RESIGN_DATE"/>
        <property name="active" column="ACTIVE"/>
    </class>

</hibernate-mapping>

4. 在同包目录下, 加入  Usr.java

package com.oscar999;

import java.util.Date;

public class Usr {

	private String emp_id;
	private String user_name;
	private String email_addr;
	private String location;
	private String title;
	private String mobile;
	private String extnum;
	private Date hire_date;
	private Date resign_date;
	private String active;

	public Usr() {
		// this form used by Hibernate
	}

	public Usr(String emp_id) {
		this.emp_id = emp_id;
	}
	
	public String getEmp_id() {
		return emp_id;
	}

	public void setEmp_id(String emp_id) {
		this.emp_id = emp_id;
	}

	public String getUser_name() {
		return user_name;
	}

	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}

	public String getEmail_addr() {
		return email_addr;
	}

	public void setEmail_addr(String email_addr) {
		this.email_addr = email_addr;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getExtnum() {
		return extnum;
	}

	public void setExtnum(String extnum) {
		this.extnum = extnum;
	}

	public Date getHire_date() {
		return hire_date;
	}

	public void setHire_date(Date hire_date) {
		this.hire_date = hire_date;
	}

	public Date getResign_date() {
		return resign_date;
	}

	public void setResign_date(Date resign_date) {
		this.resign_date = resign_date;
	}

	public String getActive() {
		return active;
	}

	public void setActive(String active) {
		this.active = active;
	}

}

5. 接下来就是写测试文件了 TestMySQL.java

/*
 * @author: oscar999
 * @Date:2014-6-3
 * Copyright (c) oscar999. All rights reserved.
 */
package com.oscar999;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

/**
 * <description>
 * 
 * @see
 * @see
 * 
 * @version 0.1, 2014-6-3
 * @author oscar999
 * @since JDK1.5
 */
public class TestMySQL {

	public static void main(String[] args) {
		Configuration configuration = new Configuration().configure();
		ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
				configuration.getProperties()).build();
		SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.save(new Usr("oscar999"));
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
	}

}

用workbench 查看db 的话, 就可以看到结果了。


再贴一下, 如果是oracle, 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">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:yoursid</property>
        <property name="connection.username">yourusername</property>
        <property name="connection.password">yourpassword</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect-->
        <property name="dialect">org.hibernate.dialect.OracleDialect</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 resource="com/oscar999/Usr.hbm.xml"/>

    </session-factory>

</hibernate-configuration>


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

oscar999

送以玫瑰,手留余香

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值