歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Hibernate4之Hello World(基礎環境搭建)

Hibernate4之Hello World(基礎環境搭建)

日期:2017/3/1 9:30:10   编辑:Linux編程

剛開始復習Hibernate,剛復習時,發現全忘了,連環境搭建都不會了,等於從頭再來啊,沒辦法硬著頭皮,只得從頭再來了。

Hibernate是一款優秀的ORM框架,即object relation mapping 對象關系映射。我的理解就是自動把pojo類對象的操作轉為對數據庫中相應表的操作。簡單說就是創建一個pojo類對象,那麼數據庫中相應的表中也會插入這麼一個對象。修改,刪除,當然也是的了。可以理解就是盡可能的隔離數據庫操作與java開發。

一、下載Hibernate jar包

在http://www.Hibernate.org/中找到需要的Hibernate版本。我下載的是最新的Hibernate-release-4.3.1.Final 。

二、復制jar包

復制在lib/required下的所有jar包到項目的lib文件夾下

三、部署Hibernate.cfg.xml,相應的pojo 的Student.hbm.xml

在項目的src目錄下新建Hibernate.cfg.xml文件。在documentation/manual/en-US/html_single/index.html

<?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:@127.0.0.1:1521:lubby</property>
<property name="connection.username">admin</property>
<property name="connection.password">admin</property>


<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</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">update</property> -->

<mapping resource="com/lubby/pojo/Student.hbm.xml"/>

</session-factory>

</hibernate-configuration>

前四個標簽是配置數據庫的驅動地址和用戶名密碼。

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

這個是配置數據庫名字和版本。在documentation/manual/en-US/html_single/index.html中能找到相應數據庫對應的字段 我用的是oracle 11g 對應就是上面所寫的

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

這個是選擇是否顯示sql語句。

<mapping resource="com/lubby/pojo/Student.hbm.xml"/>

這個非常重要是告訴hibernate配置文件所配置的pojo類的配置文件地址

四、創建pojo類Student

package com.lubby.pojo;

public class Student {
private String sid;
private String sname;
private int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String sid, String sname, int age) {
super();
this.sid = sid;
this.sname = sname;
this.age = age;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", age=" + age
+ "]";
}

}

五、配置Student.hbm.xml

每個pojo類都有一個相應的配置文件。都放在pojo類所在的包裡面。

<?xml version="1.0"?>
<!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.lubby.pojo">
<class name="Student" table="tab_stu">
<id name="sid" column="sid"></id>
<property name="sname" column="sname"></property>
<property name="age" column="age"></property>
</class>
</hibernate-mapping>

<class name="Student" table="tab_stu"> </class>

name:類名

table:數據庫中對應表名

<id name="sid" column="sid"></id>

id:是設置pojo類的主鍵,column是數據庫中對應的主鍵名。如果column不寫,默認和name的內容一樣。

<property name="sname" column="sname"></property>
<property name="age" column="age"></property>

property:設置一般的屬性

六、調用hibernate

package com.lubby.main;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.jboss.weld.logging.messages.EventMessage;

import com.lubby.pojo.Student;

public class StudentTest {

public static void main(String args[]){
Student stu = new Student();
stu.setSid("3");
stu.setAge(25);
stu.setSname("李惠堂");
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(stu);
session.getTransaction().commit();
session.close();
sf.close();

}
}

Hibernate整體理解 http://www.linuxidc.com/Linux/2014-07/104405.htm

Hibernate 的詳細介紹:請點這裡
Hibernate 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved