1. 引入jar包
- 数据库驱动包
【注意:数据库驱动包与hibernate版本需保持一致,我一开始用的mysql8.0+,就会报错,很烦人的呀!!】 - Hibernate开发必须的jar包
- Hibernate日志记录包
2. 创建数据库表
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;
3. 创建实体类
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;
/**
*get和set方法省略
*/
}
4. 创建实体类与数据库表的映射【配置文件】【重点】
可任意命名,但尽量遵守统一命名规范:类名.hbm.xml
该文件存放的位置是任意的
- class标签的配置
- 标签用来建立类与表的映射关系
- 属性:
a. name:类的全路径
b. table:表名【类名与表名一致,table可以省略】
c. catalog:数据库名【可不写】
- id标签的配置
- 标签用来建立类中的属性与表中的主键的对应关系
- 属性:
a. name:类中的属性名
b. column:表中的字段名【类中的属性名和表中的字段名如果一致,column可以省略】
c. length:长度【hibernate可自动创建表,String类型默认长度255】
d. type:类型【3种:java、hibernate、SQL】
- id标签的配置
- 标签用来建立类中的属性与表中的主键的对应关系
- 属性:
a. name:类中的属性名
b. column:表中的字段名
c. length:长度
d. type:类型
e. not-null:设置非空
f. unique:设置唯一
<?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.demo1.Customer" table="cst_customer">
<!-- 建立类中的属性与表中的主键对应 -->
<id name="cust_id" column="cust_id" >
<generator class="native"/>
</id>
<!-- 建立类中的普通属性和表的字段相对应 -->
<!-- name里面是java类中的属性,column里是数据库表里的字段值 -->
<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>
5. 创建Hibernate核心配置文件【重点】
配置文件的名称:hibernate.cfg.xml
文件的路径是固定放在src下
- 必须的配置
- 连接数据库的基本参数
a. 驱动类
b. URL路径
c. 用户名
d. 密码 - 方言
- 可选的配置
- 显示SQL:
hibernate.show_sql
- 格式化SQL:
hibernate.format_sql
- 自动建表:
hibernate.hbm2ddl.auto
a. none:不适用hibernate的自动创建
b. create:如果数据库中已经有表,删除原有表,重新创建;如果没有表,新建表。【测试用】
c.create-drop:如果数据库中已经有表,删除原有表,执行操作,删除这个表;如果没有表,新建一个,使用完了删除该表。【测试用】
d.update
:如果数据库中有表,使用原有表;如果没有表,创建新表(更新表结构)。
e.validate
:如果没有表,不回创建表;只会使用数据库中原有的表(校验映射和表结构)。
- 映射文件的引入
- 引入映射文件
<?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:///hibernate_day01</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>
</session-factory>
<mapping resource="com/hibernate/demo1/Customer.hbm.xml">
</hibernate-configuration>
还可以添加其他可选配置信息:
<!-- 打印SQL -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化SQL -->
<property name="hibernate.format_sql">true</property>
<!-- 自动创建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
6. 编写测试代码
package com.hibernate.demo1;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
/**
* Hibernate入门案例
* @author Administrator
*
*/
public class HibernateDemo1 {
@Test
//保存用户的案例
public void demo1() {
//1.加载Hibernate的核心配置文件
Configuration configuration = new Configuration().configure();
//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("zhangsna");
session.save(customer);
//6.事务提交
transaction.commit();
//7.资源释放
session.close();
}
}
执行上述代码:将“zhangsan”添加到数据库表中