Hibernate 的基本使用

数据表定义如下:

1
2
3
4
5
create table employee(
employee_id integer,
name text,
salary numeric,
);

如下图,建立 Maven 项目工程

resources 资源文件夹下创建 hibernate.cfg.xml 的 Hibernate 配置文件

创建 Employee.hbm.xml 的 Hibernate 映射文件

创建 POJO 类 Employee 为 Employee.hbm.xml 的映射类

image-20211024155344788

添加 Maven 依赖如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<dependencies>
<!-- hibernate-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-agroal</artifactId>
<version>5.5.8.Final</version>
<type>pom</type>
</dependency>

<!-- postgresql数据库驱动-->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.24.jre7</version>
</dependency>
</dependencies>

hibernate.cfg.xml 配置文件如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--数据库驱动(这里以PostgreSQL为例)-->
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL95Dialect</property>
<!-- 数据库配置-->
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/emp_database</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">postgres</property>
<property name="hibernate.show_sql">true</property>
<!-- 数据库映射文件-->
<mapping resource="cn/zzq/hibernate/demo/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Employee.hbm.xml 映射文件如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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 package="cn.zzq.hibernate.demo">
<class name="cn.zzq.hibernate.demo.Employee" table="public.employee" catalog="web">
<id name="employeeId" column="employee_id">
<generator class="assigned"/>
</id>

<property name="name" column="name" length="128"/>
<property name="salary" column="salary" length="128"/>
</class>
</hibernate-mapping>

employee 表对应的 POJO 类定义如下

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
package cn.zzq.hibernate.demo;
public class Employee {

private int employeeId;
private String name;
private float salary;


public Employee(int employeeId, String name, float salary) {
this.employeeId = employeeId;
this.name = name;
this.salary = salary;
}

public Employee() {

}

public int getEmployeeId() {
return employeeId;
}

public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public float getSalary() {
return salary;
}

public void setSalary(float salary) {
this.salary = salary;
}

@Override
public String toString() {
return "Employee{" +
"employeeId=" + employeeId +
", name='" + name + '\'' +
", salary=" + salary +
'}';
}
}

主测试类 Hibernate.java 定义如下

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
package cn.zzq.hibernate.demo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

public class Hibernate {
public static void main(String[] args) {
try {
// 配置Hibernate
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");

// 创建session(与数据库连接的会话)
StandardServiceRegistry standardServiceRegistry = new StandardServiceRegistryBuilder().configure().build();
SessionFactory sessionFactory = configuration.buildSessionFactory(standardServiceRegistry);
Session session = sessionFactory.openSession();

// 开启事务
Transaction transaction = session.beginTransaction();

// 保存到数据库
session.save(new Employee(1,"Tom12",4000f));
session.save(new Employee(2,"Tom23",7000f));
session.save(new Employee(3,"Tom23",8000f));

// HQL查询
Query q = session.createQuery("from Employee employee WHERE employee.salary>5000.0");

System.out.println(q.list());

// 提交事务
transaction.commit();

// 关闭session
session.close();
sessionFactory.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

image-20211024170150772

输出结果如上图所示