Hibernate框架_搭建第一个Hibernate框架
一、eclipse搭建
A.创建动态web项目
New-->Dynamic web project(web project)
B.导入jar包
1.数据库驱动包

2.hibernate开发必须jar包(去官网下载hibernate,解压后里面lib-->required,里面jar包是hibernate必须jar包)

3.日志记录包

C.创建表(也可以不创建)
D.创建实体类(数据库中如一个客户表就是客户实体,用Java语言描述出来)
public class Customer {
private Long cust_id;
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
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 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_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;
}
@Override
public String toString() {
return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_source=" + cust_source
+ ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_phone=" + cust_phone
+ ", cust_mobile=" + cust_mobile + "]";
}
}
E.根据实体创建映射文件(统一一下文件命名规范:类名.hbm.xml。文件位置一般和实体类放在一起)
Customer.hbm.xml
<?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.xxx.pojo.Customer" table="cst_customer">
<id name="cust_id" column="cust_id">
<!-- 主键生成策略 -->
<generator class="native"></generator>
</id>
<property name="cust_name" column="cust_name"></property>
<property name="cust_source" column="cust_source"></property>
<property name="cust_industry" column="cust_industry"></property>
<property name="cust_level" column="cust_level"></property>
<property name="cust_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property>
</class>
</hibernate-mapping>
标签及其属性所代表的含义:
class标签用来建立类和表的关系映射
*name 表示该类的全路径(带包路径)
*table 与该类映射的表名,如果表名和类名一致,表名可以省略不写
*catalog:数据库名称,可以省略
id标签用来建立类中属性域表中主键字段的对应
*name 类中的属性名
*column 表中的字段名,如果类中的属性名和表中的字段名一致,可以省略column
*length:字段的长度,用于hibernate帮你建表时候指定表中字段的长度,默认为sql类型中长度最大值
*type:类型,有三种写法:
java数据类型:如java.lang.String
hibernate类型: string (type属性默认值)
sql类型: 如varchar
对于type扩展:

property标签:用来建立表中类的普通属性域表中普通字段的对应
这里面也同样有四个属性,name,column,length,type,和上面id标签用法一样,只是多了not-null,unique,可以设置属性非空或者唯一
F.创建hibernate配置文件。(该文件命名为hibernate.cfg.xml)
<?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://localhost:3306/hibernate_crm?useUnicode=true&characterEncoding=UTF8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 配置hibernate方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- hibernate显示发送sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- hibernate格式化sql语句 -->
<property name="hibernate.format_sql">true</property>
<!-- 数据定义语言,主要是对表的操作 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- hibernate映射文件加载 -->
<mapping resource="com/xxx/pojo/customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
对于hibernate.hbm2dd.autol属性,有四个值
- none:不用hibernate自动生成表
- create:每次运行hibernate都会创建一个新的表
- create-drop:每次创建都会创建一个新的表,执行程序结束后删除这个表
- update:如果数据库中有该表,使用数据库中原有的表,如果没有,hibernate会帮你创建一个新表,可以更新表的结构
- validate:只会使用原有的表。对映射关系进行校验
G.编写测试类
/**
* 插入数据
* */
@Test
public void demo1() {
// 加载配置文件
Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
// 根据配置文件获取session工厂对象
SessionFactory sessionFactory = cfg.buildSessionFactory();
// 从session工厂里面获取session对象
Session session = sessionFactory.openSession();
// 开启事务
Transaction tx = session.beginTransaction();
// 保存更新数据
Customer cust = new Customer();
cust.setCust_name("张三");
cust.setCust_source("介绍");
cust.setCust_phone("13085469875");
Serializable uid = session.save(cust);
System.out.println(uid);
// 提交事务
tx.commit();
// 关闭资源
session.close();
}
至此,eclipse手动搭建的hibernate框架就创建好了。
二.MyEclipse自动创建hibernate框架
A.创建web项目
B.增加hibernate框架
右键项目名称 选择Configure facet--->install hibernate facet(安装hibernate方面)




接下来直接finish。
c.生成hibernate映射文件(其实主要就是开启myeclipse数据库,利用数据库反转生成pojo)
https://blog.csdn.net/tuke_tuke/article/details/49803293
myeclipse创建hibernate就大致这样
Hibernate框架_搭建第一个Hibernate框架的更多相关文章
- 2015年3月26日 - Javascript MVC 框架DerbyJS DerbyJS 是一个 MVC 框架,帮助编写实时,交互的应用。
2015年3月26日 - Javascript MVC 框架DerbyJS DerbyJS 是一个 MVC 框架,帮助编写实时,交互的应用.
- 攻城狮在路上(壹) Hibernate(二)--- 第一个hibernate程序
1.直接通过JDBC API持久化实体域对象: A.java.sql常用接口和类: DriverManager:驱动程序管理器,负责创建数据库连接. Connection:代表数据库连接. State ...
- 分享知识-快乐自己:搭建第一个 Hibernate (Demo)
使用 Hibernate 完成持久化操作 七大 步骤: 1.读取并解析配置文件及映射文件: Configuration configuration=new Configuration().config ...
- Spring框架学习-搭建第一个Spring项目
步骤一:下载Spring开发包. 官网:https://spring.io/ 下载地址:https://repo.spring.io/libs-release-local/org/ ...
- Android Afinal框架学习(二) FinalActivity 一个IOC框架
框架地址:https://github.com/yangfuhai/afinal 相应的源代码: net.tsz.afinal.annotation.view.* FinalActivity Fina ...
- 【Hibernate】hibernate框架的搭建
1, Hibernate 是什么 Hibernate是java应用程序与数据库交互的开发的框架. Hibernate是一个开源,轻量级的ORM(对象关系映射)工具. 2,Hibernate框架的优点 ...
- Selenium自动化测试框架的搭建
说 起自动化测试,我想大家都会有个疑问,要不要做自动化测试? 自动化测试给我们带来的收益是否会超出在建设时所投入的成本,这个嘛别说是我,即便是高手也很难回答,自动化测试的初衷是美好的,而测 ...
- 一个小框架,基于rx_retrofit2_mvp
离职在即,也没什么事情做,就鼓捣了一下.任意搭建了一个小框架,看看以后能不能搞出自己的一个model,好了.不说别的,上代码 1,先上依赖库 compile 'io.reactivex:rxandro ...
- python webdriver 从无到有搭建混合驱动自动化测试框架的过程和总结
一步一步实现混合驱动自动化测试框架的搭建 混合驱动自动化测试框架,是一个非常高级的框架,非常好用,但也很难,不好掌握,需要多练习,就像搭建数据驱动框架一样,需要自己去一点一点的写,一边搭建一边做思路整 ...
随机推荐
- djanogo class meta
1.指定字段名: 在定义字段的时候,增加参数db_column='real_field': 2.指定表名: 在model的class中,添加Meta类,在Meta类中指定表名db_table 例如在某 ...
- MIP 技术交流分享(3月15日)
3月15日下午,MIP 团队工程师与58赶集的 Web 前端工程师进行了一次面对面的技术交流. 在这次交流中,MIP 工程师主要分享了 MIP 技术原理,MIP 加速原理,以及 MIP 为开发者提供的 ...
- 【工具篇】接口测试神器 -- Postman 入门教程
一.Postman概述 (1)工具介绍 Postman是一个接口测试工具,一款非常流行的API调试工具.在做接口测试的时候,Postman相当于一个客户端,它可以模拟用户发起的各类HTTP请求,将请求 ...
- phper的Go之旅(-)--书写前言
前言:由于我使用的主力机是mac,所以整系列教程都以mac为主,后期可能会更新windows,有时间的话,截止写这篇博客的时间我是一个全职php开发工程师,之所以要写这篇 教程原因就是现在技术语言层出 ...
- PHP全栈学习笔记13
php与ajax技术 web2.0的到来,ajax逐渐成为主流,什么是ajax,ajax的开发模式,优点,使用技术.(ajax概述,ajax使用的技术,需要注意的 问题,在PHP应用ajax技术的应用 ...
- 《HelloGitHub》第 33 期
公告 欢迎 点击分享 自荐或发现的开源项目,也可安装 分享插件 更便捷地推荐有趣的开源项目. 小伙伴们,新的一年就要来了,今年的 Bug 改完了吗?先看看<HelloGitHub>最新一期 ...
- Python爬虫入门这一篇就够了
何谓爬虫 所谓爬虫,就是按照一定的规则,自动的从网络中抓取信息的程序或者脚本.万维网就像一个巨大的蜘蛛网,我们的爬虫就是上面的一个蜘蛛,不断的去抓取我们需要的信息. 爬虫三要素 抓取 分析 存储 基础 ...
- 设计模式之适配者模式——Java语言描述 | Amos H's blog
适配器模式是作为两个不兼容的接口之间的桥梁.这种类型的设计模糊属于结构性模式,它结合了两个独立接口的功能 概念阐述 使用适配器模式可以解决在软件系统中,将一些旧的类放入新环境中,但是新环境要求的接口旧 ...
- July 06th. 2018, Week 27th. Friday
Life has no limitations, except the ones you make. 生命无限,除非你自我设限. From Les Brown. There would be no l ...
- mysql 表结构转excel表格
最近需要写文档,由于开发模式是先开发后写文档(不想吐槽...),数据库表结构什么的都搞好了,然后写文档的时候需要贴表结构,什么字段,类型,相关说明需要一一对应起来,数据库表10多张,字段又多,手动复制 ...