Hibernate 工具类:
HibernateUtil:
//统一查询;
public static List executeQuery(String hql,String[] parameters){
Session session = null;
Transaction tx = null;
List list = null;
try {
session = HibernateUtil.getCurrentSession();
tx = session.beginTransaction();
Query query = session.createQuery(hql);
//先判断是否有参数需要绑定
if(parameters!=null && parameters.length>0){
for(int i=0;i query.setString(i, parameters[i]); } } list = query.list(); tx.commit(); } catch (Exception e) { // TODO: handle exception if(tx!=null){ tx.rollback(); throw new RuntimeException(e.getMessage()); } }finally{ if(session!=null && session.isOpen()){ session.close(); } } return list; } 主函数测试: //统一查询; /* String hql = "from Student as s "; List Iterator while(iterator.hasNext()){ Student student = iterator.next(); System.out.println(student.getSid()+";" +student.getSdept()+";" +student.getSname()+";" +student.getSsex()+";" +student.getSage()+";" +student.getSaddress() ); } */ /* String hql = "from Student as s where sname=? and sage=?"; String[] parameters = {"nn","13"}; List // Iterator // while(iterator.hasNext()){ // Student student = iterator.next(); // System.out.println(student.getSname()+";;"+student.getSsex()); // } for (Student student : list) { System.out.println(student.getSid()+";" +student.getSdept()+";" +student.getSname()+";" +student.getSsex()+";" +student.getSage()+";" +student.getSaddress() ); } */ //统一的添加 public static void save(Object obj){ Session session = null; Transaction tx = null; try { session = HibernateUtil.getCurrentSession(); tx = session.beginTransaction(); session.save(obj); tx.commit(); } catch (Exception e) { // TODO: handle exception if(tx!=null){ tx.rollback(); throw new RuntimeException(e.getMessage()); } }finally{ if(session!=null && session.isOpen()){ session.close(); } } } 主函数测试: /* //统一添加; Student student = new Student(); student.setSname("dasb"); student.setSage(99); student.setSaddress("china"); student.setSdept("sd"); student.setSsex("ns"); HibernateUtil.save(student); */ //统一删除和更改 public static void executeUpdate(String hql,String[] parameters){ Session session = null; Transaction tx = null; try { session = HibernateUtil.getCurrentSession(); tx = session.beginTransaction(); Query query = session.createQuery(hql); //先判断是否有参数需要绑定 if(parameters!=null && parameters.length>0){ for(int i=0;i query.setString(i, parameters[i]); } } query.executeUpdate(); tx.commit(); } catch (Exception e) { // TODO: handle exception if(tx!=null){ tx.rollback(); throw new RuntimeException(e.getMessage()); } }finally{ if(session!=null && session.isOpen()){ session.close(); } } } //主函数测验 /* //统一更改 String hql="update Student set sname = ? where sname = ?"; String [] parameters = {"nn","sb"}; HibernateUtil.executeUpdate(hql, parameters); //统一删 String hql="delete Student where sname = ?"; String [] parameters = {"nn"}; HibernateUtil.executeUpdate(hql, parameters); */ //分页查询 显示当前页 public static List showResultByPage(String hql_rowC,String hql_pagC,String[] parameters,int pageNow,int pageSize){ //设置分页变量 pageSize为每页显示的内容数量 // int pageNow = 1;//当前页 int pageCount = 1;//页数需要计算出 int rowCount = 1;//总数, Session session = null; Transaction tx = null; List list = null; try { session = HibernateUtil.getCurrentSession(); tx = session.beginTransaction(); //查询出rowCount // String hql = "select count(*) from Student as s "; rowCount = Integer.parseInt( session.createQuery(hql_rowC).uniqueResult().toString()); pageCount = (rowCount-1)/pageSize+1;//计算出页数 System.out.println(pageCount+","+pageSize+","+rowCount); //"from Student as s"=hql_pagC; //显示当前页的信息 // for(int i=1;i<=pageCount;i++){ System.out.println("-------第"+pageNow+"页------"); Query query = session.createQuery(hql_pagC); //先判断是否有参数需要绑定 if(parameters!=null && parameters.length>0){ for(int j=0;j query.setString(j, parameters[j]); } } query.setFirstResult((pageNow-1)*pageSize) .setMaxResults(pageSize).list(); list = query.list(); // } tx.commit(); } catch (Exception e) { // TODO: handle exception if(tx!=null){ tx.rollback(); throw new RuntimeException(e.getMessage()); } }finally{ if(session!=null && session.isOpen()){ session.close(); } } return list; } 主函数测验: /* //显示当前页,分页 String hql_rowC = "select count(*) from Student as s "; // String hql_pagC = "from Student as s where sage>?"; // String[] parameters = {"3"}; String hql_pagC = "from Student as s order by sage"; String[] parameters = null; int pageNow = 2; int pageSize = 3; List for (Student student : list) { System.out.println(student.getSid()+".." +student.getSname()+".." +student.getSage()+".." +student.getSsex()+".." +student.getSdept()+".." +student.getSaddress()+".."); } */ //分页查询 public static void showResultByPage(int pageSize){ //设置分页变量 pageSize为每页显示的内容数量 int pageNow = 1;//当前页 int pageCount = 1;//页数需要计算出 int rowCount = 1;//总数, Session session = null; Transaction tx = null; try { session = HibernateUtil.getCurrentSession(); tx = session.beginTransaction(); //查询出rowCount String hql = "select count(*) from Student as s "; rowCount = Integer.parseInt( session.createQuery(hql).uniqueResult().toString()); pageCount = (rowCount-1)/pageSize+1;//计算出页数 System.out.println(pageCount+","+pageSize+","+rowCount); //显示每一页的信息 for(int i=1;i<=pageCount;i++){ System.out.println("-------第"+i+"页------"); List .setFirstResult((i-1)*pageSize) .setMaxResults(pageSize).list(); for (Student student : list) { System.out.println(student.getSname()+".."+student.getSage()); } } tx.commit(); } catch (Exception e) { // TODO: handle exception if(tx!=null){ tx.rollback(); throw new RuntimeException(e.getMessage()); } }finally{ if(session!=null && session.isOpen()){ session.close(); } } }