學習hibernate,創建hibernate項目的過程,以及創建一個基於hibernate的工具類,實現增刪改查

創建一個hibernate項目

這裏爲了快速和方便體現hibernate,創建的是一個簡易的java項目,架構如下



首先第一步是:創建一個java項目或者web項目

第二步是:導入hibernate的一些依賴jar包,如果是java項目,就直接在項目右鍵properties,右邊選擇第二個(外部導入)

如果是web項目可以直接把jar包複製到lib下

這裏是hibernate的一些依賴jar包


第三步是:創建一個實體類,例如student類(假設類所在的這個包是com.MyHibernateDemo,entity),類屬性有,name,id,year,給seter和geter方法;並且在這個包下配置對象對應的xml文件(取名xxx.hbm.xml,xxx表示的是實體類名稱),同時也在src目錄下創建一個hibernate.cfg.xml配置文件。

其中xxx.hbm.xml的內容爲:

<?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.MyHibernateDemo.entity">
    <class name="Student" table="student">//name表示這個包下的一個類,table表示一個數據庫表,這裏是把類和表關聯起來。這裏指的注意的是,最好讓實體類的屬性和表的字段關聯起來。
        <id name="id" column="id">
            <generator class="native">//這裏是mysql的主鍵自增的意思
            </generator>
        </id>
        <property name="stu_name" />
        <property name="stu_no" />
        <property name="stu_score" />
    </class>
     

</hibernate-mapping>

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/student?characterEncoding=UTF-8</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="current_session_context_class">thread</property>
        <property name="show_sql">true</property>

        <property name="hbm2ddl.auto">update</property>

       //這裏是表示映射到這個包下的某個配置文件

        <mapping resource="com/MyHibernateDemo/entity/Student.hbm.xml" />
    </session-factory>
 
</hibernate-configuration>


第四步是:創建一個工具類(可以重新創建在一個新包中),例如HibernateUtil類,在這個類中的內容是

public class HibernateUtil {


private static SessionFactory sessionfactory;

static{//這裏是靜態代碼塊,會首先執行這裏的代碼,也就是說會先加載配置文件,得到sessionfactory的初始化數據
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
sessionfactory =cfg.buildSessionFactory();
}

public static SessionFactory getSessionFactory(){
return sessionfactory;

}

   public static Session getSession(){
    return sessionfactory.openSession();
   }
   public static void add(Object entity){
Session s=null;
Transaction tx=null;
try{
s=HibernateUtil.getSession();
tx=s.beginTransaction();
s.save(entity);
tx.commit();
}finally{
if(s!=null){
s.close();
}
}
}
   public static void update(Object entity){
Session s=null;
Transaction tx=null;
try{
s=HibernateUtil.getSession();
tx=s.beginTransaction();
s.update(entity);
tx.commit();
}finally{
if(s!=null){
s.close();
}
}
}

   public static void delete(Object entity){
Session s=null;
Transaction tx=null;
try{
s=HibernateUtil.getSession();
tx=s.beginTransaction();
s.delete(entity);
tx.commit();
}finally{
if(s!=null){
s.close();
}
}
}
   public static Object get(Serializable id){
Session s=null;
try{
s=HibernateUtil.getSession();
Object obj=s.get(Student.class, id);
return obj;
}finally{
if(s!=null){
s.close();
}
}
}

}

這個工具類包含了增刪改查基本的操作

第五步是:創建一個測試類內容爲:

package com.MyHibernateDemo.Test;


import com.MyHibernateDemo.Util.HibernateUtil;
import com.MyHibernateDemo.entity.Student;


public class HibernateTest {

public static void main(String []args){


Student student=new Student();
student.setStu_score(100);
student.setStu_name("Hello World LinJi");
student.setStu_no(123);
HibernateUtil.add(student);

System.out.println("添加結束");

System.out.println(HibernateUtil.get(2));
}
}

以上爲添加一個學生,並且獲取一個id號爲2的學生的信息

我們的實體類內容爲:

package com.MyHibernateDemo.entity;


public class Student {

private int id;
private int stu_no;
private String stu_name;
private int stu_score;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getStu_no() {
return stu_no;
}
public void setStu_no(int stu_no) {
this.stu_no = stu_no;
}
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
public int getStu_score() {
return stu_score;
}
public void setStu_score(int stu_score) {
this.stu_score = stu_score;
}
@Override
public String toString() {
return "Student [id=" + id + ", stu_no=" + stu_no + ", stu_name="
+ stu_name + ", stu_score=" + stu_score + "]";
}


}

這就是基本的hibernate框架入門使用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章