Hibernate 帮助类
来源:互联网 发布:淘宝手机壳店铺 编辑:程序博客网 时间:2024/05/27 02:29
HibernateUtils.java
import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtils {private static final String path="hibernate.cfg.xml"; private static final Configuration cfg = new Configuration(); private static SessionFactory sf=null; private ThreadLocal<Session> thread =new ThreadLocal<Session>(); static{ cfg.configure(path); if(sf==null){ sf=cfg.buildSessionFactory(); } } public Session getSession(){ Session s=thread.get(); if(s==null){ s=sf.openSession(); thread.set(s); } return s; } public void CloseSession(){ Session s =thread.get(); if(s!=null){ s.close(); } }}BaseDao.java
import java.util.List;import org.hibernate.HibernateException;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.Transaction;public class BaseDao extends HibernateUtils{//添加 public boolean add(Object obj){ Session s=null; Transaction t=null; try {s=super.getSession(); t=s.beginTransaction(); s.save(obj); t.commit(); return true;} catch (HibernateException e) { t.rollback();}finally{this.CloseSession();} return false; } //修改 public boolean update(Object obj){ Session s=null; Transaction t=null; try {s=super.getSession(); s.update(obj); t.commit(); return true;} catch (HibernateException e) {// TODO Auto-generated catch blockt.rollback();} return false; } //通过Id删除 public boolean delete(int id){ Session s=null; Transaction t=null; try {s=super.getSession(); s.delete(id); t.commit(); return true;} catch (HibernateException e) { t.rollback(); } return false; } //查询所有 public List getAll(String hql){ Session s=null; s=this.getSession(); Query q =s.createQuery(hql); List list =q.list(); return list; } //通过Id查询 public Object getById(Class cls,int id){ Session s=null; s=this.getSession(); try { Object o =s.get(cls, new Integer(id)); return o;} catch (HibernateException e) {e.printStackTrace();}finally{super.CloseSession();} return null; } }
- Hibernate 帮助类
- HibernateUt Hibernate帮助类 -----Hibernate_2
- Hibernate中使用帮助类获取session
- hibernate的一些帮助文档
- Hibernate、3.6.7在线帮助文档
- hibernate(反向生成)配置帮助
- 字符串帮助类
- (转)字符串帮助类
- Excel 帮助类
- JSONHelper JSON帮助类
- ChartsUtil jfreeChart 帮助类
- .Net文件夹帮助类
- socket 通讯 帮助类
- ENUM帮助类
- PHPExcel 类库帮助
- Dictionary帮助类
- SqlHelper 帮助类
- C#时间帮助类
- Struts2 标签详解
- Android中String.xml: The reference to entity "timestamp" must end with the ';' delimiter
- JFrame全屏设置
- Linux下通过php对Mysql中的数据进行定时统计并入库
- 什么情况下用宏定义do{}while(0);这种结构
- Hibernate 帮助类
- DP与贪心算法的一点浅见
- 设置 OSD可以在 Windows start 上显示
- 气死男生的对话
- 动作类测试
- Codeforces Round #181 (Div. 2) 部分解题报告
- ant与yui压缩静态文件
- C#数据查询帮助类
- Codeforce#181 div2 C,E--problem 300E,300C