20160505-hibernate入门2
public class User {
private int id;
private String name;
private Date birthDay;
//getter setter…
}
Configuration cfg = new Configuration();
cfg.configure(“config.cfg.xml”);
// 也可以通过cfg.setProperty设置属性。
SessionFactory sessionFactory = cfg.buildSessionFactory();
所以将以上代码封装成工具类:
HibernateUtils.java
package com.dzq.utils; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; public class HibernateUntils {
private static SessionFactory sessionFactory; private HibernateUntils() { } static {
Configuration cfg = new Configuration();
cfg.configure();//如果不是hibernate.cfg.xml这个文件名,需要加上文件名
sessionFactory = cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
public static Session getSession(){
return sessionFactory.openSession();
}
}
public static void addUser(User user){
Session s=null;
Transaction tx=null;
try {
s=HibernateUntils.getSession();
tx=s.beginTransaction();
s.save(user);
tx.commit();
} catch (Exception e) {
if(tx!=null)
tx.rollback();
throw new RuntimeException(e);
}finally{
if(s!=null){
s.close();
}
}
}

package com.dzq.utils; import java.io.Serializable; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; public class HibernateUntils {
private static SessionFactory sessionFactory; private HibernateUntils() { } static {
Configuration cfg = new Configuration();
cfg.configure();//如果不是hibernate.cfg.xml这个文件名,需要加上文件名
sessionFactory = cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
public static Session getSession(){
return sessionFactory.openSession();
} /**
* 添加
* @param entity
*/
public static void add(Object entity) {
Session s = null;
Transaction tx = null;
try {
s = HibernateUntils.getSession();
tx = s.beginTransaction();
s.save(entity);
tx.commit();
} catch (Exception e) {
if (tx != null)
tx.rollback();
throw new RuntimeException(e);
} finally {
if (s != null) {
s.close();
}
}
}
/**
* 修改
* @param entity
*/
public static void update(Object entity) {
Session s = null;
Transaction tx = null;
try {
s = HibernateUntils.getSession();
tx = s.beginTransaction();
s.update(entity);
tx.commit();
} catch (Exception e) {
if (tx != null)
tx.rollback();
throw new RuntimeException(e);
} finally {
if (s != null) {
s.close();
}
}
} /**
* 删除
* @param entity
*/
public static void delete(Object entity) {
Session s = null;
Transaction tx = null;
try {
s = HibernateUntils.getSession();
tx = s.beginTransaction();
s.delete(entity);
tx.commit();
} catch (Exception e) {
if (tx != null)
tx.rollback();
throw new RuntimeException(e);
} finally {
if (s != null) {
s.close();
}
}
} /**
* 根据主键id查询
* @param clazz
* @param id
* @return
*/
public static Object get(Class clazz,Serializable id) {
Session s = null;
try {
s = HibernateUntils.getSession();
Object obj=s.get(clazz, id);
return obj;
} finally {
if (s != null) {
s.close();
}
}
}
}
20160505-hibernate入门2的更多相关文章
- 三大框架之hibernate入门
hibernate入门 1.orm hibernate是一个经典的开源的orm[数据访问中间件]框架 ORM( Object Relation Mapping)对象关 ...
- Hibernate入门案例及增删改查
一.Hibernate入门案例剖析: ①创建实体类Student 并重写toString方法 public class Student { private Integer sid; private I ...
- Hibernate入门案例 增删改
一.Hibernate入门案例剖析: ①创建实体类Student 并重写toString方法 public class Student { private Integer sid; private I ...
- Hibernate入门6.Hibernate检索方式
Hibernate入门6.Hibernate检索方式 20131128 代码下载 链接: http://pan.baidu.com/s/1Ccuup 密码: vqlv Hibernate的整体框架已经 ...
- Hibernate入门5持久化对象关系和批量处理技术
Hibernate入门5持久化对象关系和批量处理技术 20131128 代码下载 链接: http://pan.baidu.com/s/1Ccuup 密码: vqlv 前言: 前面学习了Hiberna ...
- Hibernate入门4.核心技能
Hibernate入门4.核心技能 20131128 代码下载 链接: http://pan.baidu.com/s/1Ccuup 密码: vqlv 前言: 前面学习了Hibernate3的基本知识, ...
- Hibernate入门3.配置映射文件深入
Hibernate入门3.配置映射文件深入 2013.11.27 前言: 之前的两节是在Java项目中如何使用hibernate,并且通过一个简单地项目实践,期间有很多的错误,一般都是因为配置包的问题 ...
- 简单的Hibernate入门简介
其实Hibernate本身是个独立的框架,它不需要任何web server或application server的支持.然而,大多数的Hibernate入门介绍都加入了很多非Hibernate的东西, ...
- Hibernate入门(1)-第一个Hibernate程序
Hibernate入门(1)-第一个Hibernate程序 Hibernate是最著名的ORM工具之一,本系列文章主要学习Hibernate的用法,不涉及Hibernate的原理.本文介绍第一个Hib ...
- hibernate入门之person表
下面的hibernate入门person表指的是:根据mysql数据库中的test表和其中的元素-->建立映射表==>进而创建持久化类的顺序来操作了,下面为步骤 1.配置MySQL驱动程序 ...
随机推荐
- HDU-4619 Warm up 2 二分匹配
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4619 一看就知道是二分匹配题目,对每个点拆点建立二分图,最后答案除2.因为这里是稀疏图,用邻接表处理. ...
- [Tutorial] Using the RasPi as a WiFi hostspot
http://www.raspberrypi.org/forums/viewtopic.php?f=36&t=19120 http://wireless.kernel.org/en/users ...
- 问题-Tbutton(sender) 与 (sender as Tbutton) 等价吗?
问题:Tbutton(sender) 与 (sender as Tbutton) 等价吗? 答: 1. Sender As TButton时delphi做类型检查. 比如: var frm:TFo ...
- A Tour of Go Exercise: Slices
Implement Pic. It should return a slice of length dy, each element of which is a slice of dx 8-bit u ...
- [iOS基础控件 - 6.7.1] 微博展示 代码
Controller: // // ViewController.m // Weibo // // Created by hellovoidworld on 14/12/4. // Copyrig ...
- [iOS基础控件 - 4.2] APP列表 字典转模型Model
A.使用字典加载数据的缺点 1.用户自行指定key,容易出错 2.存入.取出都需要key,容易混乱 B.模型 (MVC中的model) 1.字典与模型对比: (1)字典:存储数据,通过字符串类型的 ...
- os7新特性之生成二维码
先导入CoreImage.framework 生成二维码 读取二维码
- Linq知识小结
Linq语法小结:有两种形式的语法可供我们在写Linq查询时使用,分别是“查询语法”.“方法语法”.1)先看个列子,有个直观认识 int[] arr = { 12, 2,45,34,23,18 ...
- linux下hexdump和od命令:显示文件十六进制格式
Linux指令: od 示例用法: od -c hello Linux指令: od od命令用户通常使用od命令查看特殊格式的文件内容.通过指定该命令的不同选项可以以十进制.八进制.十六进 ...
- MySQL5日期类型DATETIME和TIMESTAMP相关问题详解
MySQL5日期类型DATETIME和TIMESTAMP相关问题详解 MySQL5的日期类型有三种:DATETIME.DATE和TIMESTAMP,除了DATE用来表示一个不带时分秒的是日期,另外两个 ...