热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Hibernate的入门:

1下载Hibernate5http:sourceforge.netprojectshibernatefileshibernate-orm5.0.7.Finalhibernate-r

1 下载Hibernate5

http://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/hibernate-release-5.0.7.Final.zip/download

项目目录:

技术分享图片

2 创建表

技术分享图片技术分享图片
 1 Create database hibernate_day01;
 2 Use hibernate_day01;
 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_user_id` bigint(32) DEFAULT NULL COMMENT 负责人id,
 7   `cust_create_id` bigint(32) DEFAULT NULL COMMENT 创建人id,
 8   `cust_source` varchar(32) DEFAULT NULL COMMENT 客户信息来源,
 9   `cust_industry` varchar(32) DEFAULT NULL COMMENT 客户所属行业,
10   `cust_level` varchar(32) DEFAULT NULL COMMENT 客户级别,
11   `cust_linkman` varchar(64) DEFAULT NULL COMMENT 联系人,
12   `cust_phone` varchar(64) DEFAULT NULL COMMENT 固定电话,
13   `cust_mobile` varchar(16) DEFAULT NULL COMMENT 移动电话,
14   PRIMARY KEY (`cust_id`)
15 ) ENGINE=InnoDB AUTO_INCREMENT=94 DEFAULT CHARSET=utf8;
View Code

3 引入Hibernate的开发包

数据库驱动包:

Hibernate/lib/required/*

引入日志记录的包:

技术分享图片

 

4  创建实体

技术分享图片技术分享图片
package com.itheima.domain;

/*
 * 客户的javaBean
 * @author chenyanlong
 */
public class 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;
    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 Long getCust_user_id() {
        return cust_user_id;
    }
    public void setCust_user_id(Long cust_user_id) {
        this.cust_user_id = cust_user_id;
    }
    public Long getCust_create_id() {
        return cust_create_id;
    }
    public void setCust_create_id(Long cust_create_id) {
        this.cust_create_id = cust_create_id;
    }
    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_linkman() {
        return cust_linkman;
    }
    public void setCust_linkman(String cust_linkman) {
        this.cust_linkman = cust_linkman;
    }
    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;
    }

    
}
View Code

5 创建映射

技术分享图片技术分享图片
 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="com.itheima.domain.Customer" table="cst_customer">
 8            
12            <id name="cust_id" column="cust_id">
13                
14             <generator class="native"/>
15                
16            id>
17            
18          
19         <property name="cust_name" column="cust_name">property>
20         <property name="cust_user_id" column="cust_user_id">property>
21         <property name="cust_create_id" column="cust_create_id">property>
22         <property name="cust_source" column="cust_source">property>
23         <property name="cust_industry" column="cust_industry">property>
24         <property name="cust_level" column="cust_level">property>
25         <property name="cust_linkman" column="cust_linkman">property>
26         <property name="cust_phone" column="cust_phone">property>
27         <property name="cust_mobile" column="cust_mobile">property>
28 
29    class>
30 hibernate-mapping>
View Code

6 创建Hibernate的核心配置文件

技术分享图片技术分享图片
 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 
 6 <hibernate-configuration>
 7     
 8     
 9     <session-factory>
10         
11         
12         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>
13         <property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01property>
14         <property name="hibernate.connection.username">rootproperty>
15         <property name="hibernate.connection.password">123456property>
16         
17         
18         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialectproperty>
19          
20         
21         
22         
23         <mapping resource="com/itheima/domain/Customer.hbm.xml"/>
24 
25     session-factory>
26 hibernate-configuration>
View Code

7 编写代码

技术分享图片技术分享图片
 1 package com.itheima.test;
 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 import com.itheima.domain.Customer;
 9 
10 /*
11  * 测试Hibernate框架
12  * @author chenyanlong
13  */
14 public class Demo1 {
15 
16     @Test
17     public void testSave(){
18         /*
19          * 1.先加载配置文件
20          * 2.创建SessionFactory对象,生成Session对象
21          * 3.创建Sesison对象
22          * 4.开启事务
23          * 5.编写保存代码
24          * 6.提交事务
25          * 7.释放资源
26          */
27         
28         //1.加载配置文件
29         Configuration cOnfig=new Configuration();
30         //默认加载src目录下hibernate.cfg.xml的配置文件
31         config.configure();
32         //2.创建SessionFactory对象
33         SessionFactory factory=config.buildSessionFactory();
34         //3.创建session对象
35         Session session=factory.openSession();
36         //4.开启事务
37         Transaction tr=  session.beginTransaction();
38         
39         //5.编写保存代码
40         Customer customer = new Customer();
41         customer.setCust_name("小王");
42         customer.setCust_source("小广告");
43 
44         
45         session.save(customer);
46         //6.提交事务
47         tr.commit();
48         
49         //7.释放资源
50         session.close();
51         factory.close();
52     }
53 }
View Code

8 运行效果

技术分享图片

Hibernate的入门:


推荐阅读
  • postman使用环境变量
    变量postman提供了变量设置,有四种变量类型本地变量全局变量环境变量数据变量什么是环境变量环境变量指在不同环境,同一个变量值随着环境不同而变化,比如在测试环境时,host为:d ... [详细]
  • ARM LCD屏调试3屏的应用编程
    2011-06-2519:20:47驱动自己写完了,应用函数自己就不写了,找了一点代码参考,移植并修改了一下,配合之前的定义的接口文档,我贴出部分代码。目录:一,开发环境1二 ... [详细]
  • web前端菜鸟如何升级到大神(转载)标签:web前端web前端菜鸟如何升级到大神(转载)标签:web前端随着互联网的发展速度迅猛,web前端工程师越来越火热,想学习Web前端开发吗 ... [详细]
  • --查看当前已有的用户SELECTUsernameFROMdba_users;--创建临时CREATEUSERgzmpcIDENTIFIEDBYPASSWORD;--授权GRANT ... [详细]
  • 1、dt视图结构内容dt+结构名要么dt+结构名+住址kddt_object_headernt!_OBJECT_HEADER+0x000PointerCount:Int4B+0x ... [详细]
  • Javascript的模块化编程Javascript在设计之初并没有提供一种原生的,语言级别的模块化方式来组织代码,比如Java语言通过package和import来管理和使用模块 ... [详细]
  • 来看看倒排索引压缩。压缩是拿CPU换IO的最重要手段之一,不论索引是放在硬盘还是内存中。索引压缩的算法有几十种,跟文本压缩不同,索引压缩算法不仅仅需要考虑压缩率,更要考虑压缩和解压 ... [详细]
  • https:developers.weixin.qq.comminiprogramdevapinetworksocket.htmlwxconnectsocke ... [详细]
  • Mac安装Appium
    Appium官网http:appium.io实际步骤1.appium基于node.js首先安装node.jsbrewinstallnode2.安装appiumnpminstal ... [详细]
  • 实验六提交版
    1.21.3part2共用体与结构体类型的区别?答:共用体与结构体的区别在于它们的表示方法不同。结构体内,结构体的各成员顺序排列存储,每个成员都有自己独立的存储位置,而共用体的情况 ... [详细]
  • 获取鼠标的位置/坐标
    使用javascript如何获取鼠标的位置呢?获取光标的位置?获取鼠标坐标先看效果?核心方法:****返回鼠标的坐标*@parame*@returns{{x ... [详细]
  • 题目:写一个函数返回参数二进制中1的个数方法1:我自己写的,运用‘%‘和‘‘,感觉挺简单的。intcount_one_bit(intnum){unsignedintcount0;w ... [详细]
  • 【7】继承、super、this、抽象类
    1、继承定义:继承就是子类继承父类的属性和行为,使得子类对象具有与父类相同的属性、相同的行为。子类可以直接访问父类中的非私有的属性和行为。好处:1、提高代码的复用性。2、类与类之间 ... [详细]
  • 网络Cisco考试
    二、操作题(共80分)请将以下拓扑实验配置完毕,保存拓扑,建立一个文本文档,按照交换机-路由器1234的顺序,将每台设备的showrunning-config复制粘贴出来,将文本文 ... [详细]
  • 虚拟机需要关闭bcdeditsethypervisorlaunchtypeoffdocker需要开启bcdeditsethypervisorlauncht ... [详细]
author-avatar
吴姿云68153
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有