Hibernate学习笔记1(常见配置)

淩亂°似流年 2021-12-24 00:27 322阅读 0赞

1.为什么要学习Hibernate

在这里插入图片描述

2.什么是Hibernate

在这里插入图片描述
注:ORM:Object Relational Mapping(对象关系映射)。指的是将一个Java中的对象与关系型数据库中的表建立一种映射关系,从而操作对象就可以操作数据库中的表。

3.配置安装

首先下载,解压https://sourceforge.net/projects/hibernate/files/,目录如下
在这里插入图片描述

  • documentation :Hibernate开发的文档
  • lib :Hibernate开发包
    required :Hibernate开发的必须的依赖包
    optional :Hibernate开发的可选的jar包
  • project :Hibernate提供的项目

创建项目,lib导包
在这里插入图片描述
创建数据库
在这里插入图片描述
创建实体类

  1. package day01;
  2. /*
  3. * CREATE TABLE `cst_customer` (
  4. `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
  5. `cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
  6. `cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
  7. `cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
  8. `cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
  9. `cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
  10. `cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
  11. PRIMARY KEY (`cust_id`)
  12. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  13. *
  14. * 客户管理实体类
  15. */
  16. public class Customer {
  17. private Long cust_id;
  18. private String cust_name;
  19. private String cust_source;
  20. private String cust_industry;
  21. private String cust_level;
  22. private String cust_phone;
  23. private String cust_mobile;
  24. public Long getCust_id() {
  25. return cust_id;
  26. }
  27. public void setCust_id(Long cust_id) {
  28. this.cust_id = cust_id;
  29. }
  30. public String getCust_name() {
  31. return cust_name;
  32. }
  33. public void setCust_name(String cust_name) {
  34. this.cust_name = cust_name;
  35. }
  36. public String getCust_source() {
  37. return cust_source;
  38. }
  39. public void setCust_source(String cust_source) {
  40. this.cust_source = cust_source;
  41. }
  42. public String getCust_industry() {
  43. return cust_industry;
  44. }
  45. public void setCust_industry(String cust_industry) {
  46. this.cust_industry = cust_industry;
  47. }
  48. public String getCust_level() {
  49. return cust_level;
  50. }
  51. public void setCust_level(String cust_level) {
  52. this.cust_level = cust_level;
  53. }
  54. public String getCust_phone() {
  55. return cust_phone;
  56. }
  57. public void setCust_phone(String cust_phone) {
  58. this.cust_phone = cust_phone;
  59. }
  60. public String getCust_mobile() {
  61. return cust_mobile;
  62. }
  63. public void setCust_mobile(String cust_mobile) {
  64. this.cust_mobile = cust_mobile;
  65. }
  66. }

创建映射
映射需要通过XML的配置文件来完成,这个配置文件可以任意命名。尽量统一命名规范(类名.hbm.xml)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping>
  6. <!-- 建立类与表的映射 -->
  7. <class name="day01.Customer.java" table="cst_customer">
  8. <!-- 建立类中的属性与表中的主键对应 -->
  9. <id name="cust_id" column="cust_id" >
  10. <generator class="native"/>
  11. </id>
  12. <!-- 建立类中的普通的属性和表的字段的对应 -->
  13. <property name="cust_name" column="cust_name" length="32" />
  14. <property name="cust_source" column="cust_source" length="32"/>
  15. <property name="cust_industry" column="cust_industry"/>
  16. <property name="cust_level" column="cust_level"/>
  17. <property name="cust_phone" column="cust_phone"/>
  18. <property name="cust_mobile" column="cust_mobile"/>
  19. </class>
  20. </hibernate-mapping>

创建一个Hibernate的核心配置文件
Hibernate的核心配置文件的名称:hibernate.cfg.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  8. <property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>
  9. <property name="hibernate.connection.username">root</property>
  10. <property name="hibernate.connection.password">root</property>
  11. <!-- 配置Hibernate的方言 -->
  12. <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  13. <!-- 可选配置================ -->
  14. <!-- 打印SQL -->
  15. <property name="hibernate.show_sql">true</property>
  16. <!-- 格式化SQL -->
  17. <property name="hibernate.format_sql">true</property>
  18. <mapping resource="day01/Customer.hbm.xml"/>
  19. </session-factory>
  20. </hibernate-configuration>

编写测试类

  1. package day01;
  2. import org.hibernate.Session;
  3. import org.hibernate.SessionFactory;
  4. import org.hibernate.Transaction;
  5. import org.hibernate.cfg.Configuration;
  6. import org.junit.Test;
  7. /**
  8. * Hibernate的入门案例
  9. * @author ffx
  10. *
  11. */
  12. public class HibernateDemo {
  13. @Test
  14. // 保存客户的案例
  15. public void demo(){
  16. // 1.加载Hibernate的核心配置文件
  17. Configuration configuration = new Configuration().configure();
  18. // 手动加载映射
  19. // configuration.addResource("Customer.hbm.xml");
  20. // 2.创建一个SessionFactory对象:类似于JDBC中连接池
  21. SessionFactory sessionFactory = configuration.buildSessionFactory();
  22. // 3.通过SessionFactory获取到Session对象:类似于JDBC中Connection
  23. Session session = sessionFactory.openSession();
  24. // 4.手动开启事务:
  25. Transaction transaction = session.beginTransaction();
  26. // 5.编写代码
  27. Customer customer = new Customer();
  28. customer.setCust_name("张三");
  29. session.save(customer);
  30. // 6.事务提交
  31. transaction.commit();
  32. // 7.资源释放
  33. session.close();
  34. sessionFactory.close();
  35. }
  36. }

在这里插入图片描述
红字因为未配置日志文件,不影响
在这里插入图片描述
XML提示的配置
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
Hibernate的映射的配置

  • 【class标签的配置】
    标签用来建立类与表的映射关系
    属性:
    name :类的全路径
    table :表名(类名与表名一致,table可以省略)
    catalog :数据库名
  • 【id标签的配置】
    标签用来建立类中的属性与表中的主键的对应关系
    属性:
    name :类中的属性名
    column :表中的字段名(类中的属性名和表中的字段名如果一致,column可以省略)
    length :长度
    type :类型
  • 【property标签的配置】
    标签用来建立类中的普通属性与表的字段的对应关系
    属性:
    name :类中的属性名
    column :表中的字段名
    length :长度
    type :类型
    not-null :设置非空
    unique :设置唯一

Hibernate的核心的配置
1. Hibernate的核心配置方式

  • 一种方式:属性文件的方式
    hibernate.properties
    hibernate.connection.driver_class=com.mysql.jdbc.Driver

    hibernate.show_sql=true
    属性文件的方式不能引入映射文件(手动编写代码加载映射文件)
  • 二种方式:XML文件的方式
    hibernate.cfg.xml
    2 核心的配置
  • 必须的配置

    • 连接数据库的基本的参数

      • 驱动类
      • url路径
      • 用户名
      • 密码
    • 方言
  • 可选的配置

    • 显示SQL :hibernate.show_sql
    • 格式化SQL :hibernate.format_sql
    • 自动建表 :hibernate.hbm2ddl.auto

      • none :不使用hibernate的自动建表
      • create :如果数据库中已经有表,删除原有表,重新创建,如果没有表,新建表。(测试)
      • create-drop :如果数据库中已经有表,删除原有表,执行操作,删除这个表。如果没有表,新建一个,使用完了删除该表。(测试)
      • update :如果数据库中有表,使用原有表,如果没有表,创建新表(更新表结构)
      • validate :如果没有表,不会创建表。只会使用数据库中原有的表。(校验映射和表结构)。
  • 映射文件的引入
    引入映射文件的位置

发表评论

表情:
评论列表 (有 0 条评论,322人围观)

还没有评论,来说两句吧...

相关阅读

    相关 hibernate学习笔记

    如果在hibernate.cfg.xml配置文件里面不不太清楚某个数据库的方言的话,按下 CTRL+SHIFT+T就会弹出一个窗口,在窗口里面输入你想要数据库的名称,就

    相关 Hibernate学习笔记

    hibernate是JAVAEE的一个轻量级的持久层的框架,作用是为了管理类和数据库表之间的映射关系,将底层代码封装,提供一系列的方法用于和数据库进行交互。目前数据库仍然已关系

    相关 Hibernate学习笔记

    一、持久化类规范(不成文的规范) 1、属性是private,有get,set方法。 2、有无参的构造函数。 3、所有的类都要包含一个id,映射数据库表的主键。 4、