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 数据库表中的更新:
♥ 喜 欢 请 点 赞 哟 ♥ |
(●ˇ∀ˇ●) |