什么是Hibernate?
(我这里解释的可能不是很具体,大家可以去百度百科查看)
Hibernate是Java持久层框架之一,它是一个开放源码的ORM框架,它对JDBC进行了轻量级的对象封装,使得Java开放人员可以使用面向对象的编程思想来操作数据库。
总的来说,Hibernate就是一个持久层的ORM的框架,很大程度简化的Dao层的操作。
ORM:Object Relational Mapping 对象关系映射
图有点丑,,emmmm,废话不多说,去简单搭建一个。
Hibernate的搭建
1,下载Hibernate5,大家可以去官网下载。这里就不再过多赘述。
http://hibernate.org/
2,下载完之后,打开eclipse,新建一个web工程。
3,导入jar包。
在你下载完hibernate并解压之后,找到…/lib/required这个文件夹,进去之后就是该工程的一些必需包, 将其导入到工程中的lib下面。
4,在数据库中建测试表。
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;
5,创建实体类
public class 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 '移动电话',*/
private Long cust_id;
private String cust_name;
private Long cust_user_id;
private Long cust_create_id;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_linkman;
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 Long getCust_user_id() {
return cust_user_id;
}
public void setCust_user_id(Long cust_user_id) {
this.cust_user_id = cust_user_id;
}
public Long getCust_create_id() {
return cust_create_id;
}
public void setCust_create_id(Long cust_create_id) {
this.cust_create_id = cust_create_id;
}
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_linkman() {
return cust_linkman;
}
public void setCust_linkman(String cust_linkman) {
this.cust_linkman = cust_linkman;
}
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;
}
}
6,创建映射文件
实体类Customer目前还不具备持久化操作的能力,而Hibernate需要知道实体类Customer映射到数据库中的哪个表,以及类中的哪个属性对应数据库表中的哪个字段,这些都将在映射文件中配置。
所以在Customer所在的包中,创建一个名为Customer.hbm.xml的映射文件,在该文件中定义了实体类Customer的属性是如何映射到cst_customer表的列上的。
<?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.langsin.domain.Customer" table="cst_customer">
<id name="cust_id" column="cust_id">
<generator class="native"/>
</id>
<property name="cust_name" column="cust_name"></property>
<property name="cust_user_id" column="cust_user_id"></property>
<property name="cust_create_id" column="cust_create_id"></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>
对,在写映射文件前要在项目中导入约束。(这里导入需要dtd约束文件)Window/Preferences中搜cata,找到XML Catalog,点击add,添加。
这里导入时Key type要选择URI,下面的key就是你的约束文件里面的
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
第三行这句网址,这些都是一些小细节的东西,不会的可以去网上查找导入xml约束。
7,创建Hibernate核心配置文件。
映射文件反映了持久化类和数据库表的映射信息,而Hibernate的配置文件则主要用来配置数据库连接以及Hibernate运行时所需要的各个属性的值。在项目中的src下创建一个名称为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.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_day01?useSSL=false&serverTimezone=UTC</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!--在控制台打印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>
<!--加载映射-->
<mapping resource="com/langsin/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
8,书写测试类。
public class Demo1 {
@Test
public void fun1(){
//1,加载配置文件
Configuration cfg = new Configuration();
cfg.configure();
//2,创建SessionFactory;
SessionFactory sessionFactory = cfg.buildSessionFactory();
//3,创建Session对象
Session session = sessionFactory.openSession();
//4,开启事务
Transaction tx = session.beginTransaction();
//5,执行操作
Customer customer = new Customer();
customer.setCust_name("张三");
session.save(customer);
//6,事务提交,关闭
tx.commit();
session.close();
}
}
到数据库查看,成功。
这里在测试类中,有一些类,Configuration,SessionFactory,session,这里具体就不在讲解,可以去看Hibernate的api,这几个类还是比较重要的,可能之后我会再写,ok,溜。