初始Hibernate框架
首先我们需要先创建一个案例
构建一个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框架的更多相关文章
- 初始Hibernate框架技术
hibernate: 定义:ORM:Object Relational Mapping 对象 关系 映射 使用hibernate时几个必要的: 1.实体类 2.映射文件(类 -数据库表,属性-字段) ...
- Hibernate框架之Criteria查询 和注解(重点☆☆☆☆☆,难点☆☆☆)
写好一篇博客,不是容易的事.原因是:你要给自己以后看的时候,还能看懂,最重要的是当别人看到你的博客文章的时候,也一样很清楚的明白你自己写的东西.其实这也是一种成就感!! 对于每一个知识点,要有必要的解 ...
- Struts2,Spring,Hibernate框架的优缺点
Struts2,Spring,Hibernate框架的优缺点 Struts2框架(MVC框架)的优点如下: 1) 实现了MVC模式,层次结构清晰,使程序员只需关注业务逻辑的实现: ...
- Hibernate框架简介(二)基本使用增、删、改、查
一.Hibernate框架简介 Hibernate是一个优秀的Java持久化层解决方案,是当今主流的对象-关系映射(ORM,ObjectRelationalMapping)工具 1.1.理解持久化 瞬 ...
- Hibernate 系列 01 - 框架技术 (介绍Hibernate框架的发展由来)
引导目录: Hibernate 系列教程 目录 本篇导航: 为什么学习框架技术 框架的概念 主流框架的介绍 1.为什么学习框架技术 如何制作一份看上去具有专业水准的PPT文档呢?一个简单的方法就是使用 ...
- 2.0、Hibernate框架的简单搭建
一.Hibernate:是一个开放源代码的对象关系映射框架,对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句 ...
- 【Hibernate框架】对象的三种持久化状态
一.综述 hibernate中的对象有三种状态,分别是TransientObjects(瞬时对象).PersistentObjects(持久化对象)和DetachedObjects(托管对象也叫做离线 ...
- hibernate框架int和Integer类型区别
hibernate 框架在定义实体时,int类型最好定义为Inttger类型,因为在注入时int是值类型不允许为空.
- SSH(Struts2+Spring+Hibernate)框架搭建流程<注解的方式创建Bean>
此篇讲的是MyEclipse9工具提供的支持搭建自加包有代码也是相同:用户登录与注册的例子,表字段只有name,password. SSH,xml方式搭建文章链接地址:http://www.cnblo ...
随机推荐
- Reverse链表
之前学习了关于reverse数组相关的东东(http://www.cnblogs.com/webor2006/p/6727419.html),这次再来对链表进行reverse一下,在面试中也很容易被问 ...
- mysql打开报错2013解决办法
修改mysql配置文件 在[mysqld]下面设置skip-name-resolve 重启mysql from :https://www.jb51.net/article/52637.htm
- git rev-list 按照时间来列出两个 commit id 之间的相差数
git rev-list 按照时间来列出两个 commit id 之间的相差数 git rev-list: Lists commit objects in reverse chronological ...
- 开发神技能 | Python Mock 的入门
Mock是什么 Mock这个词在英语中有模拟的这个意思,因此我们可以猜测出这个库的主要功能是模拟一些东西.准确的说,Mock是Python中一个用于支持单元测试的库,它的主要功能是使用mock对象替代 ...
- Java锁--框架
根据锁的添加到Java中的时间,Java中的锁,可以分为"同步锁"和"JUC包中的锁". 同步锁 即通过synchronized关键字来进行同步,实现对竞争资源 ...
- 同一网断多套keepalived,vrid冲突问题排查
注意: 同一网段中virtual_router_id的值不能重复,否则会出错,相关错误信息如下. Keepalived_vrrp[27120]: ip address associated with ...
- Codeforces Round #584 C. Paint the Digits
链接: https://codeforces.com/contest/1209/problem/C 题意: You are given a sequence of n digits d1d2-dn. ...
- python自动华 (二)
Python自动化 [第二篇]:Python基础-列表.元组.字典 本节内容 模块初识 .pyc简介 数据类型初识 数据运算 列表.元组操作 字符串操作 字典操作 集合操作 字符编码与转码 一.模块初 ...
- JavaScript有趣的知识点
JavaScript中总有一些有趣的小知识,而且又是很容易犯错的.我把我遇到的慢慢罗列一下,方便大家避坑 typeof(null)返回的结果是 object " "变成布尔类型为t ...
- 富文本编辑器word
tinymce是很优秀的一款富文本编辑器,可以去官网下载.https://www.tiny.cloud 这里分享的是它官网的一个收费插件powerpaste的旧版本源码,但也不影响功能使用. http ...