Hibernate的环境搭建以及简单操作

来源:互联网 发布:淘宝能直接微信支付吗 编辑:程序博客网 时间:2024/05/16 07:21

定义

  • Hibrenate 应用于DAO层的框架
  • 实现数据的CRUD操作
  • 底层的操作为JDBC
  • 简化DAO的操作

Hibernate4.x为过度产品

实现思想

  1. 使用ORM思想对数据库进行CRUD操作
  2. ORM —— Object Relational Mapping

    • 实体类与数据库的表对应
    • 实体类javaBean 的属性与表中的字段一一对应
    • 直接对实体类进行操作

搭建环境

  1. 导入jar包(可以在IDE中先创建相应的user library)

    • hibernate 自身必须的jar包(../hibernate-release-5.0.7.Final/lib/required)

      (E://youdao_img/

    • hibernate jpa规范的jar包(../hibernate-release-5.0.7.Final/lib/jpa)

      jpa

    • hibernate 由于自身没有日记的jar包,需要导入外部的日记jar包,lo4j为日记的规范jar包,而slf4j为实现jar包

      log

    • 导入数据库的驱动包

      driver

  2. 创建javaBean,也就是实体类

    • hibernate 需要实体类有一个属性唯一的;
        public class User {    /**    * Field    */    //唯一属性    private int uid;    private String username;    private String password;    private String address;    //Construct    public User() {    }    public User(String username, String password, String address) {        this.username = username;        this.password = password;        this.address = address;    }    //setter 和 getter 方法省略    //...}
  3. 创建表,hibernate 可以自动创建表

  4. 配置实类和数据库表的一一对应关系

    • 使用配置文件实现
    • xml格式的配置文件
    • 名称和位置无要求(建议:在实体类所在包里面创建,实体类名称.hbm.xml)
    • 在配置文件中引入约束

          <!DOCTYPE hibernate-mapping PUBLIC    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!--配置文件 -->    <?xml version="1.0" encoding="UTF-8"?>    <!-- 引入约束 -->    <!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">    <hibernate-mapping>        <!-- 1. 配置类和表的对应             class标签                name:实体类全路径                table:数据库表的名称        -->        <class name="com.jeff.entity.User" table="t_user">            <!-- 2. 配置实体类id和表id对应 -->            <id name="uid" column="uid">            <!-- 设置数据库表id的增长策略                 native:生成表id值就是主键自动增长            -->                <generator class="native"></generator>            </id>            <!-- 配置其他属性 -->            <property name="username" column="username"></property>            <property name="password" column="password"></property>            <property name="address" column="address"></property>        </class>    </hibernate-mapping>
  5. 创建hibernate的核心配置配文件

    • xml格式,位置固定
    • 位置:必须在src下面
    • 名称:必须hibernate.cfg.xml
    • 引入dtd约束

          <!DOCTYPE hibernate-configuration PUBLIC        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    • hibernate操作过程,只会加载核心配置

      第一部分:配置数据库信息
      第二部分:配置hibernate信息
      第三部分:引入其他配置文件

    <!-- hibernate 的核心配置 --><?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory>        <!-- 第一部分:配置数据库信息 -->         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="connection.url">jdbc:mysql:///hibernate_study</property>        <property name="connection.username">root</property>        <property name="connection.password">123456</property>        <!-- 第二部分:配置hibernate信息 -->        <!-- 输出底层sql语句 -->        <property name="hibernate.show_sql">true</property>        <!-- 对输出底层sql语句进行格式化-->        <property name="hibernate.format_sql">true</property>        <!-- hibernate创建表,需要配置之后        update:如果已经有表,更新;反之创建        -->        <property name="hibernate.hbm2ddl.auto">update</property>        <!-- 配置数据库方言            不同数据库之间会有不同特定的语言,hibernate需要此配置识别特定语言            例如:mysql分页的关键字LIMIT              oracle分页的关键字ROWNUM        -->        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>        <!-- 第三部分:引入其他配置文件 -->        <mapping resource="com/jeff/entity/User.hbm.xml"/>    </session-factory></hibernate-configuration>

实现操作

  1. 加载hibernate核心配置文件
  2. 创建SessionFactory对像
  3. 使用SessionFactory创建session对象
  4. 开始事务(建议手动开始)

  5. 写具体逻辑crud操作(前后写法固定)

  6. 提交事务

  7. 关闭资源
package com.jeff.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.Test;import com.jeff.entity.User;public class HibernateTest {    @Test    public void testAdd() {        //1. 加载hibernate核心配置文件        //    到src下面找到名称是hibernate.cfg.xml        //    在hibernate里面封装对象        Configuration cfg = new Configuration();        cfg.configure();        //2. 创建SessionFactory对像        //    读取hibernate核心配置文件内容,创建sessionFactory        //   在过程中,根据映射关系,在配置数据库里面把表创建        SessionFactory sessionFactory = cfg.buildSessionFactory();        //3. 使用SessionFactory创建session对象        //    类似于连接             Session session = sessionFactory.openSession();        //4. 开始事务(建议手动开始)             Transaction tx = session.beginTransaction();        //5. 写具体逻辑crud操作(前后写法固定)             User user = new User();             user.setUsername("Jeff");             user.setPassword("123456");             user.setAddress("广东茂名");             session.save(user);        //6. 提交事务             tx.commit();        //7. 关闭资源             session.close();             sessionFactory.close();    }}/*    运行结果    log4j:WARN No appenders could be found for logger (org.jboss.logging).    log4j:WARN Please initialize the log4j system properly.    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.    Hibernate:     insert     into        t_user        (username, password, address)     values        (?, ?, ?)*/
0 0