轉自:http://blog.csdn.net/qq_15096707/article/details/51419304 ,如需轉載,請聯系原博主。
參考:intellij配置hibernate自動生成hbm.xml文件 從零開始用Intellij idea14創建hibernate項目
下面我要講的創建方式,可能更加原生態,更加類似於Eclipse下創建Hibernate項目的方式,我想這也有助於對在Intellij IDEA下創建Hibernate項目的理解。
首先需要在Intellij IDEA下創建一個項目Project,相當於Eclipse下的workspace(工作空間),當然如果你此時選擇了創建Hibernate項目的方式,Intellij 在創建Project成功后會在Project下創建這一Hibernate項目。可能看起來有點奇怪,沒關系,我們可以把默認創建的東西刪除,然后創建我們的Module,相當於Eclipse下的Project。
創建Module --》選擇 Java Enterprise選項卡,點擊右側,勾選Web Application 和 Hibernate,如下:

選擇右下角的 Configure... ,選擇Module Library:

點擊下一步,輸入Module的名稱,這里我取名為:Hibernate_00,如:

等待 Hibernate 相關jar包下載完畢……


還需要添加mysql-jdbc jar包 和 junit jar包(junit jar包實際中可以不添加,根據實際需要進行添加):
對Hibernate_00 右鍵,選擇 Open Module Settings,或者按F4:



選擇 From Maven。


以同樣的方式下載 junit jar包並進行添加:




為方便以后創建Hibernate項目,可以為 hibernate 的配置文件創建模板:



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.username">root</property>
- <property name="connection.password"></property>
- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property>
- <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="show_sql">true</property>
- <property name="format_sql">true</property>
- <property name="hbm2ddl.auto">create</property>
- <mapping resource="Students.hbm.xml"/>
- <!-- DB schema will be updated if needed -->
- <!-- <property name="hbm2ddl.auto">update</property> -->
- </session-factory>
- </hibernate-configuration>
同樣地方式,創建對象/關系映射的配置文件模板,這里創建 entity2.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="Students" table="students">
- <id name="sid" type="int">
- <column name="sid"/>
- <generator class="assigned"/>
- </id>
- <property name="sname" type="java.lang.String">
- <column name="sname"/>
- </property>
- <property name="gender" type="java.lang.String">
- <column name="gender"/>
- </property>
- <property name="birthday" type="java.util.Date">
- <column name="birthday"/>
- </property>
- <property name="address" type="java.lang.String">
- <column name="address"/>
- </property>
- </class>
- </hibernate-mapping>
- <?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"></property>
- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property>
- <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="show_sql">true</property>
- <property name="format_sql">true</property>
- <property name="hbm2ddl.auto">create</property>
- <mapping resource="Students.hbm.xml"/>
- <!-- DB schema will be updated if needed -->
- <!-- <property name="hbm2ddl.auto">update</property> -->
- </session-factory>
- </hibernate-configuration>
在src下創建 Students 類:
- import java.util.Date;
- /**
- * Created by DreamBoy on 2016/5/15.
- */
- //學生類
- public class Students {
- //1. 必須為公有的類
- //2. 必須提供公有的不帶參數的默認的構造方法
- //3. 屬性私有
- //4. 屬性setter/getter封裝
- private int sid; //學號
- private String sname; //姓名
- private String gender; //性別
- private Date birthday; //出生日期
- private String address; //地址
- public Students() {
- }
- public Students(int sid, String sname, String gender, Date birthday, String address) {
- this.sid = sid;
- this.sname = sname;
- this.gender = gender;
- this.birthday = birthday;
- this.address = address;
- }
- public int getSid() {
- return sid;
- }
- public void setSid(int sid) {
- this.sid = sid;
- }
- public String getSname() {
- return sname;
- }
- public void setSname(String sname) {
- this.sname = sname;
- }
- public String getGender() {
- return gender;
- }
- public void setGender(String gender) {
- this.gender = gender;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- @Override
- public String toString() {
- return "Students{" +
- "sid=" + sid +
- ", sname='" + sname + '\'' +
- ", gender='" + gender + '\'' +
- ", birthday=" + birthday +
- ", address='" + address + '\'' +
- '}';
- }
- }
在src下創建 對象/關系映射的配置文件(根據模板進行創建) Students.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="Students" table="students">
- <id name="sid" type="int">
- <column name="sid"/>
- <generator class="assigned"/>
- </id>
- <property name="sname" type="java.lang.String">
- <column name="sname"/>
- </property>
- <property name="gender" type="java.lang.String">
- <column name="gender"/>
- </property>
- <property name="birthday" type="java.util.Date">
- <column name="birthday"/>
- </property>
- <property name="address" type="java.lang.String">
- <column name="address"/>
- </property>
- </class>
- </hibernate-mapping>
在hibernate.cfg.xml 配置文件中添加Students對象的映射。(前面的配置文件中已添加)
- <mapping resource="Students.hbm.xml"/>
在Module下創建test目錄,為了能在該目錄中創建java類,需要將目錄修改為 Sources Root:

此時才可以創建java類:StudentsTest.java
(這里使用到 junit 中的注解 @Test、@Before、@After)
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.service.ServiceRegistry;
- import org.hibernate.service.ServiceRegistryBuilder;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- import java.util.Date;
- /**
- * Created by DreamBoy on 2016/5/15.
- */
- //測試類
- public class StudentsTest {
- private SessionFactory sessionFactory;
- private Session session;
- private Transaction transaction;
- @Before
- public void init() {
- //創建配置對象
- Configuration config = new Configuration().configure();
- //創建服務注冊對象
- ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
- //創建會話工廠對象
- sessionFactory = config.buildSessionFactory(serviceRegistry);
- //會話對象
- session = sessionFactory.openSession();
- //開啟事務
- transaction = session.beginTransaction();
- }
- @After
- public void destory() {
- transaction.commit(); //提交事務
- session.close(); //關閉會話
- sessionFactory.close(); //關閉會話工廠
- }
- @Test
- public void testSaveStudents() {
- //生成學生對象
- Students s = new Students(1, "張三豐", "男", new Date(), "武當山");
- session.save(s); //保存對象進入數據庫
- }
- }
在hibernate.cfg.xml配置文件中,可以知道,這里我配置使用的數據庫,名為hibernate,所以我們需要在運行程序前,使用mysql創建好 hibernate 數據庫,不用創建Students表。

運行 StudentsTest.java ,可能會出現如下錯誤:

大概是java編譯輸出的位置出錯了,所以要對output輸出路徑進行配置:
這里選擇在當前Module下classes文件夾下:

再次運行程序:


程序運行成功,我們可以查看一下數據庫,會發現程序已經幫我們自動創建好了數據表,並添加了一條數據:


成功運行Hibernate項目。
