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>

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

  1. Hibernate常用的Java数据类型映射到mysql和Oracle

    研究了常用的Java基本数据类型在mysql和oracle数据库的映射类型.这里使用的是包装类型做研究,一般在hibernate声明的时候最好不要用基本类型,因为数据库中的null空数据有可能映射为基 ...

  2. [Hibernate系列—] 2. 创建SessionFactory 与 Session

    Configuration 对象创建 要创建SessionFactory , 首先要创建Configuration 对象. 这个对象就是去读取hibernate 的一些配置信息. 默认状况下, hib ...

  3. [Hibernate系列—] 3. 映射文件与使用SchemaExport自动产生Schema

    自定义映射文件 这里的映射文件指的是对应到数据库表的xml 的定义文件. 对应的每个数据库表栏位, 可以定义的属性有: 属性名 类型 Description length number 栏位的长度 p ...

  4. Hibernate 系列 02 - Hibernate介绍及其环境搭建

    引导目录: Hibernate 系列教程 目录 昨晚喝多了,下午刚清醒,继续搞Hibernate.走起. 觉得还行的话,记得点赞哈,给我这个渣渣点学习的动力.有错误的话也请指出,省的我在错误上走了不归 ...

  5. 【SSH框架】之Hibernate系列一

    微信公众号:compassblog 欢迎关注.转发,互相学习,共同进步! 有任何问题,请后台留言联系! 1.Hibernate框架概述 (1).什么是Hibernate Hibernate是一个开放源 ...

  6. 《Hibernate 系列》- 入门例子

    2019独角兽企业重金招聘Python工程师标准>>> 跟我学 项目结构 --src/main/java ----com.easy.a_basic ------TestApp.jav ...

  7. (Hibernate进阶)Hibernate系列——总结篇(九)

    这篇博文是hibernate系列的最后一篇,既然是最后一篇,我们就应该进行一下从头到尾,整体上的总结,将这个系列的内容融会贯通. 概念 Hibernate是一个对象关系映射框架,当然从分层的角度看,我 ...

  8. hibernate系列之四

    数据库中表之间的关系: 一对一.一对多.多对多 一对多的建表原则:在多的一方创建外键指向一的一方的主键: 多对多的建表原则:创建一个中间表,中间表中至少有两个字段作为外键分别指向多对多双方的主键: 一 ...

  9. hibernate系列之一

    通过自己不断的学习框架以及相关知识的学习,自己学会总结了学习路上遇到的一些问题以及疑惑,自己现在跟着相关的学习资料又进行了一些总结和实践,希望通过自己走过的学习之路能够帮助小伙伴们解决一些学习上问题或 ...

最新文章

  1. MSCKF理论推导与代码解析
  2. 两个摄像头是如何将照片拼接在一起的
  3. C# C/S WPF 远程操作服务器上面的文件
  4. 关闭oracle服务 linux,Linux下启动和关闭Oracle服务与数据库
  5. 【SDOI2008】仪仗队
  6. 1449 砝码称重(思维)
  7. 【51单片机快速入门指南】6.3:DS18B20 单总线数字温度计的多路读取
  8. 榴莲肉多到流出来!引爆全国吃货的榴莲千层终于杀到来!100%好评
  9. vue中传值和传引用_vue prop属性传值与传引用示例
  10. Patsy库查看使用公式语法建模后的输入特征X 与 预测Y
  11. 使用代理,调用json-server的服务接口
  12. 【报告分享】2020直播电商生态白皮书.pdf(附下载链接)
  13. Go1.17 这个新特性竟是 6 年前提出来的
  14. 中望3d快捷键命令大全_cad快捷键命令大全
  15. 北京大学计算机系 丁主任,北大“扫地僧”韦东奕,真的是正常人吗?北大丁教授说出了答案...
  16. 关于安装VC++运行库遇到各种小问题
  17. vsftpd参数cmds_allowed
  18. 星浩资本快速发展引擎:IT就是生产力
  19. 计算机毕业设计之 少儿编程学习平台的设计与实现
  20. raid配置ssd为缓存_固态硬盘做缓存如何设置

热门文章

  1. 程序员的自我修养(转载)
  2. Linux signal 编程(转载)
  3. 5、SQL Server数据库、T-SQL
  4. IOS 深拷贝和浅拷贝应用
  5. IOS图标icon,png去掉高光效果
  6. 文件系统04 - 零基础入门学习Delphi37
  7. ASP.net MVC Mock Context(上下文)
  8. 关于用户自定义控件与引用该控件的页面之间的javascript脚本冲突
  9. 修改 Chrome浏览器主页被劫持 chrome 主页被篡改成hao.qquu8.com的解决方案
  10. ue4打包问题的巧妙解决——二分回退大法!