侧边栏壁纸
博主头像
张种恩的技术小栈博主等级

行动起来,活在当下

  • 累计撰写 747 篇文章
  • 累计创建 65 个标签
  • 累计收到 39 条评论

目 录CONTENT

文章目录

Hibernate(1)之简介及初使用

zze
zze
2017-07-01 / 0 评论 / 0 点赞 / 536 阅读 / 15065 字

不定期更新相关视频,抖音点击左上角加号后扫一扫右方侧边栏二维码关注我~正在更新《Shell其实很简单》系列

准备

简介

Hibernate 是一个开放源代码的对象关系映射框架,它对 JDBC 进行了非常轻量级的对象封装,它将 POJO 与数据库表建立映射关系,是一个全自动的 OR/M 框架,Hibernate 可以自动生成 SQL 语句,自动执行,使得 Java 程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate 可以应用在任何使用 JDBC 的场合,既可以在 Java 的客户端程序使用,也可以在 Servlet/JSP 的 Web 应用中使用,最具革命意义的是,Hibernate 可以在应用 EJB 的 JaveEE 架构中取代 CMP,完成数据持久化的重任。

下载

关注文章首部微信公众号发送 #19_hibernate5 获取资源。

使用

导包

解压下载好的 zip 包,项目中导入解压后 /lib/required 下所有 jar 包,然后导入上面百度云链接中日志支持包。

简单示例

1、编写一个 JavaBean 作为映射模型,如下:

// com.zze.bean.Customer
package com.zze.bean;

public class Customer {
    private Long cust_id;
    private String cust_name;
    private String cust_source;
    private String cust_industry;
    private String cust_level;
    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 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_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;
    }
}

2、在该 JavaBean 同目录下创建该模型的映射文件:

<!-- com/zze/bean/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">

<hibernate-mapping>
    <!--
    class : 配置类和表的映射
        name : 类的全路径
        table : 对应表名
    如果类名和表名一致,表名可省略不写
    -->
    <class name="com.zze.bean.Customer" table="customer">

        <!--
        id : 配置属性和主键列的映射
            name : 属性名
            column : 列名
        -->
        <id name="cust_id" column="cust_id">
            <!--
            generator : 配置主键的生成策略
                class : 配置主键生成策略类型,可选如下几个值:
                    increment:由 Hibernate 自动以递增的方式生成标识符,每次增量 1
                    identity:由底层数据库生成标识符,前提条件是底层数据库支持自动增长字段类型(DB2,MYSQL)
                    uuid:用 128 位的 UUID 算法生成字符串类型标识符
                    assigned:由java程序负责生成标识符,让应用程序在save()之前为对象分配一个标识符。
            -->
            <generator class="native"/>
        </id>
        <!--
        property : 配置属性和列的映射
            name : 属性名
            column : 列名
            legth : 对应列的长度
        如果属性名和列名一致,列名可以省略不写
        -->
        <property name="cust_name" column="cust_name" length="32"/>
        <property name="cust_source" column="cust_source" length="32"/>
        <property name="cust_industry" column="cust_industry" length="100"/>
        <property name="cust_level" column="cust_level" length="100"/>
        <property name="cust_phone" column="cust_phone" length="30"/>
        <property name="cust_mobile" column="cust_mobile" length="30"/>
    </class>
</hibernate-mapping>

3、在 ClassPath 下新建 Hibernate 全局配置文件:

<!-- 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>
        <!--=======================必须配置start========================-->
        <!--数据库驱动-->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <!--数据库连接url-->
        <property name="hibernate.connection.url">jdbc:mysql://192.168.202.132:3306/test</property>
        <!--数据库用户名-->
        <property name="hibernate.connection.username">root</property>
        <!--数据库密码-->
        <property name="hibernate.connection.password">root</property>
        <!--数据库方言-->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <!--=======================必须配置end========================-->

        <!--=======================可选配置start========================-->
        <!--显示 sql-->
        <property name="hibernate.show_sql">true</property>
        <!--格式化 sql-->
        <property name="hibernate.format_sql">true</property>
        <!--
        是否自动创建数据库表,它主要有一下几个值:
            none : 不使用 hibernate 自动建表
            create : 如果数据库中已经有表,删除原有表重新创建;如果没有表,那就新建表。
            create-drop : 如果数据库中已经有表,删除原有表重新创建,执行完操作后又删除该表;如果没有表,那就新建表,执行完操作后删除该表。
            update (常用) : 如果数据库中已经有表,更新表结构,使用原有表;如果没有表,创建新表。
            validate (常用) : 如果没有表,不会创建表,会校验 JavaBean 和表结构的映射关系,使用数据库中原有的结构。
        -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!--引入映射文件-->
        <mapping resource="com/zze/bean/Customer.hbm.xml"></mapping>
        <!--=======================可选配置end========================-->
    </session-factory>
</hibernate-configuration>

4、测试新增操作:

// 加载核心配置文件
Configuration configuration = new Configuration().configure();
// 创建一个 sessionFactory 对象
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 获取 session
Session session = sessionFactory.openSession();
// 开启事务
Transaction transaction = session.beginTransaction();
Customer customer = new Customer();
customer.setCust_name("张三");
// 新增 customer
session.save(customer);
// 提交事务
transaction.commit();
// 释放 session
session.close();
// 释放 sessionFactory
sessionFactory.close();

/*
    执行之后会发现 Hibernate 在对应数据库中新建了 customer 表,并新建了一条 cust_name 为 ‘张三’ 的数据。
    mysql> select * from customer;
    +---------+-----------+-------------+---------------+------------+------------+-------------+
    | cust_id | cust_name | cust_source | cust_industry | cust_level | cust_phone | cust_mobile |
    +---------+-----------+-------------+---------------+------------+------------+-------------+
    |       1 | 张三      | NULL        | NULL          | NULL       | NULL       | NULL        |
    +---------+-----------+-------------+---------------+------------+------------+-------------+
row in set (0.00 sec)
*/

映射文件的 XML 约束可在 hibernate-core.jar 下的 hibernate-mapping-3.0.dtd 中找到。

全局配置文件的 XML 约束可在 hibernate-core.jar 下的 hibernate-configuration-3.0.dtd 中找到。

全局配置文件的名称固定为 hibernate.cfg.xml,查看源码:

// org.hibernate.cfg.Configuration.configure
public static final String DEFAULT_CFG_RESOURCE_NAME = "hibernate.cfg.xml";

public Configuration configure() throws HibernateException {
    return configure( StandardServiceRegistryBuilder.DEFAULT_CFG_RESOURCE_NAME );
}

全局配置文件中可配置的参数可以在解压后 Hibernate 的 zip 包目录 project/etc/hibernate.properties 文件中找到,去除注释后如下:

hibernate.query.substitutions yes 'Y', no 'N'

hibernate.dialect org.hibernate.dialect.HSQLDialect
hibernate.connection.driver_class org.hsqldb.jdbcDriver
hibernate.connection.username sa
hibernate.connection.password
hibernate.connection.url jdbc:hsqldb:./build/db/hsqldb/hibernate

hibernate.connection.pool_size 1

hibernate.proxool.pool_alias pool1

hibernate.format_sql true

hibernate.max_fetch_depth 1

hibernate.jdbc.batch_versioned_data true

hibernate.jdbc.use_streams_for_binary true

hibernate.cache.region_prefix hibernate.test

hibernate.cache.region.factory_class org.hibernate.cache.internal.NoCachingRegionFactory

hibernate.properties

核心配置文件还可以以 properties 文件形式存在,只是使用 properties 形式核心配置文件时需要手动加载映射文件,如下:

// hibernate.propreties
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://192.168.202.132:3306/test
hibernate.connection.username=root
hibernate.connection.password=root
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
// test
Configuration configuration = new Configuration();
configuration.addResource("com/zze/bean/Customer.hbm.xml");
SessionFactory sessionFactory = configuration.buildSessionFactory();

CRUD 操作

package com.zze.test;

import com.zze.bean.Customer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import java.util.List;

public class HibernateTest {
    @Test
    public void test2() {
        /**
         * 查询所有
         */
        Configuration configuration = new Configuration().configure();
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        Session session = sessionFactory.openSession();
        List from_customer = session.createQuery("from Customer").list();
        for (Object o : from_customer) {
            System.out.println(o);
        }
        session.close();
        sessionFactory.close();
        /*
        Customer{cust_id=1, cust_name='张三'}
         */
    }

    @Test
    public void test3() {
        /**
         * 根据主键查询
         */
        Configuration configuration = new Configuration().configure();
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Customer customer = session.get(Customer.class, 1L);
        System.out.println(customer);
        session.close();
        sessionFactory.close();
        /*
        Customer{cust_id=1, cust_name='张三'}
         */
    }

    @Test
    public void test4() {
        /**
         * 修改
         */
        Configuration configuration = new Configuration().configure();
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        Customer customer = session.get(Customer.class, 1L);
        customer.setCust_name("王德发");
        session.update(customer);
        transaction.commit();
        session.close();
        sessionFactory.close();
    }

    @Test
    public void test5() {
        /**
         * 删除
         */
        Configuration configuration = new Configuration().configure();
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        Customer customer = new Customer();
        customer.setCust_id(1L);
        session.delete(customer);
        transaction.commit();
        session.close();
        sessionFactory.close();
    }

    @Test
    public void test6() {
        /**
         * 新增或修改
         * 目标实例存在主键时修改 不存在则新增
         */
        Configuration configuration = new Configuration().configure();
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        Customer customer = new Customer();
        customer.setCust_name("赵六");
        session.saveOrUpdate(customer);
        transaction.commit();
        session.close();
        sessionFactory.close();
    }
}

load 方法的延迟加载

Session session1 = HibernateUtil.openSession();
Customer customer1 = session1.get(Customer.class, 1l); // 立即发出 sql 语句
System.out.println(customer1); // null
session1.close();
Session session2 = HibernateUtil.openSession();
Customer customer2 = session2.load(Customer.class, 1l); // 不发出 sql 语句
System.out.println(customer2); // throw org.hibernate.ObjectNotFoundException
session2.close();

/*
get 和 load 的不同:
使用 get 时会立即从数据库中查询数据。
使用 load 时会返回一个代理对象,当使用这个代理对象时才会即时从数据库查询数据。
*/

配置 C3P0 连接池

1、导入 hibernate zip 包解压后目录 lib/optional/c3p0 下所有包。
2、在全局配置文件中 hibernate.cfg.xml 配置如下属性:

<!--=======================配置C3P0连接池start=======================-->
<property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
<!--在连接池中可用的数据库连接的最少数目 -->
<property name="c3p0.min_size">5</property>
<!--在连接池中所有数据库连接的最大数目  -->
<property name="c3p0.max_size">20</property>
<!--设定数据库连接的过期时间,以秒为单位,如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 -->
<property name="c3p0.timeout">120</property>
<!--每3000秒检查所有连接池中的空闲连接 以秒为单位-->
<property name="c3p0.idle_test_period">3000</property>
<!--=======================配置C3P0连接池end=======================-->

配置 Log4j

直接在项目的 ClassPath 下新增属性文件 log4j.properties,内容如下:

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

log4j.rootLogger= info, stdout
0

评论区