项目环境

  • JDK 1.8
  • maven 3.6
  • Hibernate 5.5.0
  • MySql 5.7

创建一个maven项目

使用idea创建一个普通的maven项目就可以了。

添加相关依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>HibernateTest</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<!-- hibernate5.5.0依赖 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.5.0.Final</version>
</dependency>

<!-- mysql依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- 单元测试依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>

<build>
<!-- 读取配置文件 -->
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

</project>

编写hibernate.cfg.xml配置文件

在resource文件夹下创建配置文件,写入以下信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<!-- 显示sql语句 -->
<property name="show_sql">true</property>
<!-- 基础JDBC连接配置 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///hibernatetest</property>
<property name="connection.username">root</property>
<property name="connection.password">Good18882336322</property>
<!-- 数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQL57Dialect</property>

<!-- 对象配置文件 -->
<mapping resource="com/fizzyelf/entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>

创建一个实体类

这里创建一个User类做为演视

1
2
3
4
5
6
7
8
9
10
package com.fizzyelf.entity;

import lombok.Data;

@Data
public class User {
private Integer id;
private String userName;
private String password;
}

创建类的映射配置文件

在类的包下在创建一个映射文件User.hbm.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0"?>
<!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.fizzyelf.entity.User" table="user" lazy="false">
<id name="id" column="id">
<!-- 主键自增 -->
<generator class="native"/>
</id>
<property name="userName" column="user_name"/>
<property name="password" column="password"/>
</class>

</hibernate-mapping>

在数据库创建一个user表

image-20210610163318153

测试代码

这里的代码我是从官方文档复制过来的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.fizzyelf;

import com.fizzyelf.entity.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.junit.Test;


public class HibernateTest {
@Test
public void Test(){
// A SessionFactory is set up once for an application!
// 一个SessionFactory为一个应用程序设置一次!

// 初始化服务注册对象
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure() // 加载配置文件hibernate.cfg.xml
.build();
SessionFactory sessionFactory = null;
try {
// 获取SessionFactory
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
// 从SessionFactory创建Session连接
Session session = sessionFactory.openSession();
session.beginTransaction();
User user = new User();
user.setUserName("FizzyElf");
user.setPassword("123");
session.save(user);
// 提交事务
session.getTransaction().commit();
session.close();
}
catch (Exception e) {
// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
// so destroy it manually.
System.out.println(e.getMessage());
StandardServiceRegistryBuilder.destroy( registry );
}

}
}

运行测试代码

image-20210610163517981

我们可以看见数据库中新增了一条数据

image-20210610163555157