
本篇将介绍 Hibernate 是如何实现对数据库的增删改查CRUD操作的,Hibernate 增删改查主要分为如下6个步骤。
1.创建Hibernate工程
Hibernate整体工程结构,如下图所示:

下面主要会涉及建表、以及生成User实体类,以及配置文件,以及增删改查等方法,下面一一详解。
2.创建User表
CREATE DATABASE hibernate; USE hibernate; CREATE TABLE `user` ( `id` int(32) PRIMARY KEY AUTO_INCREMENT, `name` varchar(20), )
3.User实体类
生成User实体类,如下所示:
package com.mikechen.domain;
public class User {
private Integer id; // 唯一标识id
private String name; // 姓名
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4.hibernate配置文件
hibernate.cfg.xml主要用于:配置数据库连接、事务管理、Hibernate 本身的配置信息。
在 src 目录下创建一个名称为 hibernate.cfg.xml 的文件,详细的配置如下所示:
<?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>
<!-- 指定方言 -->
<property name="dialect">
org.hibernate.dialect.MySQL5Dialect
</property>
<!-- 链接数据库url -->
<property name="connection.url">
<![CDATA[jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=utf-8]]>
</property>
<!-- 连接数据库的用户名 -->
<property name="connection.username">
root
</property>
<!-- 数据库的密码 -->
<property name="connection.password">
123456
</property>
<!-- 数据库驱动 -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- 显示sql语句 -->
<property name="show_sql">
true
</property>
<!-- 格式化sql语句 -->
<property name="format_sql">true</property>
<!-- 映射文件配置 -->
<mapping resource="com/mikechen/domain/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
5.user.hbm.xml
建立刚才新建的User实体类与表的对应关系,如下所示:
<?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> <!-- name代表的是类名,table代表的是表名 --> <class name="com.mikechen.domain.User" table="user"> <!-- name代表的是User类中的id属性,column代表的是user表中的主键id --> <id name="id" column="id"> <!-- 主键生成策略 --> <generator class="native" /> </id> <!-- 其他属性使用property标签映射 --> <property name="name" column="name" type="java.lang.String" /> </class> </hibernate-mapping>
6.增删改查测试
前期配置完成后,现在开始通过hibernate增删改查。
1)增加数据
@Test
public void testInsert() {
// 1.创建Configuration对象并加载hibernate.cfg.xml配置文件
Configuration config = new Configuration().configure();
// 2.获取SessionFactory
SessionFactory sessionFactory = config.buildSessionFactory();
// 3.得到一个Session
Session session = sessionFactory.openSession();
// 4.开启事务
Transaction transaction = session.beginTransaction();
// 5.执行持久化操作
User user = new User();
user.setName("mikechen");
// 将对象保存到表中
session.save(user);
// 6.提交事务
transaction.commit();
// 7.关闭资源
session.close();
sessionFactory.close();
}
2)查询数据
@Test
public void queryTest() {
// 1.创建Configuration对象并加载hibernate.cfg.xml配置文件
Configuration config = new Configuration().configure();
// 2.获取SessionFactory
SessionFactory sessionFactory = config.buildSessionFactory();
// 3.得到一个Session
Session session = sessionFactory.openSession();
// 4.开启事务
Transaction transaction = session.beginTransaction();
// 5.执行持久化操作
User user = (User) session.get(User.class, 1);
System.out.println(user.getId() + "" + user.getName() );
// 6.提交事务
transaction.commit();
// 7.关闭资源
session.close();
sessionFactory.close();
}
3)修改数据
@Test
public void testUpdate() {
// 1.创建Configuration对象并加载hibernate.cfg.xml配置文件
Configuration config = new Configuration().configure();
// 2.获取SessionFactory
SessionFactory sessionFactory = config.buildSessionFactory();
// 3.得到一个Session
Session session = sessionFactory.openSession();
// 4.开启事务
Transaction transaction = session.beginTransaction();
// 5.执行持久化操作
User user = new User();
user.setName("mikechen");
// 更新数据
session.update(user);
// 6.提交事务
transaction.commit();
// 7.关闭资源
session.close();
sessionFactory.close();
}
4)删除数据
@Test
public void deleteByIdTest() {
// 1.创建Configuration对象并加载hibernate.cfg.xml配置文件
Configuration config = new Configuration().configure();
// 2.获取SessionFactory
SessionFactory sessionFactory = config.buildSessionFactory();
// 3.得到一个Session
Session session = sessionFactory.openSession();
// 4.开启事务
Transaction transaction = session.beginTransaction();
// 5.执行持久化操作
User user = (User) session.get(User.class, 1);
session.delete(user);
// 6.提交事务
transaction.commit();
// 7.关闭资源
session.close();
sessionFactory.close();
}
关于mikechen
mikechen睿哥,10年+大厂架构经验,资深技术专家,就职于阿里巴巴、淘宝、百度等一线互联网大厂。