hibernate详细教程(入门到熟练)
一、下载hibernate开发环境
1.https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/
2.创建一个项目,首先引入jar包。
3. 创建数据库表
CREATE TABLE `customer` (
`cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
`cust_name` varchar(32) COLLATE utf8_unicode_ci NOT NULL COMMENT '客户名称(公司名称)',
`cust_source` varchar(32) COLLATE utf8_unicode_ci DEFAULT 'null' COMMENT '客户信息来源',
`cust_industry` varchar(32) COLLATE utf8_unicode_ci DEFAULT 'null' COMMENT '客户所属行业',
`cust_level` varchar(32) COLLATE utf8_unicode_ci DEFAULT 'null' COMMENT '客户级别',
`cust_phone` varchar(64) COLLATE utf8_unicode_ci DEFAULT 'null' COMMENT '固定电话',
`cust_mobile` varchar(16) COLLATE utf8_unicode_ci DEFAULT 'null' COMMENT '移动电话',
PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
4. 创建实体类Customer
private int 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;
5. 创建映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.itheima.bean">
<class name="Customer" table="customer">
<id name="cust_id">
<!-- generator:主键生成策略 -->
<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>
6.创建一个Hibernate的核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 配置数据库驱动 -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- 配置数据库连接URL -->
<property name="connection.url">
jdbc:mysql://localhost:3306/cst_cuntmoer
</property>
<!-- 数据库user -->
<property name="connection.username">root</property>
<!-- 数据库user密码 -->
<property name="connection.password">admine</property>
<!-- 配置JDBC内置连接池 -->
<property name="connection.pool_size">1</property>
<!-- 配置数据库方言 -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- 输出运行时生成的SQL语句 -->
<property name="show_sql">true</property>
<!-- 配置对输出的SQL语句进行格式化 -->
<property name="format_sql">true</property>
<!-- 配置映射文件 -->
<mapping resource="com/itheima/bean/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
6. 映射的配置(重要)
【class标签的配置】
标签用来建立类与表的映射关系
属性:
name :类的全路径
table :表名(类名与表名一致,table可以省略)
catalog :数据库名
【id标签的配置】
标签用来建立类中的属性与表中的主键的对应关系
属性:
name :类中的属性名
column :表中的字段名(类中的属性名和表中的字段名如果一致,column可以省略)
length :长度
type :类型
【property标签的配置】
标签用来建立类中的普通属性与表的字段的对应关系
属性:
name :类中的属性名
column :表中的字段名
length :长度
type :类型
not-null :设置非空
unique :设置唯一
7. 核心配置文件(了解,自行百度即可,hibernate.cfg.xml)
必须的配置
连接数据库的基本的参数
驱动类
url路径
用户名
密码
方言
可选的配置
显示SQL :hibernate.show_sql
格式化SQL :hibernate.format_sql
自动建表 :hibernate.hbm2ddl.auto
none :不使用hibernate的自动建表
create :如果数据库中已经有表,删除原有表,重新创建,如果没有表,新建表。(测试)
create-drop :如果数据库中已经有表,删除原有表,执行操作,删除这个表。如果没有表,新建一个,使用完了删除该表。(测试)
update:如果数据库中有表,使用原有表,如果没有表,创建新表(更新表结构)
validate:如果没有表,不会创建表。只会使用数据库中原有的表。(校验映射和表结构)。
映射文件的引入
引入映射文件的位置
8. 编写测试类
package com.itheima.hibernate.demo1;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import com.itheima.bean.Customer;
import com.itheima.utils.HibernateUtils;
/**
* 入门案例
*
* @author csh
*
*/
public class HibernateDemo1 {
/**
* 增加操作
*/
@Test
public void demo1() {
// 加载核心配置文件
Configuration configuration = new Configuration().configure();
// 创建一个session工厂,类似于jdbc连接池
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 获取session对象
Session session = sessionFactory.openSession();
// 手动开启事务
Transaction tranction = session.beginTransaction();
// 编写代码
Customer customer = new Customer();
customer.setCust_name("杨玉环");
session.save(customer);
tranction.commit();
session.close();
}
/**
* 修改操作
*/
@Test
public void demo2() {
// 加载核心配置文件
Session session = HibernateUtils.OpenSession();
Transaction tx = session.beginTransaction();
Customer customer = session.get(Customer.class, 1);
customer.setCust_name("暗杀回电话");
session.update(customer);
tx.commit();
session.close();
}
/**
* 删除操作
*/
@Test
public void demo3() {
// 加载核心配置文件
Session session = HibernateUtils.OpenSession();
Transaction tx = session.beginTransaction();
Customer customer = session.get(Customer.class, 1);
session.delete(customer);
tx.commit();
session.close();
}
/**
* 保存或更新操作
*/
@Test
public void demo4() {
// 加载核心配置文件
Session session = HibernateUtils.OpenSession();
Transaction tx = session.beginTransaction();
Customer customer = new Customer();
customer.setCust_id(1);
customer.setCust_name("刘鹗恶化");
session.saveOrUpdate(customer);
tx.commit();
session.close();
}
}
/**
* 查询所有
*/
@Test
public void demo5() {
// 加载核心配置文件
Session session = HibernateUtils.OpenSession();
Transaction tx = session.beginTransaction();
//接受HQL语句(面向对象查询语言)
/* Query query2 = session.createQuery("from Custonmer");
List<Customer> list2 = query2.list();
for (Customer customer : list2) {
System.out.println(customer);
}*/
//接受Sql语句
SQLQuery query = session.createSQLQuery("select * from customer");
List<Object[]> list = query.list();
for (Object[] object : list) {
System.out.println(Arrays.toString(object));
}
tx.commit();
session.close();
}
HibernateUtils工具类:
package com.itheima.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* hibernate抽取工具类
* @author csh
*
*/
public class HibernateUtils {
private static final Configuration cfg;
private static final SessionFactory sf;
static {
cfg= new Configuration().configure();
sf = cfg.buildSessionFactory();
}
public static Session OpenSession() {
return sf.openSession();
}
}
9. Transaction:事务对象
Hibernate中管理事务的对象。
l commit();
rollback();(一般在出错后面)
hibernate详细教程(入门到熟练)相关推荐
- Spring详细教程入门(一)
1. Spring 的概述 1.1 什么是 Spring pring 是一个开源框架,Spring 是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson在其著作 Exp ...
- mock详细教程入门这一篇就够了(*)
前言 1.什么是mock测试 Mock 测试就是在测试活动中,对于某些不容易构造或不容易获取的比较复杂的数据/场景,用一个虚拟的对象(Mock对象)来创建用于测试的测试方法. 2.为什么要进行Mock ...
- 《Kafka系列》Kafka详细教程入门
Kafka 1 消息队列--消息中间件 1.1 消息队列的作用 1.2 消息队列的概念--MQ Message 在互联网中,多台设备产生通信的数据的总称:可以是视频.文本.音频等等. Quene 一种 ...
- Redis详细教程入门
Redis 1 互联网架构演变 1.1 单一数据库 在90年代,由于互联网没有普及.一个网站访问量并不大,单一数据库完全能够满足访问的需求.因为在哪个年下几乎都是静态的页面,动态交互型的功能不多. ...
- 《数据同步-NIFI系列》Nifi详细教程入门-01概念
Nifi概念 1.1 Nifi介绍 Apache Nifi是Apache基金会的顶级项目之一,是一个易于使用,功能强大且可靠的处理和分发数据的系统.使用Nifi可以自动化管理系统间的数据,支持从多 ...
- spring入门详细教程(五)
前言 本篇紧接着spring入门详细教程(三),建议阅读本篇前,先阅读第一篇,第二篇以及第三篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/ ...
- 手把手带你入门前端工程化——超详细教程
授权自@谭光志 链接:https://segmentfault.com/a/1190000037752931,也可点击阅读原文 本文将分成以下 7 个小节: 技术选型 统一规范 测试 部署 监控 性能 ...
- Spring入门详细教程(四)
前言 本篇紧接着spring入门详细教程(三),建议阅读本篇前,先阅读第一篇,第二篇以及第三篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/ ...
- Spring入门详细教程(三)
前言 本篇紧接着spring入门详细教程(二),建议阅读本篇前,先阅读第一篇和第二篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/p/101 ...
- Spring入门详细教程(二)
前言 本篇紧接着spring入门详细教程(一),建议阅读本篇前,先阅读第一篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/p/1016553 ...
最新文章
- oracle中 关于触发器,oracle 闪回关于触发器的bug
- HBase 在京东人资数据预处理平台中的实践!
- GitHub基本使用
- 序列化shelve模块
- 生成最简单的验证码图片的Java代码
- mysql数据库改成支持中文_修改Mysql编码支持中文
- OCR识别之图像预处理
- 上车,在北京地铁10号线偶遇一下
- 开源微信商城java源码_微信小程序商城(java后台源码)
- CCF CSP历年题解(python)
- 北京建筑大学计算机学院岑孝鹏,北京建筑大学
- 小勇机器人如何绑定_App Store 上的“小勇机器人”
- 自动化软件测试工作内容,「自动化测试」自动化测试岗位职责(职位描述,任职要求)...
- 开课吧JAVA高级架构师怎么样_开课吧JavaEE企业级高级架构师
- CUDA软件架构—网格(Grid)、线程块(Block)和线程(Thread)的组织关系以及线程索引的计算公式
- android 仿微信聊天气泡显示图片,怎么实现微信聊天时的气泡图(一)
- 在Windows Server 2008和Vista中增加恢复菜单WinRE
- pandora k2p
- 基于Python的车辆管理系统
- 做人,别伤人,别骗人,别负人!