Hibernate入门
01Hibernate概述
Hibernate是一个持久层的ORM(Object Relational Manpping,对象关系映射ORM建立对象和表的映射关系)框架,用来和数据库打交道。下面是经典三层结构:Web层,业务层,持久层。
02入门案例
- 创建一个数据库,建立一个表,建表语句如下,这里用的是SQLyog软件:
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;
- 建立一个Java工程,加入相关的包,主要有:数据库驱动包,Hibenate开发的必须包,Hibernate引入日志记录包,新建一个类Customer
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;
/*
eclipse sorce生成相应的Get和Set方法
*/
}
- 创建映射
映射需要通过xml的配置文件来完成,这个配置文件的名称可以任意命名,命名规范:类名.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标签 建立类与表的映射 -->
<class name="com.itheima.hibernate.demo01.Customer" table="cst_customer">
<!-- id标签 建立类中的属性与表中的主键的对应关系 -->
<id name="cust_id" column="cust_id">
<!-- 主键的生成策略 -->
<generator class="native"></generator>
</id>
<!-- 建立类中的普通属性和表的字段的对应 -->
<property name="cust_name" column="cust_name" length="32" />
<property name="cust_source" column="cust_source" length="32"/>
<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核心配置文件的名称:hibernate.cfg.xml(src目录下)
<?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>
<!-- 打印sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化sql语句 -->
<property name="hibernate.format_sql"></property>
<!-- 自动创建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/itheima/hibernate/demo01/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
- 创建测试类,保存一个用户到数据库,这里运行JUnit测试单元
public class HibernateDemo01 {
@Test
public void demo01(){
//1.加载Hibernate的核心配置文件
Configuration configuration = new Configuration().configure();
//2.创建SessFactory对象
SessionFactory sessionFactory = configuration.buildSessionFactory();
//3.通过SessionFactory获取到Session对象
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();
}
}
03Hibernate工具类
package com.itheima.hibernate.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
public static final Configuration cfg;
public static final SessionFactory sf;
static{
cfg = new Configuration().configure();
sf = cfg.buildSessionFactory();
}
public static Session openSession(){
return sf.openSession();
}
}
原有的Demo得到了简化:
public class HibernateDemo01 {
@Test
public void demo01(){
Session sess = HibernateUtils.openSession();
Transaction tx = sess.beginTransaction();
//代码区
Customer customer = new Customer();
customer.setCust_name("秦二世");
sess.save(customer);
tx.commit();
sess.close();
}