Hibernate入门详细教程
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;}@Overridepublic 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.org/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.org/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 {@Testpublic 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中的ConnectionSession 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().commit(); // 开启事务
- 关闭session;
- 关闭sessionFactory;
最后附上大佬链接
Hibernate入门详细教程相关推荐
- spring入门详细教程(五)
前言 本篇紧接着spring入门详细教程(三),建议阅读本篇前,先阅读第一篇,第二篇以及第三篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/ ...
- Spring入门详细教程(四)
前言 本篇紧接着spring入门详细教程(三),建议阅读本篇前,先阅读第一篇,第二篇以及第三篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/ ...
- Spring入门详细教程(三)
前言 本篇紧接着spring入门详细教程(二),建议阅读本篇前,先阅读第一篇和第二篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/p/101 ...
- Spring入门详细教程(二)
前言 本篇紧接着spring入门详细教程(一),建议阅读本篇前,先阅读第一篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/p/1016553 ...
- ThinkJS框架入门详细教程(二)新手入门项目
一.准备工作 参考前一篇:ThinkJS框架入门详细教程(一)开发环境 安装thinkJS命令 npm install -g think-cli 监测是否安装成功 thinkjs -v 二.创建项目 ...
- NMAP入门详细教程
NAMP入门详细教程 一.功能: 网络扫描和嗅探. 二.原理: 使用TCP/IP协议栈指纹准确地判断目标主机的相关信息. 三.作用: 识别活跃主机 识别开放端口以及相关的服务 识别主机的系统指纹 路由 ...
- Pandas入门详细教程
作者:luanhz 来源:小数志 导读 本文主要是对pandas进行入门详细介绍,通过本文你将系统性了解pandas为何会有数据分析界"瑞士军刀"的盛誉. 行文二级目录 01 关于 ...
- numpy入门详细教程(一)
本讲主要介绍对numpy库和numpy库的N维数组对象:ndarray的基本了解.更多内容请看numpy入门详细教程(二) numpy: NumPy是一个开源的Python科学计算基础库,包含: • ...
- python数据科学系列:pandas入门详细教程
导读 前2篇分别系统性介绍了numpy和matplotlib的入门基本知识,今天本文自然是要对pandas进行入门详细介绍,通过本文你将系统性了解pandas为何会有数据分析界"瑞士军刀&q ...
- springboot 入门详细教程 源码
转载知乎文章 gitee官方教程+开源项目 Gitee 已认证的官方帐号 1,303 人赞同了该回答 推荐以 Spring Boot 教程与 Spring Cloud 教程的详细开源项目 &quo ...
最新文章
- 月薪30K+的程序员都会啥,通过3000字告诉你……
- Mat 显示到MFC中Pictrue Control的问题
- (17)10-10-12分页
- 毕业一两年,怎样快速成长和晋升?
- 分数线划定(洛谷-P1068)
- Gin实践 番外 Golang交叉编译
- 巧用推荐墙入口,APP轻松盈利
- Android 网络开发框架的选择
- 数据库oracle有哪些函数,Oracle函数大全
- 「三分钟系列05」3分钟看懂并发与并行
- Windows API函数大全(Windows编程参考手册)
- 1的阶乘加到20的阶乘
- 2018麦考林杂志计算机科学,加拿大大学2020年最新的麦考林杂志排名
- python 自动玩 连连看 外挂
- [USACO Hol10] 政党
- 主分区和逻辑分区的区别
- python基础知识Chapter9
- AndroidStudio多渠道打包
- java中设置基偶隔行换色_隔行变色 - 李俊互联网技术传播者 - 博客园
- python爬取淘宝数据魔方_《淘宝数据魔方技术架构解析》阅读笔记