1、通過 new project 創建一個 Hibernate 項目,點擊 next 後命名項目名稱,之後點擊 finish :
【如果勾選 “Create default hibernate configuration and main class”, 後續不用再手動創建 hibernate.cfg.xml配置文件】
2、創建如圖所示的一個數據庫 hibernate 以及表 user:
3、IDEA 裏連接數據庫:
自定義 Name ,輸入 mysql 的用戶以及密碼,然後輸入要連接的數據庫 Database,之後點擊 Test Connection,如圖所示:
【如果顯示 Server returns invalid timezone 報錯,則是因爲 mysql 時區默認是 UTC 時區,需要修改:在 mysql 命令模式下,輸入 set global time_zone='+8:00';
即可】
4、添加 jdbc 的 jar 包,右鍵點擊 jar 包,然後點擊 Add as Library:
5、在 src 下新建目錄 com.example,另外,在項目名稱下新建目錄 test,並將 test 目錄設爲 Test Sources Root ,如圖:
6、創建 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">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- DB schema will be updated if needed -->
<!-- <property name="hibernate.hbm2ddl.auto">update</property> -->
<!-- 配置數據庫方言-->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 執行操作時是否在控制檯打印 SQL-->
<property name="show_sql">true</property>
<!-- 是否對 SQL 進行格式化-->
<property name="format_sql">true</property>
<!-- 指定自動生成數據表的策略-->
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
7、根據數據庫中的user表,生成UserEntity類:
點擊側邊欄 Persistence ,選中數據庫樣式的 hibernate.cfg.xml 文件,右鍵 Generate Persistence Mapping —> By Database Schema:
注:將生成的 UserEntity.hbm.xml 文件放到與 UserEntity 同一個目錄下
UserEntity.java:
package com.example;
import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name = "user", schema = "hibernate", catalog = "")
public class UserEntity {
private int uid;
private String username;
private String password;
@Id
@Column(name = "uid", nullable = false)
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
@Basic
@Column(name = "username", nullable = true, length = 12)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Basic
@Column(name = "password", nullable = true, length = 8)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserEntity that = (UserEntity) o;
return uid == that.uid &&
Objects.equals(username, that.username) &&
Objects.equals(password, that.password);
}
@Override
public int hashCode() {
return Objects.hash(uid, username, password);
}
}
UserEntity,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.example.UserEntity" table="user" schema="hibernate">
<id name="uid">
<column name="uid" sql-type="int(4)"/>
</id>
<property name="username">
<column name="username" sql-type="varchar(12)" length="12" not-null="true"/>
</property>
<property name="password">
<column name="password" sql-type="varchar(8)" length="8" not-null="true"/>
</property>
</class>
</hibernate-mapping>
8、在 test 目錄下新建一個測試類 UserTest:
import com.example.UserEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class UserTest {
public static void main(String[] args) {
Configuration configuration=new Configuration();
configuration.configure("hibernate.cfg.xml");
SessionFactory sessionFactory=configuration.buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction transaction=session.beginTransaction();
UserEntity userEntity=new UserEntity();
// userEntity.setUid(1);//此處設不設id都行
userEntity.setUsername("ximu");
userEntity.setPassword("123456");
session.save(userEntity);
transaction.commit();
session.close();
System.out.println("Successful saved.");
}
}
9、運行 UserTest.java 文件:
如果出以下 bug——
可能是因爲 hibernate.cfg.xml 文件中的內容已經不知不覺中發生了改變——
- com.mysql.jdbc.Driver 竟變成了 com.mysql.cj.jdbc.Driver
- 下面兩行連接數據庫的用戶名、以及密碼的代碼也消失了
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
解決方式自然是恢復成原來的樣子即可!
10、IDEA運行結果,以及 mysql 數據庫表中的更新:
♥ 喜 歡 請 點 贊 喲 ♥ |
(●ˇ∀ˇ●) |