码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate工具类

时间:2014-10-23 15:55:22      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:io   os   ar   java   for   sp   on   cti   amp   

package com.cjq.util;

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 final class HibernateUtil {

 private static SessionFactory sessionFactory;

 private HibernateUtil() {

 }

 static {
  sessionFactory = new Configuration().configure().buildSessionFactory();
 }

 /**
  * 得到会话工厂
  *
  * @return
  */
 public static SessionFactory getSessionFactory() {
  return sessionFactory;
 }

 /**
  * 查询方法
  */
 public static List executeQuery(String hql, String[] parameters) {
  Transaction tx = null;
  Session session = null;
  List list = null;
  try {
   session = sessionFactory.getCurrentSession();
   tx = session.beginTransaction();
   Query query = session.createQuery(hql);
   if (parameters != null && parameters.length > 0) {
    for (int i = 0; i < parameters.length; i++) {
     query.setString(i, parameters[i]);
    }
   }
   list = query.list();
   tx.commit();
  } catch (Exception e) {
   if (tx != null) {
    tx.rollback();
   }
   throw new RuntimeException(e.getMessage());
  } finally {
   if (session != null && session.isOpen()) {
    session.close();
   }
  }
  return list;

 }

 /**
  * 添加
  */
 public static void saveObject(Object obj) {
  Transaction tx = null;
  Session session = null;
  try {
   session = sessionFactory.getCurrentSession();
   tx = session.beginTransaction();
   session.save(obj);
   tx.commit();
  } catch (Exception e) {
   if (tx != null) {
    tx.rollback();
    throw new RuntimeException(e.getMessage());
   }
  } finally {
   if (session != null && session.isOpen()) {
    session.close();
   }
  }
 }

 /**
  * 修改删除方法
  *
  * @param hql
  * @param parameters
  */
 public static void excuteUpdate(String hql, String[] parameters) {
  Transaction tx = null;
  Session session = null;
  try {
   session = sessionFactory.getCurrentSession();
   tx = session.beginTransaction();
   Query query = session.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) {
   if (tx != null) {
    tx.rollback();
    throw new RuntimeException(e.getMessage());
   }
  } finally {
   if (session != null && session.isOpen()) {
    session.close();
   }
  }
 }

 /**
  * 分页
  */
 public static List excuteQueryByPaginPage(String hql, String[] parameters,
   int pageSize, int pageNow) {
  Transaction tx = null;
  Session session = null;
  List list = null;
  try {
   session = sessionFactory.getCurrentSession();
   tx = session.beginTransaction();
   Query query = session.createQuery(hql);
   if (parameters != null && parameters.length > 0) {
    for (int i = 0; i < parameters.length; i++) {
     query.setString(i, parameters[i]);
    }
   }
   query.setFirstResult((pageNow - 1) * pageSize).setMaxResults(
     pageSize);
   query.list();
   tx.commit();
  } catch (Exception e) {
   if (tx != null) {
    tx.rollback();
    throw new RuntimeException(e.getMessage());
   }
  } finally {
   if (session != null && session.isOpen()) {
    session.close();
   }
  }
  return list;
 }

}

Hibernate工具类

标签:io   os   ar   java   for   sp   on   cti   amp   

原文地址:http://www.cnblogs.com/java1234/p/4045829.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!