Hibernate入门详细教程
1、下载Hibernate
Hibernate官网链接
本文以 hibernate-release-5.3.16.Final 为例
(1)下载好解压后如下图所示:
(2)打开 lib
(3)打开 required
2、编写第一个程序
(1)开发工具IDEA
(2)创建项目Hibernate,导入 jar 包
- 导入上面 required 中的所有 jar 包
- 导入 Mysql 驱动包 mysql-connector-java-8.0.19.jar
- (说明:Mysql版本为8,加载驱动时写:com.mysql.cj.jdbc.Driver。 Mysql版本为5写:com.mysql.jdbc.Driver)
(3)编写数据库文件
create database hibernate_demo01;
use hibernate_demo01;
CREATE TABLE `cst_customer` (
`cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
`cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
`cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
`cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
`cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
`cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
`cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
(4)在src下创建包com.hibernate.demo01,在包下创建一个实体类——Customer.java,如下:
package com.hibernate.demo01;
public class Customer {
private Long cust_id;
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_phone;
private String cust_mobile;
public Long getCust_id() {
return cust_id;
}
public void setCust_id(Long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
}
@Override
public String toString() {
return "Customer{" +
"cust_id=" + cust_id +
", cust_source='" + cust_source + '\'' +
", cust_industry='" + cust_industry + '\'' +
", cust_level='" + cust_level + '\'' +
", cust_phone='" + cust_phone + '\'' +
", cust_mobile='" + cust_mobile + '\'' +
'}';
}
}
(5)创建Hibernate的相关配置文件
首先我们要编写Hibernate的相关配置文件,Hibernate的相关配置文件分为两种:
Xxx.hbm.xml:它主要是用于描述类与数据库中的表的映射关系;
hibernate.cfg.xml:它是Hibernate框架的核心配置文件。
创建映射配置文件
映射配置文件应与实体类在同一个包下,并且名称应是 类名.hbm.xml,所以我们要在com.hibernate.demo01包下创建一个Customer.hbm.xml文件,约束可以在Hibernate的核心jar包——hibernate-core-5.3.16.Final.jar的org.hibernate包下查找到hibernate-mapping-3.0.dtd文件,打开该文件,找到如下内容:
然后把图中选中部分复制粘贴到Customer.hbm.xml文件中即可,下面是Customer.hbm.xml中内容:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 建立类与表的映射-->
<class name="com.hibernate.demo01.Customer" table="cst_customer">
<!-- 建立类中的属性与表中的主键相对应 -->
<id name="cust_id" column="cust_id">
<!-- 主键的生成策略,现在使用的是本地生成策略 -->
<generator class="native"/>
</id>
<!-- 建立类中的普通属性和表中的字段相对应 -->
<property name="cust_name" column="cust_name"/>
<property name="cust_source" column="cust_source"/>
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/>
<property name="cust_phone" column="cust_phone"/>
<property name="cust_mobile" column="cust_mobile"/>
</class>
</hibernate-mapping>
创建核心配置文件
核心配置文件主要是Hibernate框架所使用的,它主要包含了连接数据库的相关信息和Hibernate的相关配置等。编写完的核心配置文件应在src目录下,并且名称应是hibernate.cfg.xml,所以我们要在src目录下创建一个hibernate.cfg.xml文件(一般这个hibernate.cfg.xml会自动创建好,可以直接用),但约束同样可以在Hibernate的核心jar包——hibernate-core-5.0.7.Final.jar的org.hibernate包下查找到hibernate-configuration-3.0.dtd文件,打开该文件,找到如下内容:
然后复制粘贴到hibernate.cfg.xml文件中即可。配置可以参考hibernate-core-5.3.16.Final\project\etc\hibernate.properties文件。先给出hibernate.cfg.xml文件的内容:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 下面是三个必须要有的配置 -->
<!-- 配置连接MySQL数据库的基本参数 -->
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_demo01?serverTimezone=UTC</property>
<!-- 数据库登录名-->
<property name="hibernate.connection.username">root</property>
<!-- 数据库登录密码-->
<property name="hibernate.connection.password">kjjdhd</property>
<!-- 配置Hibernate的方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 下面两个是可选的配置! -->
<!-- 打印sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化sql语句 -->
<property name="hibernate.format_sql">true</property>
<!-- 告诉Hibernate的核心配置文件加载哪个映射文件 -->
<mapping resource="com/hibernate/demo01/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Hibernate快速入门测试
在com.hibernate.demo01包下创建一个单元测试类——HibernateDemo1.java。
但先要导入一个包,在 D:\IntelliJ IDEA 2020.1.1\lib 中找到 junit-4.12.jar 包,导入
package com.hibernate.demo01;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class HibernateDemo1 {
@Test
public void demo1() {
//1. 加载Hibernate的核心配置文件
Configuration configuration = new Configuration().configure();
//如果在Hibernate的核心配置文件没有设置加载哪个映射文件,则可手动加载映射文件
//configuration.addResource("com/meimeixia/hibernate/demo01/Customer.hbm.xml");
//2. 创建SessionFactory对象,类似于JDBC中的连接池
SessionFactory sessionFactory = configuration.buildSessionFactory();
//3. 通过SessionFactory获取到Session对象,类似于JDBC中的Connection
Session session = sessionFactory.openSession();
//4. 手动开启事务,(最好是手动开启事务)
Transaction transaction = session.beginTransaction();
//5. 编写代码
Customer customer = new Customer();
customer.setCust_name("依依妖妖");
//保存一个用户
session.save(customer);
//6. 事务提交
transaction.commit();
//7. 释放资源
session.close();
sessionFactory.close();
}
}
运行测试类
Hibernate执行原理总结
- 通过Configuration().configure);读取并解析hibernate.cfg.xml配置文件。
- 由hibernate.cfg.xml中的
<mapping resource="com/xx/User.hbm.xml"/>
读取解析映射信息。 - 通过config.buildSessionFactory(); 得到 sessionFactory。
- sessionFactory.openSession; // 到session。
- sesion.beginTransaction(); // 开启事务。
- persistent operate;
- session.getTransaction()mit(); // 开启事务
- 关闭session;
- 关闭sessionFactory;
最后附上大佬链接
更多推荐
Hibernate入门详细教程
发布评论