hibernate jpa,[Hibernate系列—] 1. 下載與試用Hibernate(MySQL與Oracle 配置)

 2023-10-12 阅读 22 评论 0

摘要:Hibernate是什么? 對于學習Java的開發者來說,這個問題不應該是一個問題。 hibernate jpa。一句話: Hibernate 是針對Java環境的對象/關系映射的解決方案,或者說是第三方庫。 它的作用就是省去了開發者花在Java類對應數據庫表,Java數據對應

Hibernate是什么?

對于學習Java的開發者來說,這個問題不應該是一個問題。

hibernate jpa。一句話: 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>


版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/135012.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息