hibernate的hql帮助类
import java.io.Serializable; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.omg.Dynamic.Parameter; public class HibernateHelper { static SessionFactory sf=null; static{ Configuration cf=new Configuration().configure(); sf=cf.buildSessionFactory(); } //1、得到session。 public static Session getcurrentsession(){ Session session=sf.getCurrentSession(); return session; } public static Session opensession(){ Session session=sf.openSession(); return session; } //2、load提供一个返回一个对象的方法 public static Object findbyid(Class clazz, Serializable id){ Session s=HibernateHelper.getcurrentsession(); Transaction ts=s.beginTransaction(); Object obj=null; try { obj=s.load(clazz, id); //提交事务 ts.commit(); } catch (HibernateException e) { // TODO Auto-generated catch block if (ts!=null) { ts.rollback(); } e.printStackTrace(); }finally{ if (s!=null&&s.isOpen()) { s.close(); } return obj; } } //3、统一的一个修改和删除(批量 hql) hql"delete upate ...??" public static void executeUpdate(String hql,String [] parameters){ Session s=null; Transaction tx=null; try { s=opensession(); tx=s.beginTransaction(); Query query=s.createQuery(hql); //先判断是否有参数要绑定 if(parameters!=null&& parameters.length>0){ for(int i=0;i<parameters.length;i++){ query.setString(i, parameters[i]); } } query.executeUpdate(); tx.commit(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); // TODO: handle exception }finally{ if(s!=null&&s.isOpen()){ s.close(); } } } //4、统一的添加的方法 public static void save(Object obj){ Session s=null; Transaction tx=null; try { s=opensession(); tx=s.beginTransaction(); s.save(obj); tx.commit(); } catch (Exception e) { if(tx!=null){ tx.rollback(); } throw new RuntimeException(e.getMessage()); // TODO: handle exception }finally{ if(s!=null && s.isOpen()){ s.close(); } } } //5、只返回一个数据 public static String executeQuery_onepara(String hql,String parameter[]){ String s=null; //创建session Session session=HibernateHelper.getcurrentsession(); //创建事务 Transaction ts=session.beginTransaction(); try { Query q=session.createQuery(hql); if (parameter!=null&¶meter.length>0) { for (int i = 0; i < parameter.length; i++) { q.setString(i, parameter[i]); } } s=(String)q.uniqueResult(); //提交事务 ts.commit(); } catch (HibernateException e) { // TODO Auto-generated catch block if (ts!=null) { ts.rollback(); } e.printStackTrace(); }finally{ if (session!=null&&session.isOpen()) { session.close(); } } return s; } //6、只返回一个对象 public static Object executeQueryForObject(String hql,String parameter[]){ Object o=null; //创建session Session session=HibernateHelper.getcurrentsession(); //创建事务 Transaction ts=session.beginTransaction(); try { Query q=session.createQuery(hql); if (parameter!=null&¶meter.length>0) { for (int i = 0; i < parameter.length; i++) { q.setString(i, parameter[i]); } } o=q.uniqueResult(); //提交事务 ts.commit(); } catch (HibernateException e) { // TODO Auto-generated catch block if (ts!=null) { ts.rollback(); } e.printStackTrace(); }finally{ if (session!=null&&session.isOpen()) { session.close(); } } return o; } //7、分页 //提供提议分页 //通过参数绑定进行查询, public static List executeQuery_bypage(String hql,String parameter[],int pagesize,int pagenow){ List list=null; //创建session Session session=HibernateHelper.getcurrentsession(); //创建事务 Transaction ts=session.beginTransaction(); try { Query q=session.createQuery(hql); if (parameter!=null&¶meter.length>0) { for (int i = 0; i < parameter.length; i++) { q.setString(i, parameter[i]); } } q.setFirstResult((pagenow-1)*pagesize).setMaxResults(pagesize); list=q.list(); //提交事务 ts.commit(); } catch (HibernateException e) { // TODO Auto-generated catch block if (ts!=null) { ts.rollback(); } e.printStackTrace(); }finally{ if (session!=null&&session.isOpen()) { session.close(); } } return list; } //通过参数绑定进行查询, public static List executeQuery_common_currentSe_setParameter(String hql,String parameter[]){ List list=null; //创建session Session session=HibernateHelper.getcurrentsession(); //创建事务 Transaction ts=session.beginTransaction(); try { Query q=session.createQuery(hql); if (parameter!=null&¶meter.length>0) { for (int i = 0; i < parameter.length; i++) { q.setString(i, parameter[i]); } } list=q.list(); //提交事务 ts.commit(); } catch (HibernateException e) { // TODO Auto-generated catch block if (ts!=null) { ts.rollback(); } e.printStackTrace(); }finally{ if (session!=null&&session.isOpen()) { session.close(); } } return list; } }
相关推荐
LetonLIU 2020-03-28
tugangkai 2020-07-04
taisenki 2020-06-02
zzjmay 2020-05-17
shuiluobu 2020-03-20
solarspot 2014-06-27
LetonLIU 2019-12-23
东方咖啡屋 2019-12-08
浮生 2011-12-12
surpassdream 2011-12-11
keruis 2015-02-03
小黄鸡kimhuhg 2015-02-16
Lingforme 2015-07-23
bigcactus 2012-05-21
张浩BIT 2012-04-22
虽然我很菜 2016-01-08
WJI 2014-11-06
kobexing 2014-07-16
crazzylp 2014-01-08