Hibernate学习笔记1

Hibernate–1

学习内容:

  1. Hibernate相关jar包的依赖

  2. 创建表和实体之间的映射配置文件Student.hbm.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <?xml version="1.0" encoding="UTF-8" ?>
    <!--指定命名空间,从hibernate-core-5.2.16.Final包中找-->
    <!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.lee.hibernate1.domain">
    <class name="Student" table="student">
    <id name="sid" column="sid">
    <!--主键自增长,使用数据库本地的自增长能力-->
    <generator class="native"></generator>
    </id>
    <property name="name" column="sname"></property>
    <property name="sex" column="ssex"></property>
    <property name="age" column="sage"></property>
    <property name="address" column="saddress"></property>
    </class>
    </hibernate-mapping>

  3. 创建hibernate.cfg.xml配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    <?xml version="1.0" encoding="UTF-8" ?>
    <!--导入约束文件文件 ,从从hibernate-core-5.2.16.Final包中找到-->
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
    <!--配置SessionFactory,它是数据库连接的会话对象,它是执行CRUD的对象-->

    <!--创建SessionFactory对象的三部分必须配置:
    1.第一部分:
    连接数据库的信息
    2.第二部分
    hibernate的可选配置
    3.第三部分
    映射配置文件的位置
    -->
    <session-factory>
    <!--第一部分-->
    <!--JDBC驱动-->
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <!--连接数据库的url-->
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
    <!--数据库的用户名-->
    <property name="hibernate.connection.username">root</property>
    <!--登录数据库的密码-->
    <property name="hibernate.connection.password">123</property>
    <!--#hibernate.dialect org.hibernate.dialect.MySQLDialect-->
    <!--&lt;!&ndash;配置数据库方言&ndash;&gt;-->
    <!--<property name="hibernate.dialect">org.hibernate.dialect.MySQL55Dialect</property>-->

    <!--第二部分-->
    <!--是否在控制台显示生成的sql语句-->
    <property name="hibernate.show_sql">true</property>
    <!--是否将控制台里的sql语句格式化输出-->
    <property name="hibernate.format_sql">true</property>
    <!--采用何种方式生成DDL语句-->
    <!--其中update表示:检测实体类和表结构是否一致,如果不一致,更新表结构达到一致,如果不存在该表,就创建一张表-->
    <property name="hibernate.hbm2ddl.auto">update</property>

    <!--第三部分-->
    <!--指定映射的bean配置文件的位置-->
    <!--如果有多个,就配置多个-->
    <mapping resource="com/lee/hibernate1/Student.hbm.xml"/>
    </session-factory>
    </hibernate-configuration>

  4. 使用hibernate向数据库中插入数据的小练习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public void test1() {
Student student = new Student();
student.setName("Lee");
student.setAddress("安康市");
student.setSex("男");

// 1. 解析主配置文件
Configuration configuration = new Configuration();
configuration.configure(); // 配置默认的配置文件,即为根目录下的hibernate.cfg.xml文件
// 2.根据主配置文件,创建SessionFaction对象
SessionFactory factory = configuration.buildSessionFactory();
// 3.创建Session
Session session = factory.openSession();
// 4.开启事务
Transaction transaction = session.beginTransaction();
// 5.执行插入
session.save(student);
// 6.提交事务
transaction.commit();
// 7.释放资源
session.close();
factory.close();
}

Configuration

配置配置文件

一般在hibernate.cfg.xml中配置的东西,在该对象中都是可以配置的,但是不推荐这么做,硬编码,后期维护升级很费事

SessionFactory

  • 该对象在服务器启动(应用启动)的时候创建,在服务器关闭(应用卸载)的时候销毁,一个应用只存在一个实例
  • 它是线程安全的
  • 在创建SessionFactory以后对配置文件进行更改,将不会影响到该factory

Session

  • 一个线程只有一个对象

Transaction

Hibernate工具类

Hibernate中CRUD操作

Hibernate中的异常处理

HibernateException继承了RuntimeException,这个异常可以不用捕获

Hibernate中配置c3p0连接池

F:\java资料\SSH框架\hibernate-release-5.2.16.Final\project\etc/hibernate.properties文件中查找配置格式(搜索c3p0)

Hibernate中查询的get和load方法的区别

查询时机不同,返回查询的结果不一样

  • get:立即加载
  • load:延迟加载(懒加载,惰性加载),返回的对象是增强对象(动态代理),增强了toString()方法;他也可以通过配置的方式改为立即加载

学习中遇到的问题及其结局方案:

使用的MySQL版本为5.5,使用Hibernate的版本为5.2.16,在配置hibernate.cfg.xml的时候

1
2
<!--配置数据库方言-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

在MySQL版本为5.5及以上时,如果配置数据库方言,将org.hibernate.dialect.MySQLDialect改为上面的

org.hibernate.dialect.MySQL5Dialect 不然在hibernate创建表格的时候会报错.

或者这个配置项直接不配置也是可以的