引言:使用連接數據庫來創建實體類,很快就學會了,但是對於怎么用hibernate創建數據表確實用了兩天的時間,記錄下這次學習的過程。
1、在一開始怎么都搜不到資料,后來才明白,在搜的時候要加正向的,用數據庫創建實體類是反向,用實體類創建數據表是正向的,這樣搜就可以了
2、在一開始的時候,總是執着於IDE和插件的問題,對於所遇到的問題沒有進行深入的研究,導致浪費掉了大量的時間(最后還是沒搞定)
3、總是執着於教程上的操作方式,到最后才發現,在新版本里的寫法是不一樣的,坑之又坑
4、浪費時間最長的是如何使用hibernate用實體類生成hbm.xml文件,在教程里看老師用的挺溜的,因為電腦的原因,ide、插件安裝失敗,所以最后是只能是手寫了
1、創建hibernate項目:參考資料:https://blog.csdn.net/fighting_sxw/article/details/80566295(友情提示:注意測試時新版本與舊版本的書寫方式)
2、在使用hibernate創建數據表需要四個主要文件:實體類,實體類對應的映射文件(后綴名為hbm.xml結尾),hibernate的配置文件(以cfg.xml結尾),以及測試類
3、對應的主要內容如下:
實體類:
package com.hibernate.entity;
import java.util.Date;
public class Students {
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 + '\'' +
'}';
}
}
實體類對應的hbm.xml文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.hibernate.entity.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 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/rushi</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">Sys935269</property>
<!-- DB schema will be updated if needed -->
<!-- <property name="hbm2ddl.auto">update</property> -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!--自動生成-->
<property name="hbm2ddl.auto">create</property>
<!--數據庫方言-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL55Dialect</property>
<!--加載資源-->
<mapping resource="com/hibernate/entity/Students.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4、這里對測試類重點介紹下,測試的時候新版本與舊版本可能會有所不同,按照教程可能永遠都找不出問題所在(參考資料:https://blog.csdn.net/bingjianit/article/details/68954250)
package com.hibernate.entity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import java.util.Date;
public class Test {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init() {
//創建配置對象
//Configuration configure = new Configuration().configure();
//創建服務注冊對象
//ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
////以上為舊版本的寫法,若是不注意,就會報org.hibernate.MappingException: Unknown entity: com.hibernate.entity.Student這個錯誤
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
//創建會話工廠對象
SessionFactory sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
//會話對象
session = sessionFactory.openSession();
//開啟事務
transaction = session.beginTransaction();
}
@After
public void destory() {
//提交事務
transaction.commit();
//關閉會話
session.close();
//關閉會話工廠
sessionFactory.close();
}
@org.junit.Test
public void TestSaveStudents() {
//生成學生對象
Students s = new Students(2, "張三豐", "南", new Date(), "武當山");
session.save(s);
}
}
程序運行結束,可能提示運行失敗,但是數據已經存在表格里了,不確定的可以去數據里查看下

格言:從不熟悉到熟悉是一個過程,無需恐懼,
