Hibernate持久层框架知识总结

 文章一共有四节,供我回忆hibernate的用法和操作细则

第一章:

一、hibernate简介

HIbernate是一套主流的持久层框架,能够大大简化代码量,提高工作效率

HIbernate是一个开源的ORM(Object Relation Mapping对象关系映射)框架,对JDBC进行了轻量级的封装,开发人员可以使用面向对象的编程思想来操作数据库。它将POJO

与数据库简历映射关系(POJO:Plain Ordinary Java Object简单的Java对象,实际就是普通JavaBeans,和JavaBean也有区别,是为了避免和EJB混淆所创造的简称),全自

动的ORM框架,可以自动生成SQL语句。

与其它操作数据库的技术相比,Hibernate具有以下几点优势:

1、Hibernate对JDBC访问数据库的代码做了轻量级封装,大大简化了数据访问层繁琐的重复性代码,并且减少了内存消耗,加快了运行效率。

2、Hibernate是一个基于JDBC的主流持久化框架,是一个优秀的ORM实现,它很大程度的简化了DAO(Data Access Object,数据访问对象)层编码工作。

3、Hibernate的性能非常好,映射的灵活性很出色。它支持很多关系型数据库,从一对一到多对多的各种复杂关系。

4、可扩展性强,由于源代码的开源以及API的开放,当本身功能不够用时,可以自行编码进行扩展。

 

二、搭建HIbernate开发环境(基于Eclipse)

第一步:导包:

数据库驱动包

 

 

 Hibernate驱动包

 

 

 日志包

 

 

 第二步:创建数据库和实体类

持久化类是应用程序中的业务实体类,这里的持久化是指类的对象能够被持久化保存到数据库中。Hibernate使用普通Java对象(Plain Old Java Object),即POJO的编程模式来进行持久化。POJO类中包含的是与数据库表相对应的各个属性,这些属性通过getter和setter方法来访问,对外部隐藏了内部的实现细节。下面就来编写Customer持久化类。

在项目src目录下,创建cn.ybb.domain包,并在包中创建实体类Customer(对应数据库表cst_customer),Customer类包含与cst_customer数据表字段对应的属性,以及相应的getXxx ()和setXxx ()方法。

package comybb.domain;

import java.io.Serializable;

/**
 * 客户实体类
 * @author yuanbeibei
 *
 * Apr 11, 2021  7:49:50 PM
 *
 */
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 + "]";
    }
    
    
    
}
View Code

切记:实现serialzable序列化方法,目的:

序列化,就是为了在不同时间或不同平台的JVM之间共享实例对象

第三步:编写映射配置文件

实体类Customer目前还不具备持久化操作的能力,而Hibernate需要知道实体类Customer映射到数据库Hibernate中的哪个表,以及类中的哪个属性对应数据库表中的哪个字段,这些都需要在映射文件中配置。

在实体类Customer所在的包中,创建一个名称为Customer.hbm.xml的映射文件,在该文件中定义了实体类Customer的属性是如何映射到cst_customer表的列上的。

<?xml version="1.0" encoding="UTF-8"?>
<!-- 导入约束:dtd约束
位置:在Hibernate的核心jar包中名称为hibernate-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.ybb.domain">
    <!-- package属性用于设定包的名称,接下来该配置文件中凡是用到此包中的对象时都可以省略包名 -->
    <!-- class标签
            作用:建立实体类和表的对应关系
            属性:
                name:指定实体类的名称
                table:指定数据库表的名称
     -->
     <class name="Customer" table="cst_customer">
         <!-- id标签
             作用:用于映射主键
             属性:
                 name:指定的是属性名称。也就是get/set方法后面的部分,并且首字母要转小写。
                 column:指定的是数据库表的字段名称
          -->
          <id name="custId" column="cust_id">
              <!-- 
              generator标签:
            作用:配置主键的生成策略。
            属性:
                class:指定生成方式的取值。
                取值之一:native。使用本地数据库的自动增长能力。
                mysql数据库的自动增长能力是让某一列自动+1。但是不是所有数据库都支持这种方式。
               -->
               <generator class="native"></generator>
          </id>
          <!-- 
          property标签:
                作用:映射其他字段
                属性:
                    name:指定属性的名称。和id标签的name属性含义一致
                    column:指定数据库表的字段名称
           -->
          <property name="custName" column="cust_name"></property>
         <property name="custLevel" column="cust_level"></property>
            <property name="custSource" column="cust_source"></property>
          <property name="custIndustry" column="cust_industry"></property>
         <property name="custAddress" column="cust_address"></property>
         <property name="custPhone" column="cust_phone"></property>
     </class>

</hibernate-mapping>
View Code

 

posted on 2021-04-11 21:53  南瓜小子  阅读(280)  评论(0)    收藏  举报

导航