1.Hibernate框架简述

Hibernate的核心组件
在基于MVC设计模式的JAVA WEB应用中,Hibernate可以作为模型层/数据访问层。它通过配置文件(hibernate.properties或hibernate.cfg.xml)和映射文件(***.hbm.xml)把JAVA对象或PO(Persistent Object,持久化对象)映射到数据库中的数据库,然后通过操作PO,对数据表中的数据进行增,删,改,查等操作。
除配置文件,映射文件和持久化类外,Hibernate的核心组件包括以下几部分:
a)Configuration类:用来读取Hibernate配置文件,并生成SessionFactory对象。
b)SessionFactory接口:产生Session实例工厂。
c)Session接口:用来操作PO。它有get(),load(),save(),update()和delete()等方法用来对PO进行加载,保存,更新及删除等操作。它是Hibernate的核心接口。
d)Query接口:用来对PO进行查询操。它可以从Session的createQuery()方法生成。
e)Transaction接口:用来管理Hibernate事务,它主要方法有commit()和rollback(),可以从Session的beginTrancation()方法生成。

Persistent Object
持久化对象可以是普通的Javabeans,惟一特殊的是它们与(仅一个)Session相关联。JavaBeans在Hibernate中存在三种状态:
1.临时状态(transient):当一个JavaBean对象在内存中孤立存在,不与数据库中的数据有任何关联关系时,那么这个JavaBeans对象就称为临时对象(Transient Object)。
2.持久化状态(persistent):当一个JavaBean对象与一个Session相关联时,就变成持久化对象(Persistent Object)
3.脱管状态(detached):在这个Session被关闭的同时,这个对象也会脱离持久化状态,就变成脱管状态(Detached Object),可以被应用程序的任何层自由使用,例如可以做与表示层打交道的数据舆对象(Data Transfer Object)。

Hibernate的运行过程
Hibernate的运行过程如下:
A:应用程序先调用Configration类,该类读取Hibernate的配置文件及映射文件中的信息,并用这些信息生成一个SessionFactpry对象。
B:然后从SessionFactory对象生成一个Session对象,并用Session对象生成Transaction对象;可通过Session对象的get(),load(),save(),update(),delete()和saveOrUpdate()等方法对PO进行加载,保存,更新,删除等操作;在查询的情况下,可通过Session对象生成一个Query对象,然后利用Query对象执行查询操作;如果没有异常,Transaction对象将 提交这些操作结果到数据库中。

Hibernate的运行过程如下图:

2.入门案例

01.准备各种jar包(我想就不用我教了吧)

02.准备学生实体类(用于操作对应数据库)

package cn.zhang.entity;
//实体类
public class Student { private int stuno; private String stuname; private int stuage; private int stuid; private int stuseat; public Student() {
super();
// TODO Auto-generated constructor stub
} public Student(String stuname, int stuage, int stuid, int stuseat) {
super();
this.stuname = stuname;
this.stuage = stuage;
this.stuid = stuid;
this.stuseat = stuseat;
} public Student(int stuno, String stuname, int stuage, int stuid, int stuseat) {
super();
this.stuno = stuno;
this.stuname = stuname;
this.stuage = stuage;
this.stuid = stuid;
this.stuseat = stuseat;
} public int getStuid() {
return stuid;
} public void setStuid(int stuid) {
this.stuid = stuid;
} public int getStuseat() {
return stuseat;
} public void setStuseat(int stuseat) {
this.stuseat = stuseat;
} public int getStuno() {
return stuno;
} public void setStuno(int stuno) {
this.stuno = stuno;
} public String getStuname() {
return stuname;
} public void setStuname(String stuname) {
this.stuname = stuname;
} public int getStuage() {
return stuage;
} public void setStuage(int stuage) {
this.stuage = stuage;
} }

03.在src下设计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> <!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">zhangzong</property>
<property name="connection.password">123</property> <!-- SQL dialect (SQL 方言)-->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property> <!-- Echo all executed SQL to stdout 在控制台打印后台的SQL语句-->
<property name="show_sql">true</property> <!-- 格式化显示SQL -->
<property name="format_sql">true</property> <!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> --> <!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</property> --> <!-- Disable the second-level cache -->
<!-- <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>--> <mapping resource="cn/zhang/entity/Student.hbm.xml" /> </session-factory> </hibernate-configuration>

04.在实体类下设计映射文件Student.hbm.xml(在配置文件hibernate.cfg.xml使用)

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.zhang.entity">
<class name="Student" table="stuinfo">
<id name="stuno" column="stuno">
<!-- 主键生成策略:native:
native:如果后台是Oracle
后台是MySQL,自动应用自增 -->
<generator class="native"/>
</id>
<property name="stuname" type="string" column="stuname"/>
<property name="stuage"/> <property name="stuid" type="int" column="stuid"/>
<property name="stuseat"/>
</class> </hibernate-mapping>

05.添加测试类

001.更新(新增)一个学生记录

package cn.zhang.test;
//新增一条数据
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session; import cn.zhang.entity.Student; public class InsertTest { public static void main(String[] args) {
//准备对象
Student student=new Student("光衣", 12,112333,2);//Student.hbm.xml已配置编号为自增,所以这里不用添加编号了
//读取大配置文件,获取要连接的数据库信息
Configuration configuration=new Configuration().configure();
//创建SessionFactory
SessionFactory factory = configuration.buildSessionFactory();
//加工session
Session openSession = factory.openSession(); Transaction beginTransaction = openSession.beginTransaction();
openSession.save(student); beginTransaction.commit(); System.out.println("成功"); } }

002.修改一个学生信息

package cn.zhang.test;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session; import cn.zhang.entity.Student; public class UpdateTest { /**
* @param args
*/
public static void main(String[] args) {
//1.读取大配置文件,获取要连接的数据库信息
Configuration conf=new Configuration().configure();
//2.创建SessionFactory
SessionFactory factory =conf.buildSessionFactory();
//3加工session
Session session = factory.openSession();
Transaction tx=session.beginTransaction();
//获取对象
Student stu =new Student(1,"光衣", 12,112333,2);
//更新
session.update(stu);
//提交事务
tx.commit();
System.out.println("更新成功"); } }

003.删除一个指定学生信息

package cn.zhang.test;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session; import cn.zhang.entity.Student;
public class DeleteTest { public static void main(String[] args) {
//1.读取大配置文件,获取要连接的数据库信息
Configuration conf=new Configuration().configure();
//2.创建SessionFactory
SessionFactory factory =conf.buildSessionFactory();
//3.加工session
Session session = factory.openSession(); Transaction tx=session.beginTransaction();
//获取对象
Student stu =new Student();
stu.setStuno(3);//指定要删除的编号
//删除指定
session.delete(stu);
//提交事务
tx.commit();
System.out.println("删除成功"); } }

004.查询一个指定学生信息

package cn.zhang.test;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session; import cn.zhang.entity.Student; public class SelectTest { public static void main(String[] args) {
//1.读取大配置文件,获取要连接的数据库信息
Configuration conf=new Configuration().configure();
//2.创建SessionFactory
SessionFactory factory =conf.buildSessionFactory();
//3.打开session
Session session = factory.openSession();
//4.加载数据操作
//如果表中没有你指定的主键列,get()方法的是null
Student student =(Student)session.get(Student.class, 4);
//如果表中没有你指定的主键列,程序运行到student.getStuname()时会抛出异常
//Student student =(Student)session.load(Student.class, 4);
//5.输出数据
System.out.println(student.getStuname());
//6.关闭session
session.close(); } }

Hibernate框架之入门的更多相关文章

  1. Hibernate框架之入门案例

    今天终于开始学习了三大框架的其中一个框架,Hibernate框架,在这里不去讲Hibernate框架的一些基础概念了,直接切入代码,带大家了解一下Hibernate能干什么, Hibernate的人们 ...

  2. Hibernate 框架入门

    接着上一篇的 Hibernate 框架的了解,我们就继续学习 Hibernate 框架.这次就进入 Hibernate 框架的入门学习. 首先在学习 Hibernate 框架之前,我们要准备好我们需要 ...

  3. Hibernate框架进阶(上篇)

    导读 前面一片文章介绍了Hibernate框架的入门,主要是讲解Hibernate的环境搭建和简单测试,有兴趣的童鞋出门左转.本文在入门的基础上进行Hibernate的进阶讲解,分为上中下三篇,本篇为 ...

  4. Hibernate框架入门

    导读 本文主要介绍hibernate的入门,主要包括以下内容:hibernate介绍.hibernate环境搭建.hibernate简单测试.测试涉及的api详解. 一.hibernate介绍 JDB ...

  5. Java - Struts框架教程 Hibernate框架教程 Spring框架入门教程(新版) sping mvc spring boot spring cloud Mybatis

    https://www.zhihu.com/question/21142149 http://how2j.cn/k/hibernate/hibernate-tutorial/31.html?tid=6 ...

  6. Hibernate 框架入门(一)

    1. SSH Web 层: Struts2 业务层: Spring 持久层: Hibernate 2. Hibernate 概述 概述 Hibernate 是一个对象关系映射框架(ORM 框架); 对 ...

  7. Hibernate入门第一讲——Hibernate框架的快速入门

    Hibernate框架的概述 什么是框架? 框架指的是软件的半成品,已经完成了部分功能. JavaEE开发的三层架构 了解框架的基本概念之后,我们就来看看Hibernate框架处于JavaEE开发的经 ...

  8. hibernate框架的简单入门

    1.什么是框架 框架是一个半成品,框架帮我们实现了一部分的功能. 2.使用框架的最大好处 使用框架的最大好处就是,少写一部分代码但仍能实现我们所需要实现的功能. 3.什么是hiberbnate框架 ( ...

  9. Hibernate框架学习(一)——入门

    一.框架是什么 1.框架是用来提高开发效率的 2.封装好了一些功能,我们需要使用这些功能时,调用即可,不需要手动实现 3.框架可以理解成一个半成品的项目,只要懂得如何驾驭这些功能即可 二.hibern ...

随机推荐

  1. Linux流量监控工具-iftop教程

    Linux流量监控工具-iftop教程http://automationqa.com/forum.php?mod=viewthread&tid=2854&fromuid=2

  2. Untracked files不想add

    $ git status On branch feature/20160420_complain_630222 Untracked files: (use "git add <file ...

  3. 一点一滴之NHibernate

    之前介绍了Dapper,速度很快,很轻量,很好用. 但是Dapper其实有自己的弊端,比如在数据关系复杂,数据库表非常多,多数据库支持,数据库结构变动频繁的时候总是很无奈.尽管有代码生成器,但是代码生 ...

  4. Win10系统下编译GDAL1.9.2版本

    环境说明: 1.Win10企业版.64位: 2.VS2012旗舰版: 3.GDAL1.9.2 GADL编译 1.解压GDAL压缩包至F:\GDAL\gdal-1.9.2: 2.设置GDAL编译后安装目 ...

  5. Android通过编码实现GPS开关

    在Android 2.2以后才可使用 import android.content.ContentResolver; import android.content.Context; import an ...

  6. JS思维之路菜鸟也能有大能量(2)--模拟数组合并concat

    我们有两个这样的数组 var arr1 = [1,2,3]; var arr2 = [4,5,6]; 任务:合并成这样,请至少提供两种思路. var arr1 = [1,2,3,4,5,6]; 思路一 ...

  7. webgame设计之功能模块的代理模式

    原文地址:http://chengduyi.com/blog/?post=27 在游戏设计中,通常会将一些实现了具体功能的模块进行封装,达到重用的目的.这些功能模块包括:1.网络通信模块(实现连接,断 ...

  8. AEM - Adobe CMS 扒坑记之始

    AEM是Adobe公司所出的商业内容管理系统,全称阿豆比体验管理系统(Adobe Experience Manager),其前身叫CQ,分别有CQ5 CQ6两个大版本.它提供了整套的网站内容管理系统解 ...

  9. [Python] Search navigation in Pycharm

    From: http://blog.csdn.net/u013088062/article/details/50323393 From: http://blog.csdn.net/u013088062 ...

  10. Linux shell tee指令学习

    转载自:http://blog.163.com/xujian900308@126/blog/static/12690761520129911304568/   tee tee:读取标准输入的数据,并将 ...