Intellij Idea 15 下建立 Hibernate 項目以及如何添加配置,intellijhibernate

來源:互聯網
上載者:User

Intellij Idea 15 下建立 Hibernate 項目以及如何添加配置,intellijhibernate

1.說明:Idea 下,項目對應於 Eclipse 下的 workspace,Module 對應於 Eclipse 下的項目。Idea 下,新添加的項目既可以單獨作為一個 Project,也可以作為一個 Project 下的 Module。

2.本篇文章介紹內容:

(1)如何在 Project 建立 Hibernate Module。

(2)如何添加 jar 包到 Module 下。

(3)如何添加 hibernate.cfg.xml,以及如何自訂模板。

(4)如何添加 Entity.hbm.xml 檔案,以及自動產生實體。

3.在最開始前,添加 Hibernate 的外掛程式。

4.如何在 Project 下建立 Hibernate Module。

(1)建立一個空項目。

(2)點擊 Finish 之後,會彈出 Module 結構圖。

(3)建立 Hibernate Framework 的 Module。

說明:第一處表紅的地方選擇後會預設建立 hbm.cfg.cml 檔案以及一個測試類別,點擊 Configure 會彈出第二張圖,需要注意的是 level 的選擇。

(4)建立完成。

5.如何添加 jar 包到 Module 下。

(1)開啟 Project Structure 。

(2)選擇 library。選擇從 maven 從下載。

(3)點擊 OK 後,會彈出 Configure Library 的彈窗,同樣注意 level 的選取。

(4)選中添加的 Jar 包,點擊 Add Selected 按鈕完成添加。

6.如何添加 hibernate.cfg.xml,以及如何自訂模板。

(1)若在建立 Module 的時候沒有選擇建立 hibernate.cfg.xml 檔案,可以通過如下的方式來添加。

(2)點開 Project Structure

點擊加號,選擇 hibernate.cfg.xml。

(3)預設添加的 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> -->  </session-factory></hibernate-configuration>

(4)自訂模板。

如果覺著 Idea 給添加的 hibernate.cfg.xml 不太友好的話,可以通過自訂模板的方式來添加適合自己的檔案。

點擊 OK 之後就可以使用添加的 hibernate.cfg.xml。

7.如何添加 Entity.hbm.xml 檔案,以及自動產生實體。

(1)說明:在 Eclipse 下,添加 Hibernate tool 後,可以根據已經建立的實體去建立對應的 Entity.hbm.xml 檔案,然後在程式啟動的時候,

會在資料庫產生對應的表資訊。而在 Idea 下,是根據表和 hibernate.cfg.xml 去建立的實體和 Entity.hbm.xml 檔案,至於能否根據實體

去建立 Entity.hbm.xml 和表資訊,現在還沒有探索出來,探索出來時再進行補充,也希望知道的童鞋告訴我,謝謝。

(2)在 hibernate.cfg.xml 檔案中配置串連資料庫基本資料,以及 Hibernate 基本資料和自動產生資料表策略。

<?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.username">root</property>        <property name="connection.password">123456</property>        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="connection.url">jdbc:mysql:///hibernate</property>        <!-- 配置 Hibernate 的基本資料 -->        <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>        <property name="show_sql">true</property>        <property name="format_sql">true</property>        <!-- 指定自動產生資料表的策略 -->        <property name="hbm2ddl.auto">update</property>    </session-factory></hibernate-configuration>

(3)點擊 Persistance 視圖(View-ToolWindow-Persistance 或 直接點擊捷徑)

如果沒有已經建立的 data source ,可以通過點擊標紅的按鈕進行添加。如:

在不勾選 JPA Annotations 的情況下,產生的實體不含 JPA 註解。如:

/** * @author solverpeng * @create 2016-09-28-14:11 */public class NewsEntity { private int id; private String name; 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; } @Override public boolean equals(Object o) { if(this == o) { return true; } if(o == null || getClass() != o.getClass()) { return false; } NewsEntity that = (NewsEntity) o; if(id != that.id) { return false; } if(name != null ? !name.equals(that.name) : that.name != null) { return false; } return true; } @Override public int hashCode() { int result = id; result = 31 * result + (name != null ? name.hashCode() : 0); return result; }}NewsEntity.java

對應的 NewsEntity.hbm.xml 檔案

<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping> <class name="com.nucsoft.hibernate.NewsEntity" table="news" schema="hibernate"> <id name="id"> <column name="id" sql-type="int(11)"/> </id> <property name="name"> <column name="name" sql-type="varchar(50)" length="50"/> </property> </class></hibernate-mapping>NewsEntity.hbm.xml

在勾選 JPA Annotations 的情況下,產生的實體包含 JPA 註解。如:

/** * @author solverpeng * @create 2016-09-28-14:16 */@Entity@Table(name = "news", schema = "hibernate", catalog = "")public class NewsEntity { private Integer id; private String name; @Id @Column(name = "id", nullable = false) public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Basic @Column(name = "name", nullable = false, length = 50) public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public boolean equals(Object o) { if(this == o) { return true; } if(o == null || getClass() != o.getClass()) { return false; } NewsEntity that = (NewsEntity) o; if(id != null ? !id.equals(that.id) : that.id != null) { return false; } if(name != null ? !name.equals(that.name) : that.name != null) { return false; } return true; } @Override public int hashCode() { int result = id != null ? id.hashCode() : 0; result = 31 * result + (name != null ? name.hashCode() : 0); return result; }}NewsEntity.java

注意: Gennerate Separate XML per Entity 這個選項,意思是為每一個 Entity 產生一個 hbm.xml 檔案。

在勾選 Genernate JPA Annotations 選項的情況下,可以不勾選它。但是如果沒有勾選 Genernate JPA Annotations 選項,需要勾選 Gennerate Separate XML per Entity。

8.總結:

介紹了 Intellij Idea 下如何建立 Hibernate 項目以及如何組建組態資訊。事實上,Idea 還能完成表和表之間關係的處理,和 hql 語句的測試,關於這兩個方面,在以後的文章中進行探索說明。

同樣也介紹了 Module 的建立。

9.題外篇

如何添加別的架構?如上面添加了 Hibernate 架構,那麼如何再添加 Spring 架構呢?

看圖說話,可以通過此種方式來添加。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.