一、ORM技术

ORM:Obejict Relation Mapping,对象关系映射。ORM技术可以建立对象与数据库之间的关系。它可以通过对象来访问数据库。目前市场有:Hibernate、MyBateis、TopLink、EJB等等

二、Hibernate框架

Hibernate是目前市场上最流行的ORM框架之一。

它的优点

1)Hibernate使用面向对象的方式来访问数据库;

2)Hibernate的查询性能更好;

3)Hibernate可以在不同的数据库之间进行切换,因此可移植性更强;

在实际使用中,Hibernate是属于Dao层技术。

2.1Hibernate的下载和安装

Hibernate的官网:http://www.sourceforge.net

下载完成后直接压缩。它的目录结果:

Hibernate框架的讲解

documention:保存Hibernate的API文档;

lib:保存Hibernate相关的jar包

project:保存Hibernate项目的源文件

2.2第一个Hibernate例子

第一步:导入jar包

把lib/required目录下所有jar包复制到工程中。除此以外,还要把数据库的驱动包复制到工程中。

第二步:创建表

1 CREATE TABLE `cst_customer` (
 2   `cust_id` BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
 3   `cust_name` VARCHAR(32) NOT NULL COMMENT '客户名称(公司名称)',
 4   `cust_source` VARCHAR(32) DEFAULT NULL COMMENT '客户信息来源',
 5   `cust_industry` VARCHAR(32) DEFAULT NULL COMMENT '客户所属行业',
 6   `cust_level` VARCHAR(32) DEFAULT NULL COMMENT '客户级别',
 7   `cust_address` VARCHAR(128) DEFAULT NULL COMMENT '客户联系地址',
 8   `cust_phone` VARCHAR(64) DEFAULT NULL COMMENT '客户联系电话',
 9   PRIMARY KEY (`cust_id`)
10 ) ENGINE=INNODB DEFAULT CHARSET=utf8;

第三步:创建实体类

实体类就是符合JavaBeen的规范的类

编写JavaBeen的规范

1)把成员属性私有化;

2)为每个成员属性提供setter和getter方法

3)提供无参的构造函数;

4)建议实现Serializable接口;

public class Customer implements Serializable {
    
    private Integer custId;//客户编号
    private String custName;//客户名称
    private String source;//客户信息的来源
    private String industry;//客户所在的行业;
    private String level;//客户的等级
    private String address;//联系地址
    private String phone;//联系电话
    public Integer getCustId() {
        return custId;
    }
    public void setCustId(Integer custId) {
        this.custId = custId;
    }
    public String getCustName() {
        return custName;
    }
    public void setCustName(String custName) {
        this.custName = custName;
    }
    public String getSource() {
        return source;
    }
    public void setSource(String source) {
        this.source = source;
    }
    public String getIndustry() {
        return industry;
    }
    public void setIndustry(String industry) {
        this.industry = industry;
    }
    public String getLevel() {
        return level;
    }
    public void setLevel(String level) {
        this.level = level;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
}

第四步:配置实体类与数据库表的映射关系

在实体类所在包下新建一个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">
 <!-- package属性:指定实体类的包名 -->
 <hibernate-mapping package="com.enter.dome">
   <!-- class用来配置实体类与数据库表的映射关系 
      name:类名
      table:实体类所对应的表
   -->
   <class name="Customer" table="cst_customer">
     <!-- 配置实体类的ID与主键的映射关系
         name:实体类的属性名
         column:表的子段名
      -->
      <id name="custId" column="cust_id">
        <!-- 主键的自增方式 -->
        <generator class="identity"/>
      </id>
      <!-- 配置实体类成员属性与字段的关系 -->
      <property name="custName" column="cust_name"/>
      <property name="source" column="cust_source"/>
      <property name="industry" column="cust_industry"/>
      <property name="level" column="cust_level"/>
      <property name="address" column="cust_address"/>
      <property name="phone" column="cust_phone"/>
   </class>
 
 </hibernate-mapping>

第五步:创建Hibernate核心配置文件

把hibernate/project/etc目录下的hibernate.cfg.xml拷贝项目的src目录下。

然后修改文件,设置hibernate的配置信息。详细的配置信息可以参考hibernate.properties文件中的配置。

<!--
   ~ Hibernate, Relational Persistence for Idiomatic Java
   ~
   ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
   ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
   -->
 <!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.dialect">org.hibernate.dialect.MySQLDialect</property>
          <!-- 配置数据库的驱动 -->
          <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
          <!-- 数据库驱动 -->
          <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jun1</property>
          <!-- 用户名 -->
          <property name="hibernate.connection.username">root</property>
          <!-- 密码 -->
          <property name="hibernate.connection.password">entor</property>
          <!-- 是否显示SQL语句 -->
         <property name="show_sql">true</property>
         <!-- 指定映射文件的位置 
         <mapping resource="com/entor/doction/Customer.hbm.xml"/>-->
         <mapping class="com.enter.dome.Customer"/>
     </session-factory>
 </hibernate-configuration>

第六步:使用Hibernate提供的API访问数据库

Session:提供了一些访问数据库的方法。例如:save()、update()、delete()、get()等等。每次访问数据库都应该创建一个Session。

SessionFactory:Session的工厂类,它负责产生Session对象。当服务器启动的时候创建该对象即可。也就是说,该只需要创建一次。

Configuration:代表Hibernate的配置。它的作用:1)加载配置文件;2)创建SessionFactory对象

public class HelloWorld {
     public static void main(String[] args) {
         Configuration configuration=new Configuration();
         configuration.configure();
         SessionFactory factory=configuration.buildSessionFactory();
         Session session=factory.openSession();
         Customer customer=new Customer();
         //Transaction tx=session.beginTransaction();
         
         customer.setCustName("陈骏金111");
         session.save(customer);
         //tx.commit();
         session.close();
         /*Transaction tx=session.beginTransaction();
         customer.setCustId(1);
         customer.setCustName("陈骏金11");
         session.update(customer);
         tx.commit();
         session.close();*/
     }
 
 }