一個Hibernate的Hello World, 基於Hibernate 4.0


先在官網上下載最近的Hibernate完整包, 目前最新的是hibernate-release-4.0.0.Final http://www.hibernate.org/downloads .

 

打開Myeclipse, 新建一個JavaProject. 

1.導入Hibernate需要的Jar包.

  需要加入的包有:hibernate-release-4.0.0.Final里面Lib目錄下Required的所有Jar包.

2.加入Hibernate配置文件hibernate.cfg.xml, 文件內容可參考文檔(這里使用的是么Mysql):

<?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/hibernate
</property>
<property name="connection.username">用戶名</property>
<property name="connection.password">密碼</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>

<!-- Enable Hibernate's automatic session context management
<property name="current_session_context_class">thread</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>
-->
<property name="myeclipse.connection.profile">
Hibernate_Test
</property>
<mapping resource="jyu/hibernate/model/Student.hbm.xml" />
<mapping class="jyu.hibernate.model.Teacher" />
</session-factory>
</hibernate-configuration>

3.新建一個實體類Student, 並成長get, set方法:

package jyu.hibernate.model;

public class Student {
private int id;
private String name;
private int age;


public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}


}


4.在數據庫中建好對應的表Student, 有id, name ,age三個字段.

5.編寫實體與關系的映射文件:Student.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>
<class name="jyu.hibernate.model.Student">
<id name="id" column="id"/>
<property name="name" />
<property name="age" />
</class>

</hibernate-mapping>

6.在hibernate.cfg.xml配置文件中加入Student.hbm.xml:在 </session-factory> 之前加入:<mapping resource="jyu/hibernate/model/Student.hbm.xml" />

7.測試:StudentTest

 

package jyu.hibernate.model;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class StudentTest {

public static void main(String[] agrs){
Student st = new Student();
st.setId(2);
st.setName("學生");
st.setAge(20);

Configuration cfg = new Configuration(); //讀取配置hibernate文件
SessionFactory sf = cfg.configure().buildSessionFactory(); //根據配置文件生成事務工廠
Session session = sf.openSession();
session.beginTransaction(); //事務開啟
session.save(st); //保存對象
session.getTransaction().commit();
session.close();
sf.close();
}
}

運行, 可以查看到數據庫中已經插入了一條記錄.


這種方法是比較常用的使用XML來映射實體與關系的方法, 另有使用Annotation(注解)方式也能完成映射功能.

 

本文為尚學堂馬士兵Hibernate視頻教程筆記.

 
        



  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM