學習Hibernate創建一個簡單案例

由於博主喜歡使用IntelliJ IDEA 開發,個人覺得IntelliJ IDEA 功能太強大了,所以很喜歡。如果是Eclipse操作Hibernate就需要做一系列煩絮的導包工作。下面我將用IntelliJ IDEA 使用Hibernate框架,創建一個簡單對數據庫插入信息案例。

 

目錄

一、數據建庫

二、用IDEA創建Hibernate項目

三、引用數據庫mysql到項目中

四、配置hibernate.cfg.xml文件

五、創建一個測試test類,對Hibernate對數據庫操作進行測試

六、遇到的坑


整體項目結構:

一、數據建庫

1、數據庫使用mysql,在mysql創建數據庫名爲“hiberbate”(由於過於慌張,本來數據庫名叫Hibernate的,將就用吧)

2、創建一個表“Student” 

二、用IDEA創建Hibernate項目

   1、創建新項目窗口中,點擊java,勾選web Application,鼠標向下滑動勾選Hibernate,再勾選上Create degault hibernate configuration and main class就會自動生成hibernate.cfg.xml文件

點擊next創建項目名字(本次項目取名爲Hibernate01),點擊finish。Hibernate項目創建成功,IDEA自動在項目中創建lib文件夾,並且下載好了Hibernate相關的包

三、引用數據庫mysql到項目中

1、點擊IDEA右上角的Database

2、點擊+號,展開點擊Mysql

3、創建MySQL連接,完成後點擊Apply,點擊ok

完成後就出現下圖

 

4、映射數據庫到項目中,點擊IDEA左下角persistence,鼠標右擊項目名Hibernate01,

根據箭頭指示點擊By Database Schema。

出現如下圖:

 

隨後在指定包中自動生成映射文件:

 

四、配置hibernate.cfg.xml文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306/hiberbate</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">15282896494</property>
        <!--配置本地事務-->
        <property name="hibernate.current_session_context_class">thread</property>


        <!--配置數據庫方言-->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>


        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <!--指定自動生成數據表的策略-->
        <property name="hbm2ddl.auto">update</property>

        <!--映射文件-->
        <mapping resource="yongjie/ben/StudentEntity.hbm.xml"></mapping>
    </session-factory>
</hibernate-configuration>

五、創建一個測試test類,對Hibernate對數據庫操作進行測試

Test.java文件

package yongjie.ben;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;


 class majorTest {

     private static SessionFactory sessionFactory;

     private static Session session;
     private static  Transaction transaction;

    public static  void main(String[] args){
        //1. 創建配置對象
        Configuration config = new Configuration().configure("/hibernate.cfg.xml");

        //3. 創建會話工廠對象
        sessionFactory = config.buildSessionFactory();
        //4. 會話對象
        session = sessionFactory.openSession();
        //5. 開啓事務
        transaction = session.beginTransaction();

        //6. 生成專業對象
        StudentEntity studentEntity = new StudentEntity();

        //新增
        studentEntity.setId(11);
        studentEntity.setName("11");
        studentEntity.setPassword("11");
        studentEntity.setAddress("11");
        studentEntity.setSex("11");

        //7. 保存對象進入數據庫
        session.save(studentEntity);

        //8. 提交事務
        transaction.commit();
        //9. 關閉會話
        session.close();


    }
}

運行通過後,控制檯出現如下圖

刷新數據庫,出現添加的數據。

六、遇到的坑

1、出現報錯:Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

       檢查hibernate.cfg.xml文件是否輸入完整,數據庫名字和密碼是否輸入正確。

2、方言在Hibernate5以上用

<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

3、com.mysql.jdbc.Driver報錯未找到包,需要問們手動導入。

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>

 

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