Hibernate的Dao的封装
来源:互联网 发布:ubuntu 修改apt get源 编辑:程序博客网 时间:2024/05/24 02:32
package cn.com.bochy.dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateUntil<T> {
private SessionFactory sf;
private Session session;
@SuppressWarnings("deprecation")
//1、获取SessionFactory
public SessionFactory getSF(){
//读取Hibernate配置文件
Configuration cfg=new Configuration().configure();
//获取SessionFactory
sf=cfg.buildSessionFactory();
return sf;
}
//2、获取session
public Session getSession(){
getSF();
session=sf.openSession();
return session;
}
@SuppressWarnings("unchecked")
//3、封装成list
public List<T> getList(String hql){
getSession();
Query query=session.createQuery(hql);
List<T> lis=query.list();
closeAll();
return lis;
}
//4、添加
public boolean Save(T obj){
getSession();
Transaction tx=session.beginTransaction();
boolean mark=true;
try {
session.save(obj);
tx.commit();
} catch (Exception e) {
// TODO Auto-generated catch block
mark=false;
e.printStackTrace();
tx.rollback();
}
closeAll();
return mark;
}
//5、修改
public boolean update(T obj){
getSession();
Transaction tx=session.beginTransaction();
boolean mark=true;
try {
session.saveOrUpdate(obj);
tx.commit();
} catch (Exception e) {
// TODO Auto-generated catch block
mark=false;
e.printStackTrace();
tx.rollback();
}
closeAll();
return mark;
}
//6、根据id,获取实体对象
public T getObjectById(int id,Class<T> c){
getSession();
@SuppressWarnings("unchecked")
T obj=(T)session.get(c, id);
closeAll();
return obj ;
}
//7、删除
public boolean delete(T obj){
getSession();
Transaction tx=session.beginTransaction();
boolean mark=true;
try {
session.delete(obj);
tx.commit();
} catch (Exception e) {
// TODO Auto-generated catch block
mark=false;
e.printStackTrace();
tx.rollback();
}
closeAll();
return mark;
}
//8、关闭资源
public void closeAll(){
if(session!=null){
session.close();
}
if(sf!=null){
sf.close();
}
}
}
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateUntil<T> {
private SessionFactory sf;
private Session session;
@SuppressWarnings("deprecation")
//1、获取SessionFactory
public SessionFactory getSF(){
//读取Hibernate配置文件
Configuration cfg=new Configuration().configure();
//获取SessionFactory
sf=cfg.buildSessionFactory();
return sf;
}
//2、获取session
public Session getSession(){
getSF();
session=sf.openSession();
return session;
}
@SuppressWarnings("unchecked")
//3、封装成list
public List<T> getList(String hql){
getSession();
Query query=session.createQuery(hql);
List<T> lis=query.list();
closeAll();
return lis;
}
//4、添加
public boolean Save(T obj){
getSession();
Transaction tx=session.beginTransaction();
boolean mark=true;
try {
session.save(obj);
tx.commit();
} catch (Exception e) {
// TODO Auto-generated catch block
mark=false;
e.printStackTrace();
tx.rollback();
}
closeAll();
return mark;
}
//5、修改
public boolean update(T obj){
getSession();
Transaction tx=session.beginTransaction();
boolean mark=true;
try {
session.saveOrUpdate(obj);
tx.commit();
} catch (Exception e) {
// TODO Auto-generated catch block
mark=false;
e.printStackTrace();
tx.rollback();
}
closeAll();
return mark;
}
//6、根据id,获取实体对象
public T getObjectById(int id,Class<T> c){
getSession();
@SuppressWarnings("unchecked")
T obj=(T)session.get(c, id);
closeAll();
return obj ;
}
//7、删除
public boolean delete(T obj){
getSession();
Transaction tx=session.beginTransaction();
boolean mark=true;
try {
session.delete(obj);
tx.commit();
} catch (Exception e) {
// TODO Auto-generated catch block
mark=false;
e.printStackTrace();
tx.rollback();
}
closeAll();
return mark;
}
//8、关闭资源
public void closeAll(){
if(session!=null){
session.close();
}
if(sf!=null){
sf.close();
}
}
}
阅读全文
0 0
- Hibernate的Dao的封装
- hibernate DAO类的封装范文
- Spring对Hibernate DAO的封装
- 在DAO中对Hibernate的封装(新)
- 讨论:在DAO中对Hibernate的封装
- 在Spring+Hibernate中提供的DAO封装
- Mybatis和hibernate中的DAO层封装的区别
- dao的在封装
- Hibernate的DAO实现
- Hibernate的通用dao
- Hibernate的Dao
- Hibernate的通用dao
- Hibernate的DAO实现
- hibernate的dao层
- hibernate底层dao封装
- 读取配置文件的封装Dao
- SSH的Dao层封装
- 自己扩展封装Hibernate模板DAO-之三Hibernate+JSP的分页功能
- 单片机第一个代码
- XML编辑器oXygen XML Editor V19大版本更新 | 附下载
- 架构设计关注点分离
- TCP三次握手四次挥手
- python 模块安装
- Hibernate的Dao的封装
- 怎么样才可以让你站点关键字在很短的时间里迅速提高
- loop循环插入练习
- 计算机系统(嵌入式类)术语概念
- 【LeetCode】Remove Duplicates from Sorted List系列
- PHP递归简写
- Github 开源:高效好用的对象间属性拷贝工具 升讯威 Mapper:( Sheng.Mapper)
- Spring学习笔记(三十五):关于httpClient的post请求,到后台中文乱码的问题
- Trafodion 删除重复记录