【极客营】Hibernate入门到精通- hibernate的快速入门

作者:何征天

课程视频地址:https://ke.qq.com/course/273907

  hibernate的jar包下载和导入

1. 下载相应的jar包等
  * http://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/hibernate-release-5.0.7.Final.zip/download    
    
2. 解压后对目录结构有一定的了解

 

Hibernate开发包内容:

 Jar包的导入(开发环境的搭建)

新建web工程Hibernate5_d01_c03

引入Hibernate开发所需要的jar包

   * MySQL的驱动jar包
  * Hibernate开发需要的jar包(资料/hibernate-release-5.0.7.Final/lib/required/所有jar包)

  

基于hibernate实现数据库表CRUD的操作(重点)

开发准备和配置(三个准备,7个步骤)

【第一个准备】:创建表结构:


create database hibernate_day01;

use hibernate_day01;

CREATE TABLE `cst_customer` (

  `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',

  `cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',

  `cust_user_id` bigint(32) DEFAULT NULL COMMENT '负责人id',

  `cust_create_id` bigint(32) DEFAULT NULL COMMENT '创建人id',

  `cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',

  `cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',

  `cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',

  `cust_linkman` varchar(64) DEFAULT NULL COMMENT '联系人',

  `cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',

  `cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',

  PRIMARY KEY (`cust_id`)

) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

【第二个准备】: 编写Hibernate核心的配置文件

1. 在src目录下,创建名称为hibernate.cfg.xml的配置文件
2. 在XML中引入DTD约束

<!DOCTYPE hibernate-configuration PUBLIC

    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">


打开:资料/hibernate-release-5.0.7.Final/project/etc/hibernate.properties,可以查看具体的配置信息
        * 必须配置的4大参数
            #hibernate.connection.driver_classcom.mysql.jdbc.Driver
            #hibernate.connection.urljdbc:mysql:///test
            #hibernate.connection.usernamegavin
            #hibernate.connection.password
        
        * 数据库的方言(必须配置的)
            #hibernate.dialectorg.hibernate.dialect.MySQLDialect
        
        * 可选的配置
            #hibernate.show_sqltrue
            #hibernate.format_sqltrue
            #hibernate.hbm2ddl.autoupdate
        
        * 引入映射配置文件(一定要注意,要引入映射文件,框架需要加载映射文件)
            * <mapping 
resource="com/igeek/demo1/Customer.hbm.xml"/>
    

4. 具体的配置如下

<?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>

       <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

       <property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property>

       <property name="hibernate.connection.username">root</property>

       <property name="hibernate.connection.password">123456</property>

       <property name="hibernate.dialect org.hibernate.dialect.MySQLDialect"></property>

    </session-factory>

</hibernate-configuration>

5.  在com.igeek.demo1中创建TestCustomer类,测试是否连接上数据库,代码如下:

packagecom.igeek.demo1;

 

import org.hibernate.cfg.Configuration;

import org.junit.Test;

 

publicclass TestCustomer {

    @Test

    publicvoid run1(){

       //hibernate读取连接数据的配置文件hibernate.cfg.xml

       Configuration config = new Configuration();

       config.configure();

       //连接数据库

       SessionFactory factory = config.buildSessionFactory();

    }

}

5.  在com.igeek.demo1中创建TestCustomer类,测试是否连接上数据库,代码如下:

packagecom.igeek.demo1;

 

import org.hibernate.cfg.Configuration;

import org.junit.Test;

 

publicclass TestCustomer {

    @Test

    publicvoid run1(){

       //hibernate读取连接数据的配置文件hibernate.cfg.xml

       Configuration config = new Configuration();

       config.configure();

       //连接数据库

       SessionFactory factory = config.buildSessionFactory();

    }

}

【第三个准备】:编写实体类和ORM映射文件

【编写Customer实体类】

在com.igeekdemo1包中创建Customer.java类,类中代码如下:

1. Customer 类的代码如下:

publicclass Customer {

    private Long cust_id;

    private String cust_name;

    private Long cust_user_id;

    private Long cust_create_id;

    private String cust_source;

    private String cust_industry;

    private String cust_level;

    private String cust_linkman;

    private String cust_phone;

    private String cust_mobile;

    // 省略get和set方法

}

【创建类与表结构的映射】

1. 在JavaBean所在的包下创建映射的配置文件
 * 默认的命名规则为:实体类名.hbm.xml
 * 在xml配置文件中引入约束(引入的是hibernate3.0的dtd约束,不要引入4的约束)

<!DOCTYPE hibernate-mapping PUBLIC

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

2. 如果不能上网,编写配置文件是没有提示的,需要自己来配置
  * 先复制http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd --> window--> preferences --> 搜索xml --> 选择xml catalog --> 点击add --> 选择URI --> 粘贴复制的地址 --> 选择location,选择本地的DTD的路径

【请注意】在配置完catalog之后,需要重新打开Customer.hbm.xml文件



3. 编写映射的配置文件

<?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="com.igeek.demo1.Customer" table="cst_customer">

       <id name="cust_id" column="cust_id">

           <generator class="native"></generator>

       </id>

       <property name="cust_name" column="cust_name"/>

       <property name="cust_user_id" column="cust_user_id"/>

       <property name="cust_create_id" column="cust_create_id"/>

       <property name="cust_source" column="cust_source"/>

       <property name="cust_industry" column="cust_industry"/>

       <property name="cust_level" column="cust_level"/>

       <property name="cust_linkman" column="cust_linkman"/>

       <property name="cust_phone" column="cust_phone"/>

       <property name="cust_mobile" column="cust_mobile"/>

    </class>

</hibernate-mapping>

 4.在hibernate.cfg.xml中引入上述配置文件

       <mapping resource="com/igeek/demo1/Customer.hbm.xml"/>

7个步骤】 : java编程实现CRUD,基本示例如下:


 保存(插入)数据

在TestCustomer类中编写testSave方法用来保存数据,代码如下:

publicclass TestCustomer {

    @Test

    publicvoid testSave(){

       //先加载配置文件,默认是src目录下的hibernate.cfg.xml

       Configuration config = new Configuration().configure();

       //创建sessionFactory对象

       SessionFactory factory = config.buildSessionFactory();

       //创建session对象

       Session session = factory.openSession();

       //开启事务

       Transaction tr = session.beginTransaction();

       //编写保存代码

       Customer c = new Customer();

       c.setCust_name("刘亦菲");

       c.setCust_mobile("1381383838888");

       //保存客户

       session.save(c);

       //提交事务

       tr.commit();

       //释放资源

       session.close();

       factory.close();

      

    }

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值