初始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 ...
随机推荐
- python excel基本操作
#coding=utf-8 ''' excel基本操作 ''' from openpyxl import Workbook wb=Workbook() ws1=wb.create_sheet('sh1 ...
- python 学习笔记_2 模拟socket编程 服务端、客户端通信(参考核心编程2代码实现)
服务器端代码实现: #!/usr/bin/env python#coding=gbk'''接收客户端字符串,在字段串前面打上当前时间,然后返回server端采用 python2 linux下调试运行客 ...
- django--没有整理,笔记
https://docs.djangoproject.com/en/2.2/常用的数据路命令:python manage.py makemigrations 数据移植准备python manage.p ...
- Django REST framework+Vue 打造生鲜电商项目(笔记二)
(转自https://www.cnblogs.com/derek1184405959/p/8768059.html)(有修改) 接下来开始引入django resfulframework,体现它的强大 ...
- 控制warning信息在控制台的显示
在运行代码时,有时出现warning信息, 1.当你后台不需要warning信息的时候,可以直接把warning信息省略掉. 2.如果代码是循环,则会在控制台打印多次warning信息,这会使得war ...
- js校验密码必须包含字母大小写、数字
校验密码必须包含字母大小写.数字 function checkPasswordNew(s){ var str=trim(s); //var reg = /^(?![A-Z]+$)(?![a-z]+$) ...
- 银联刷卡POS机冲正
冲正是为系统认为可能交易失败时采取的补救手法. 即一笔交易在终端已经置为成功标志,但是发送到主机的账务交易包没有得到响应,即终端交易超时,所以不确定该笔交易是否在主机端也成功完成,为了确保用户的利益, ...
- webpack - 优化阻塞渲染的css
随着浏览器的日新月异,网页的性能和速度越来越好,并且对于用户体验来说也越来越重要. 现在有很多优化页面的办法,比如:静态资源的合并和压缩,code splitting,DNS预读取等等. 本文介绍的是 ...
- reGeorg(不需要外网ip的代理)
reGeorg _____ ______ __|___ |__ ______ _____ _____ ______ | | | ___|| ___| || ___|/ \| | | ___| | \ ...
- Redis集群模式之分布式集群模式
前言 Redis集群模式主要有2种: 主从集群 分布式集群. 前者主要是为了高可用或是读写分离,后者为了更好的存储数据,负载均衡. 本文主要讲解主从集群.本章主要讲解后一半部分,Redis集群. 与本 ...