Hibernate框架
前奏
三层架构
1.表现层(web层):接收和处理请求 MVC模型,struts框架都是表现层模型
2.业务层(service层):处理业务需求
3.持久层(dao层):对数据库操作
MVC模型
1.M:model(模型):作用封装数据,目前代表实体类
2.V:view(试图):作用展示数据,JSP/HTML
3.C:Controller(控制器):作用控制程序流程,Servlet/Filter
三大框架和三层架构之间关系
1.hibernate框架:持久层框架
2.struts框架:表现层框架
3.sprint框架:综合类框架
正题
持久层技术
JDBC:操作数据库最底层的方式
优势:底层,效率高
弊端:编写代码时比较繁琐,尤其时结果集
DBUtils:基于JDBC,对JDBC做简单的封装
优势:装结果集的操作变得简单。并且仍然是自己编写sql语句,相对效率并没有太大影响。
它用了池的思想管理连接。
弊端:要求实体类中属性名称和数据库表的字段名必须一致。
要我们记的东西也比较多
他们的共同点:都要我们自己写SQL语句。
目标:执行java代码===>>对数据库操作
怎么实现呢???
Hibernate框架
如何实现
实体类:
public class Product{
private Integer pid;
private String pname;
private Double price;
}
表结构:
create table product(
product_id int primary key auto_increment,
product_name varchar(50),
product_price double(7,2)
);
用JDBC或者DBUtils操作时,表和实体类的关系就是SQL语句。
insert into product(product_name,product_price)values(product.getPname(),product.getPrice());
那么改进的框架表和实体类如何建立对应关系呢?
使用配置文件:
xml:
properties:
使用哪个?
xml:因为xml可以描述层级关系
ORM(Object Relational Mapping)
对象关系映射:建立实体类和数据库表之间的关系.
实现操作实体类就相当于操作数据库
映射关系配置示例:
<class name="类名" table="表名">
<id name="实体类属性名称" column="表中的主键字段名">
<property name="属性名" column="表中的字段名"/>
</class>
Hibernate框架特性
轻量级,企业级,开源的ORM持久层框架.是可以操作数据库的框架
框架:是一个架构
轻量级:指依赖资源少(目前依赖:log4j,c3p0连接池)
企业级:在企业级应用中比较多
开源:开放源代码
ORM操作方式:对象关系映射,操作实体类相当于操作数据库
简单项目实例
一、需求分析及准备工作
需求分析
实现一个客户信息保存到数据库
准备工作
hibernate-mapping-3.0.dtd-------->映射文件需要导入的DTD从文件中拷贝
hibernate-configuration-3.0.dtd---------->配置文件需要导入的DTO从文件中拷贝
下载hibernate-release-5.0.7.Final源码,里面有所需要导入的包
二、设计数据库表
/*创建客户表*/
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_address` varchar(128) DEFAULT NULL COMMENT '客户联系地址',
`cust_phone` varchar(64) DEFAULT NULL COMMENT '客户联系电话',
PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=94 DEFAULT CHARSET=utf8;
三、实体类设计
package com.tfrunning.domain;
import java.io.Serializable;
/**
* @author Admin
*客户实体类
*/
public class Customer implements Serializable {
private Long custId;
private String custName;
private String custSource;
private String custIndustry;
private String custLevel;
private String custAddress;
private String custPhone;
public Long getCustId() {
return custId;
}
public void setCustId(Long custId) {
this.custId = custId;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCustSource() {
return custSource;
}
public void setCustSource(String custSource) {
this.custSource = custSource;
}
public String getCustIndustry() {
return custIndustry;
}
public void setCustIndustry(String custIndustry) {
this.custIndustry = custIndustry;
}
public String getCustLevel() {
return custLevel;
}
public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
}
public String getCustAddress() {
return custAddress;
}
public void setCustAddress(String custAddress) {
this.custAddress = custAddress;
}
public String getCustPhone() {
return custPhone;
}
public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
}
@Override
public String toString() {
return "Customer [custId=" + custId + ", custName=" + custName + ", custSource=" + custSource
+ ", custIndustry=" + custIndustry + ", custLevel=" + custLevel + ", custAddress=" + custAddress
+ ", custPhone=" + custPhone + "]";
}
}
四、数据库表和实体类映射文件
1.实体类包下新建映射文件Customer.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 实体类包下面创建 -->
<!-- 导入DTD,在bibernate-mapping-3.0.dtd文件中拷贝即可 -->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.tfrunning.domain">
<class name="Customer" table="cst_customer">
<id name="custId" column="cust_id">
<!-- generator指定主键的生成方式,取值是固定几个值之一
native:使用本地数据库自增长能力
-->
<generator class="native"></generator>
</id>
<property name="custName" column="cust_name"></property>
<property name="custSource" column="cust_source"></property>
<property name="custIndustry" column="cust_industry"></property>
<property name="custLevel" column="cust_level"></property>
<property name="custAddress" column="cust_address"></property>
<property name="custPhone" column="cust_phone"></property>
</class>
</hibernate-mapping>
五、配置文件
配置文件参考位置:源码:hibernate-release-5.0.7.Final/project/etc/hibernate properties
在src目录下新建hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 导入约束DTD -->
<!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(作用是创建Session对象)
Session对象:hibernate中操作数据库的核心对象
三部分内容:
一:连接数据库信息:可以在源码中参考(hibernate-release-5.0.7.Final\project\etc\hibernate.properties)mysql定位
二:hibernate可选配置
三:映射配置文件的位置
-->
<session-factory>
<!-- 一、连接数据库信息 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_table</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 二、hibernate可选配置 -->
<!-- 是否显示hibernate生成的SQL语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 是否使用格式化输出sql语句到控制台 -->
<property name="hibernate.format_sql">true</property>
<!-- 配置hibernate采用何种方式生成DDL语句 ,update:检测实体类映射配置和表结构是否一致,如果不一致更新表结构-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 三、映射配置文件的位置 -->
<mapping resource="com/tfrunning/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
六、测试
package test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.tfrunning.domain.Customer;
/*
* hibernate的入门案例
* 需求:实现保存一个客户到数据库
* */
public class HibernateDemo1 {
public static void main(String[] args) {
Customer cust = new Customer();
cust.setCustName("同方软银");
//1.解析主配置文件
Configuration cfg = new Configuration().configure();
//2.根据配置文件创建SessionoFactory
SessionFactory sf = cfg.buildSessionFactory();
//3.根据SessionFactory创建Session对象
Session session = sf.openSession();
//4.开启事务
Transaction ts = session.beginTransaction();
//5.执行操作(保存)
session.save(cust);
//6.提交事务
ts.commit();
//7.释放资源
session.close();
sf.close();
}
}