1. 环境搭建
1.1 导包
首先去官网下载hibernate
然后解压
在eclipse中创建文本项目,在WEB-INF下的lib文件夹中导入刚刚解压的hibernate文件下的lib下的required文件夹下的所有jar包,这是环境必要的jar包
导入mysql数据库的驱动包
1.2 创建数据库,准备表,实体
我用的是MySQL数据库
CREATE TABLE `cst_customer` (
`cust_id` BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
`cust_name` VARCHAR(32) NOT NULL COMMENT '客户名称(公司名称)',
`cust_user_id` BIGINT(32) DEFAULT NULL COMMENT '负责人id',
`cust_create_id` BIGINT(32) DEFAULT NULL COMMENT '创建人id',
`cust_source` VARCHAR(32) DEFAULT NULL COMMENT '客户信息来源',
`cust_industry` VARCHAR(32) DEFAULT NULL COMMENT '客户所属行业',
`cust_level` VARCHAR(32) DEFAULT NULL COMMENT '客户级别',
`cust_linkman` VARCHAR(64) 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;
实体
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_linkman;
private String cust_phone;
private String cust_mobile;
private Long cust_id;
这里省略了get,set函数,实体的属性名最好与数据库中的表中的列名一致,方便插入
1.3 书写orm元数据(对象与表的映射配置文件)
导入约束
eclipse -> perferences -> XML -> XML Catalog ->add
添加 hibernate-configuration-3.0.dtd 约束文件,这个是主配置文件的约束
这个文件在下载的hibernate文件中 ,路径为
hibernate-release-5.0.7.Final\project\build\classes\production\hibernate-core\org\hibernate
注意
key type 选 URI
key中填写的是 hibernate-configuration-3.0.dtd 这个文件的中的东西,文件可以用记事本打开
同理添加 hibernate-mapping-3.0.dtd 约束 ,这是配置数据库中的表与实体映射的约束文件
添加好后如图即可
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">
<!-- 配置表与实体对象的关系 -->
<!-- package属性:填写一个包名,在元素内部凡是需要写完整类名的属性,可以直接写简单类名-->
<hibernate-mapping package="pers.c.domain">
<!-- class元素:配置实体与表的对应关系
name:完整类名
table:数据库表名-->
<class name="Customer" table="cst_customer">
<!-- id元素:配置主键映射的属性
name:填写主键对应属性名
column(可选):填写表中主键列名/默认使用属性名-->
<id name="cust_id" column="cust_id">
<generator class="native"></generator>
</id>
<!-- property属性:配置除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_linkman" column="cust_linkman"></property>
<property name="cust_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property>
</class>
</hibernate-mapping>
1.4 书写主配置文件
在 src 下创建 hibernate.cfg.xml 文件,文件名字是默认,大小写不能改变(后面api中会解释)
<?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>
<!--
#hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
方言
#hibernate.connection.driver_class com.mysql.jdbc.Driver
选择数据库的驱动
#hibernate.connection.url jdbc:mysql:///test
数据库的url
如果是远程的服务器,jdbc:mysql://ip:端口/数据库名
#hibernate.connection.username gavin
登录数据库的用户名
#hibernate.connection.password
登录数据的密码
-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate01</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123</property>
<!-- 数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!--
#hibernate.show_sql true
#hibernate.format_sql true
-->
<!-- 在控制台输出sql语句,并且格式化输出 -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!--
## auto schema export 自动导出表结构,自动建表
#hibernate.hbm2ddl.auto create-drop
#hibernate.hbm2ddl.auto create
#hibernate.hbm2ddl.auto update
#hibernate.hbm2ddl.auto validate
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 引入orm元数据 路径:src下的路径 -->
<mapping resource="pers/c/domain/Customer.hbr.xml"/>
</session-factory>
</hibernate-configuration>
这些配置信息在 hibernate-release-5.0.7.Final\project\etc 下的 hibernate.properties 文件中可以找到,打开文件后,ctrl+f查找 mysql ,show,hbr
2. 基本api介绍
2.1 代码测试
package pers.c.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import pers.c.domain.Customer;
//测试Hibernate
public class Demo {
@Test
public void fun1() {
/*
* Configuration 配置加载类,用于加载主配置,orm数据元配置 configure()方法,这个方法加载 src下的
* hibernate.cfg.xml主配置文件 hibernate.cfg.xml文件中已经配置了 加载orm元数据
*/
Configuration conf = new Configuration().configure();
/*
* 根据配置信息创建SessionFactory 用于创建操作数据库核心对象session对象的工厂
* 负责保存和使用所有配置信息,消耗内存资源非常大,属于线程安全对象设计
*/
SessionFactory sessionFactory = conf.buildSessionFactory();
/*
* 获得session对象
* session是框架与数据库之间的连接,
* 是核心操作对象
*/
Session session = sessionFactory.openSession();
//开启事务并获得操作事务的对象
Transaction tx = session.beginTransaction();
Customer c = new Customer();
c.setCust_name("tom");
// 执行保存
session.save(c);
//提交事务
tx.commit();
//释放资源
session.close();
sessionFactory.close();
}
}
运行结果