Hibernate入门:
- Hibernate的环境搭建、
- Hibernate的API、
- Hibernate的CRUD
1.框架的基本概述
- 什么是框架?
指的是软件的半成品,已经完成了部分功能 - EE的经典三层结构
2.Hibernate基本概述
- 什么是Hibernate?
开源的对象关系映射框架,对JDBC进行了轻量级的封装,
是一个持久层的ORM(Object Relational Mapping 对象关系映射)框架 - 什么是ORM?
Object Relational Mapping 对象关系映射,指的是将一个Java中的对象与关系型数据库中的表建立一种映射关系,从而操作对象就可以操作数据库中的表。
- 为什么学习Hibernate?
3.Hibernate的入门
- 下载Hibernate的开发环境
https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/
Hibernate5.x - 解压Hibernate
- 创建一个项目,引入jar包
java或web项目都可以(以java工程为例,需要添加路径)
添加路径:
- 创建表
在MySQL数据库创建数据库hibernate及表cst_customer
create database hibernate;
use hibernate;
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;
- 创建实体类Customer
package com.by.demo;
/*
* 客户管理的实体类
* `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`)
*/
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;
//alt+shift+r
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_name=" + cust_name
+ ", cust_source=" + cust_source + ", cust_industry="
+ cust_industry + ", cust_level=" + cust_level
+ ", cust_phone=" + cust_phone + ", cust_mobile=" + cust_mobile
+ "]";
}
}
- 创建映射(*****),
映射需要通过XML的配置文件来完成,把一个PO与一个数据表映射起来,这个配置文件命名规范(类名.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.by.demo.Customer" table="cst_customer">
<!-- 建立类中的属性与 表中的主键对应 -->
<id name="cust_id" column="cust_id">
<generator class="native"></generator>
</id>
<!-- 建立类中普通属性 和表中其他字段对应-->
<property name="cust_name" column="cust_name"></property>
<property name="cust_source" column="cust_source"></property>
<property name="cust_industry" column="cust_industry"></property>
<property name="cust_level" column="cust_level"></property>
<property name="cust_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property>
</class>
</hibernate-mapping>
- 创建一个Hibernate的核心配置文件(*****)
Hibernate的核心配置文件的名称:hibernate.cfg.xml
<?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>
<!-- 连接数据库的基本参数 =======-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</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>
<!-- 引入映射文件========== -->
<mapping resource="com/by/demo/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
- 编写测试代码(*****)
Hibernate的映射的配置
映射的配置
【class标签的配置】标签用来建立类与表的映射关系
属性:
name :类的全路径
table :表名(类名与表名一致,table可以省略)
catalog :数据库名
<!-- 建立类与表的映射 -->
<class name="com.by.demo.Customer" table="cst_customer">
</class>
【id标签的配置】标签用来建立类中的属性与表中的主键的对应关系
<!-- 建立类中的属性与 表中的主键对应 -->
<id name="cust_id" column="cust_id">
<generator class="native"></generator>
</id>
属性:
name :类中的属性名
column :表中的字段名(类中的属性名和表中的字段名如果一致,column可以省略)
length :长度
type :类型
【property标签的配置】
标签用来建立类中的普通属性与表的字段的对应关系
属性:
name :类中的属性名
column :表中的字段名
length :长度
type :类型
not-null :设置非空
unique :设置唯一
Hibernate的核心的配置
方式一:属性文件的方式
hibernate.properties
hibernate.connection.driver_class=com.mysql.jdbc.Driver
…
hibernate.show_sql=true
属性文件的方式不能引入映射文件(手动编写代码加载映射文件)
方式二:XML文件的方式
:XML文件的方式
hibernate.cfg.xml
核心的配置
必须的配置
连接数据库的基本的参数
驱动类
url路径
用户名
密码
方言
可选的配置
显示SQL :hibernate.show_sql
格式化SQL :hibernate.format_sql
自动建表 :hibernate.hbm2ddl.auto
none :不使用hibernate的自动建表
create :如果数据库中已经有表,删除原有表,重新创建,如果没有表,新建表。(测试)
create-drop :如果数据库中已经有表,删除原有表,执行操作,删除这个表。如果没有表,新建一个,使用完了删除该表。(测试)
update :如果数据库中有表,使用原有表,如果没有表,创建新表(更新表结构)
validate :如果没有表,不会创建表。只会使用数据库中原有的表。(校验映射和表结构)。
映射文件的引入
引入映射文件的位置
```xml
<?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>
<!-- 连接数据库的基本参数 =======-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</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>
<!-- 引入映射文件========== -->
<mapping resource="com/by/demo/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>