1. 程式人生 > >hibernate學習之在intellij idea下使用maven建立hibernate專案(詳細圖文教程)

hibernate學習之在intellij idea下使用maven建立hibernate專案(詳細圖文教程)

在上次學習hibernate基礎瞭解之後,這裡再次進行intellij idea IDE使用maven進行hibernate的安裝,並跑通一個HelloWord

環境

  • windows 7 64位
  • intellij idea
  • maven
  • hibernate

本次教程針對毫無經驗的小白,比如我,然後教你如何一步步的成功執行起hibernate的helloword

建議先稍微看下之前的這兩篇文章:

使用IntelliJ IDEA通過Maven建立Spring的HelloWord(超詳細圖文教程)
使用Intellij IDEA通過Maven搭建struts2的HelloWord(超詳細圖文教程)

步驟:

(一)建立Maven專案,選擇quickstart專案,點選next
在這裡插入圖片描述
(二)根據自己隨意命名專案名稱:一般是公司名+專案名
在這裡插入圖片描述
(三)設定Maven倉庫地址及路徑,點選+號,填寫資訊,選擇ok,並點選下一步
在這裡插入圖片描述
(四)填寫專案名,專案存放路徑
在這裡插入圖片描述
(五)新視窗開啟
在這裡插入圖片描述
(六) 建立成功後的整個架構
在這裡插入圖片描述
點選Auto 後
在這裡插入圖片描述
(七)匯入hibernate需要依賴的Jar包

    <!--引入hibernate包-->
    <dependency>
      <groupId>org.hibernate</groupId>
      <
artifactId
>
hibernate-core</artifactId> <version>5.2.9.Final</version> </dependency> <!--引入mysql包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</
version
>
</dependency>

都知道hibernate是一種ORM的框架,是基於面向物件對資料庫進行增刪改查,所以這裡有需要用到mysql包
在這裡插入圖片描述
(八)對專案進行Sources Root 與Resources Root的配置,好引入hibernate所需要的xml檔案
在這裡插入圖片描述
(九)新增hibernate的檔案
在這裡插入圖片描述
選擇Facets,後選擇+號,再選擇hibernate
在這裡插入圖片描述
選中後,選中當前專案,點選OK
在這裡插入圖片描述
ok後再選擇+號,進行新增
在這裡插入圖片描述
選擇版本號,這裡建議使用推薦的5.2.x版本的就行
在這裡插入圖片描述
點選ok後,這時候專案下的resouces下會多出來一個hibernate.cfg.xml的檔案。
在這裡插入圖片描述

(十)對hibernate的檔案進行設定,主要是資料庫的關聯,以及與bean的關聯操作
在這裡插入圖片描述
將修改為:

<?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"/>-->
    <!--<property name="connection.driver_class"/>-->
    <!-- <property name="connection.username"/> -->
    <!-- <property name="connection.password"/> -->

    <!-- DB schema will be updated if needed -->
    <!-- <property name="hbm2ddl.auto">update</property> -->


    <property name="hibernate.connection.username">root</property>

    <!--  mysql密碼  -->
    <property name="hibernate.connection.password">h5$CYMdjWZp</property>

    <!--  mysql驅動  -->
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

    <!--  mysql連線URL  -->
    <!--建議:mysql的url最好是這樣寫上,如果要連線遠端mysql,只需要把127.0.0.0.1的改為遠端mysql地址即可,這裡的zhibo 為資料庫名,後面跟上的一串為字串的編碼,建議都寫上-->
    <property name="hibernate.connection.url">jdbc:mysql://127.0.0.0.1:3306/zhibo?useUnicode=true&amp;characterEncoding=UTF-8</property>

    <!--  資料庫方言  -->
    <!--<property name="dialect">org.hibernate.dialect.MySQLDialect</property>-->
    <!--// 5.0之後使用的資料庫方言-->
    <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

    <!--  顯示sql語句  -->
    <property name="show_sql">true</property>

    <!--  格式化sql語句  -->
    <property name="format_sql">true</property>

    <!--  根據需要建立資料庫  (這裡的操作是,如果資料庫中已經有這個表,就將這個表刪除掉,重新建立表格)-->
    <!--<property name="hbm2ddl.auto">create</property>-->
    <!--新增剛剛生成的對映類-->
    <mapping class="com.hibernatedemo.dao.TbAreaEntity"></mapping>
  </session-factory>
</hibernate-configuration>

修改設定後的圖片
在這裡插入圖片描述
(十一)使用intellij idea的database資料庫進行連線資料庫。
在這裡插入圖片描述
填寫賬號,密碼,ip地址,資料庫名等
在這裡插入圖片描述
(十二)在intellij idea 中進行資料庫的查詢,有沒有被驚訝到?
在這裡插入圖片描述
(十三)使用idea來建立一個student表
在這裡插入圖片描述
在這裡插入圖片描述
建立好後,點選ok
在這裡插入圖片描述
多出一張student表
在這裡插入圖片描述
(十四)將剛建立好的表與類完成資料對映的關係
在這裡插入圖片描述
這時候我們會發現出現Persistence,然後選中右鍵進行關聯
在這裡插入圖片描述
選中資料來源及所對應的表
在這裡插入圖片描述
yes
在這裡插入圖片描述
ok
在這裡插入圖片描述
生成StudentEntity
在這裡插入圖片描述
增加兩個構造方法
在這裡插入圖片描述
(十五)在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"/>-->
    <!--<property name="connection.driver_class"/>-->
    <!-- <property name="connection.username"/> -->
    <!-- <property name="connection.password"/> -->

    <!-- DB schema will be updated if needed -->
    <!-- <property name="hbm2ddl.auto">update</property> -->


    <property name="hibernate.connection.username">root</property>

    <!--  mysql密碼  -->
    <property name="hibernate.connection.password">h5$CYMZdjWZp</property>

    <!--  mysql驅動  -->
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

    <!--  mysql連線URL  -->
    <!--建議:mysql的url最好是這樣寫上,如果要連線遠端mysql,只需要把127.0.0.0.1的改為遠端mysql地址即可,這裡的zhibo 為資料庫名,後面跟上的一串為字串的編碼,建議都寫上-->
    <property name="hibernate.connection.url">jdbc:mysql://127.0.0.0.1:3306/zhibo?useUnicode=true&amp;characterEncoding=UTF-8</property>

    <!--  資料庫方言  -->
    <!--<property name="dialect">org.hibernate.dialect.MySQLDialect</property>-->
    <!--// 5.0之後使用的資料庫方言-->
    <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

    <!--  顯示sql語句  -->
    <property name="show_sql">true</property>

    <!--  格式化sql語句  -->
    <property name="format_sql">true</property>

    <!--  根據需要建立資料庫  (這裡的操作是,如果資料庫中已經有這個表,就將這個表刪除掉,重新建立表格)-->
    <!--<property name="hbm2ddl.auto">create</property>-->
    <!--新增剛剛生成的對映類-->
    <mapping class="com.mhibernate.hibernatedemo.StudentEntity"></mapping>
  </session-factory>
</hibernate-configuration>

(十六)建立TestStudent測試類,並進行測試。


package com.mhibernate.hibernatedemo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestStudent {
    private SessionFactory sessionFactory;
    private Session session;
    private Transaction transaction;

    // 測試開始
    @Before
    public void init(){
        //建立會話工廠
        sessionFactory = new Configuration().configure().buildSessionFactory();
        // 開啟會話
        session =sessionFactory.openSession();
        //開啟事物
        transaction = session.beginTransaction();
    }

    // 測試結束
    @After
    public void destory(){
        //關閉事物
        transaction.commit();
        //關閉對話
        session.close();
        // 關閉會話工廠
        sessionFactory.close();
    }
//    下面是測試用例
    @Test
    public void testStudent(){
        StudentEntity student = new StudentEntity();
        student.setAge(233);
        student.setName("周杰倫");
        session.save(student);
    }
}

執行成功後的截圖:
在這裡插入圖片描述
在這裡插入圖片描述
因為之前有插入過資料,所以這裡顯示了三條了資料。

以上就是在intellij idea下使用Maven建立hibernate專案的HelloWord,如果有不懂的地方,歡迎在評論區留言。


個人網站:http://xudailong.cc

關注「蛇崽網盤教程資源」公眾號 ,在微信後臺回覆「領取資源」,獲取IT資源200G乾貨大全。

更多資源請訪問:

超詳細圖文搭建個人免費部落格

關注「蛇崽網盤教程資源」公眾號 ,在微信後臺回覆「領取資源」,獲取IT資源200G乾貨大全。

在微信後臺回覆「130個小程式」,即可免費領取享有匯入就能跑的微信小程式

在這裡插入圖片描述