首先我们需要先创建一个案例

构建一个Student 的实体类

private String name;
private Integer age;
private Integer id;

在SRC根目录下构建一个大配置

名称是: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="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost::orcl</property>
<property name="connection.username">sb</property>
<property name="connection.password">sb</property> <!-- 输出所有 SQL 语句到控制台。 -->
<property name="hibernate.show_sql">true</property> <!-- 在 log 和 console 中打印出更漂亮的 SQL。 -->
<property name="hibernate.format_sql">true</property>
<!-- 方言 -->
<property name="hibernate.dialect"> org.hibernate.dialect.Oracle10gDialect</property> <!-- 关联小配置 --> </session-factory> </hibernate-configuration>

大配置

继而在组建一个名为:Student.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 package="cn.happy.entity">
<class name="Student" table="Student">
<id name="id" type="int" column="id">
</id>
<property name="name" type="string" column="name"/>
<property name="age" type="int" column="age"/>
</class>
</hibernate-mapping>

小配置

编写一个测试类:

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before; import cn.happy.entity.Student;
import cn.happy.until.Hibernate_until; public class Test { public static void main(String[] args) {
//addAll();
//delete();
find();
} public static void addAll()
{
Student stu=new Student();
stu.setSid();
stu.setAge();
stu.setName("我是狗"); //获取session对象
Session session = Hibernate_until.getSession();
//开启事务
Transaction tran = session.beginTransaction();
//hibernate保存
session.save(stu);
System.out.println("成功保存!");
tran.commit();
Hibernate_until.closeSession();
}
public static void delete()
{
//打开session
Session session = Hibernate_until.getSession();
//开始一个事务
Transaction tx=session.beginTransaction();
//获取部门的对象
Student stu=(Student)session.get(Student.class, new Integer());
//删除对象(持久化操作)
if(stu!=null)
{
session.delete(stu);
}
try {
//提交事务
tx.commit();
System.out.println("删除成功");
} catch (Exception e) {
//回滚事务
tx.rollback();
System.out.println("删除回滚");
}
Hibernate_until.closeSession();
}
public static void update()
{
Session session = Hibernate_until.getSession();
//开始一个事务
Transaction tx=session.beginTransaction();
//获取部门的对象
Student stu=(Student)session.get(Student.class, new Integer());
//删除对象(持久化操作)
if(stu!=null)
{
stu.setName("武松");
session.update(stu);
}
try {
//提交事务
tx.commit();
System.out.println("删除成功");
} catch (Exception e) {
//回滚事务
tx.rollback();
System.out.println("删除回滚");
}
Hibernate_until.closeSession();
}
public static void find()
{
Session session = Hibernate_until.getSession();
//获取部门的对象
Student stu=(Student)session.get(Student.class, new Integer());
System.out.println(stu);
} }

测试类

上述就是在Hibernate 框架中,第一次实例的书写

初始Hibernate框架的更多相关文章

  1. 初始Hibernate框架技术

    hibernate: 定义:ORM:Object Relational Mapping 对象 关系 映射 使用hibernate时几个必要的: 1.实体类 2.映射文件(类  -数据库表,属性-字段) ...

  2. Hibernate框架之Criteria查询 和注解(重点☆☆☆☆☆,难点☆☆☆)

    写好一篇博客,不是容易的事.原因是:你要给自己以后看的时候,还能看懂,最重要的是当别人看到你的博客文章的时候,也一样很清楚的明白你自己写的东西.其实这也是一种成就感!! 对于每一个知识点,要有必要的解 ...

  3. Struts2,Spring,Hibernate框架的优缺点

    Struts2,Spring,Hibernate框架的优缺点 Struts2框架(MVC框架)的优点如下:         1)  实现了MVC模式,层次结构清晰,使程序员只需关注业务逻辑的实现:   ...

  4. Hibernate框架简介(二)基本使用增、删、改、查

    一.Hibernate框架简介 Hibernate是一个优秀的Java持久化层解决方案,是当今主流的对象-关系映射(ORM,ObjectRelationalMapping)工具 1.1.理解持久化 瞬 ...

  5. Hibernate 系列 01 - 框架技术 (介绍Hibernate框架的发展由来)

    引导目录: Hibernate 系列教程 目录 本篇导航: 为什么学习框架技术 框架的概念 主流框架的介绍 1.为什么学习框架技术 如何制作一份看上去具有专业水准的PPT文档呢?一个简单的方法就是使用 ...

  6. 2.0、Hibernate框架的简单搭建

    一.Hibernate:是一个开放源代码的对象关系映射框架,对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句 ...

  7. 【Hibernate框架】对象的三种持久化状态

    一.综述 hibernate中的对象有三种状态,分别是TransientObjects(瞬时对象).PersistentObjects(持久化对象)和DetachedObjects(托管对象也叫做离线 ...

  8. hibernate框架int和Integer类型区别

    hibernate 框架在定义实体时,int类型最好定义为Inttger类型,因为在注入时int是值类型不允许为空.

  9. SSH(Struts2+Spring+Hibernate)框架搭建流程<注解的方式创建Bean>

    此篇讲的是MyEclipse9工具提供的支持搭建自加包有代码也是相同:用户登录与注册的例子,表字段只有name,password. SSH,xml方式搭建文章链接地址:http://www.cnblo ...

随机推荐

  1. Python&Selenium 数据驱动【unittest+ddt+Excel】

    一.摘要 一般情况下我们为了更好的管理测试数据会选择将测试数据存储在Excel文件当中去,本节内容将展示给读者将测试数据存储在Excel文档中的案例. 二.创建存储测试数据的Excel 创建一个Exc ...

  2. 分享一波目前写的最强的autohotkey 插件

    支持各种软件快速切换,补全括号,代码等!!!!!!!! ;这种全局定义要写在所有代码的前面才能让所有代码起作用. SetCapsLockState , AlwaysOff SetNumlockStat ...

  3. 一个ball例程带你进入 Halcon 世界

    * 此例程来自halcon自带例程,请打开 halcon->ctrl+E 打开例程->搜索框中输入ball added by xiejl* ball.hdev: Inspection of ...

  4. 24、自动装配-@Profile环境搭建

    24.自动装配-@Profile环境搭建 Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能. 开发环境.测试环境.正式环境 数据源切换 24.1 添加 数据源和jdbc驱动 ...

  5. 【题解】[Nwerc 2006]escape -C++

    Description 给出数字N(1<=N<=10000),X(1<=x<=1000),Y(1<=Y<=1000),代表有N个敌人分布一个X行Y列的矩阵上 矩形的 ...

  6. pyecharts v1 版本 学习笔记 散点图

    散点图 基本案例 from example.commons import Faker from pyecharts import options as opts from pyecharts.char ...

  7. Win 7 x64 + Visual Studio 2015为WinXP编译可执行程序

    造冰箱的大熊猫@cnblogs 2019/9/5 本文承接<Win7下使用Visual Studio为WinXP编译可执行文件>一文. - 在64位Win7(开发机)上,编写基于C的Win ...

  8. 外网可以反问lxr

    , 'baseurl_aliases' => 174       [ 'http://172.168.2.4/lxr' 175       , 'http://mydomain/lxr' 176 ...

  9. 推荐 | Vue 入门&进阶路线

    今儿跟大家聊聊 Vue . 不得不承认, Vue 越来越受欢迎了.对比 Angular 和 React,虽然三者都是非常优秀的前端框架,但从 GitHub 趋势看,Vue 已经排在第一位,达到了13万 ...

  10. SpringAOP配置与使用(示例)

    1.pom.xml追加 spring-aspects aspectjrt 为控制器以外的类织入切面 2.新建spring-aop.xml <?xml version="1.0" ...