一、hibernate简介
1.什么是hibernate
- hibernate是一个开放源代码的对象关系映射框架,对JDBC进行了非常轻量级的对象封装,将POJO(plain ordinary Java object)与数据库表建立映射关系,是一个全自动的持久层的ORM框架。
- hibernate是一个基于JDBC的主流持久化框架,是一个优秀的ORM实现,很大程度上简化了DAO(data access object)层编码工作。
2.什么是ORM
- ORM(object relational mapping)对象关系映射,指的是将一个Java中的对象与关系型数据库中的表建立一种映射,从而操作对象就可以操作数据库中的表。
二、环境搭建
1.工程环境
2.hibernate映射配置
- class标签,用来建立类与表之间的关系name:类名,table:表名
- id标签,建立中的属性与表中的主键的对应关系
- property,建立类中的普通属性与表的字段的对应关系
3.hibernate的核心配置
- 必须的配置:连接数据库的基本参数:驱动类、URL路径、用户名、密码,方言的配置以及映射文件的引入
- 可选的配置显示化SQL语句、格式化SQL语句、自动建表
三、代码测试
1.代码目录结构
![1](https://img-blog.csdnimg.cn/20200602135543193.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1OTIwNzI5,size_16,color_FFFFFF,t_70)
2.持久化类的编写
package com.itheima.hibernate.demo1;
public class Customer {
private Long cust_id;
private String cust_name;
private String cust_source;
private String cuso_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 getCuso_industry() {
return cuso_industry;
}
public void setCuso_industry(String cuso_industry) {
this.cuso_industry = cuso_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;
}
}
3.hibernate映射文件的编写
<?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.itheima.hibernate.demo1.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="cuso_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>
4.hibernate核心配置文件的编写
<?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?useUnicode=true&characterEncoding=utf-8</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>
<!-- 自动创建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 映射文件的引入 -->
<mapping resource="com/itheima/hibernate/demo1/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>