使用maven創建一個簡單的hibernate項目(IDEA)

使用maven創建一個簡單的hibernate項目(IDEA),小白必備哦

環境

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

步驟:
1.創建一個maven項目,選擇quickstart,然後next
在這裏插入圖片描述
在這裏插入圖片描述2.填寫公司域名和項目名
在這裏插入圖片描述3. 設置maven根目錄,setting.xml地址和倉庫地址
點擊加號,添加屬性
再next
在這裏插入圖片描述4. 自動生成的項目名可修改
項目存放的路徑也可修改
在這裏插入圖片描述5. 打開一個新窗口
在這裏插入圖片描述6.等加載一會兒後,點擊下方Enable Auto-Import

在這裏插入圖片描述

7.在當前頁面加入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>

8.在main下新建一個resources文件夾,如果有就不用添加
在這裏插入圖片描述
9.右鍵點擊resources,對文件夾進行Resources Root的配置,好引入hibernate所需要的xml文件

在這裏插入圖片描述10.添加hibernate的文件
在這裏插入圖片描述
11.選擇facets,點擊加號,選擇hibernate
在這裏插入圖片描述12.選中項目,點擊ok
在這裏插入圖片描述13.點擊+號,添加
在這裏插入圖片描述14.選擇版本號,這裏建議使用推薦的5.2.x版本的就行
在這裏插入圖片描述15.點擊ok後,這時候項目下的resouces下會多出來一個hibernate.cfg.xml的文件。
在這裏插入圖片描述16.對hibernate的文件進行設置,主要是數據庫的關聯,以及與bean的關聯操作
將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">你的mysql密碼</property>

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

    <!--  mysql連接URL  -->
    <!--這裏的hibernatetest爲數據庫名-->
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatetest?useSSL=false</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.bdqn.entity.TuserEntity"></mapping>
  </session-factory>
</hibernate-configuration>

17.數據庫的連接
在這裏插入圖片描述18.填寫數據庫名,用戶名,密碼,然後測試,測試成功後點擊ok
我是默認的端口號,可以根據需要修改
在這裏插入圖片描述19.隨便寫一個數據庫的查詢語句,控制檯會出現數據

在這裏插入圖片描述20.將需要的表與類完成數據映射的關係
在這裏插入圖片描述21.出現Persistence後,然後選中右鍵進行關聯
在這裏插入圖片描述22.選中數據源及所對應的表
在這裏插入圖片描述yes
在這裏插入圖片描述
ok
在這裏插入圖片描述
23.自動生成實體類
在這裏插入圖片描述24.添加有參和無參構造方法
在這裏插入圖片描述25.在hibernate.cfg.xml裏修改映射類
在這裏插入圖片描述26.創建測試類TestCommodity 進行測試

package com.hibernate;

import com.hibernate.entity.CommodityEntity;
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 TestCommodity {
    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(){
        CommodityEntity comm = new CommodityEntity();
        //如果id是自增列,無需賦值,否則會報錯
        //comm.setId(3);
        comm.setSname("OPPOR15");
        comm.setBrand("OPPO");
        comm.setSid(2);
        comm.setSmoney(2400.0);
        session.save(comm);
    }

}

27.成功後
在這裏插入圖片描述在這裏插入圖片描述這樣一個簡單的hibernate的demo就可以啦

相關增刪改查操作,記得關注我呦!

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