package com.chnu.util;
//import org.hibernate.Session;
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;
//import com.yao.domain.Student;
final public class HibernateUtil {
private static SessionFactory sessionFactory = null;
private static ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
public HibernateUtil(){
}
static{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
//获取全新的session
public static Session openSession(){
return sessionFactory.openSession();
}
//获取和线程关联的session
public static Session getCurrentSession(){
Session session = threadLocal.get();
//判断是否得到
//add by yyy || !session.isOpen()
if(session == null || !session.isOpen()){
session = sessionFactory.openSession();
//session对象设置到threadLocal -- 相当于session和线程绑定
threadLocal.set(session);
}
return session;
}
//提供一个通过Id号返回对象的方法
public static Object queryById(Class clazz, java.io.Serializable id){
Session session = null;
Transaction tx = null;
Object obj = null;
try {
session = getCurrentSession();
tx = session.beginTransaction();
obj = session.load(clazz, id);
tx.commit();
System.out.println("完成");
} catch (Exception e) {
if(tx != null){
tx.rollback();
}
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}finally{
if(session !=null && session.isOpen()){
session.close();
}
}
return obj;
}
//统一的查询语句
public static List executeQuery(String hql, String[] params){
Session session = null;
// Transaction tx = null;
Query query = null;
List list = null;
session = openSession();
try {
// tx = session.beginTransaction();
query = session.createQuery(hql);
if(params!=null && params.length > 0){
for(int i=0; i < params.length; i++){
query.setString(i, params[i]);
}
}
list = query.list();
// tx.commit();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
// TODO: handle exception
}finally{
if(session != null && session.isOpen()){
session.close();
}
}
return list;
}
//统一的分页查询--只得到一个页的结果
public static List queryPage(String hql, String[] params, int pageSize, int currentPage){
Session session = null;
Transaction tx = null;
List list = null;
Query query = null;
session = getCurrentSession();
tx = session.beginTransaction();
query = session.createQuery(hql);
if(params!=null && params.length > 0){
for(int i=0; i < params.length; i++){
query.setString(i, params[i]);
}
}
//增加分页时的设置信息
query.setFirstResult((currentPage-1)*pageSize).setMaxResults(pageSize);
list = query.list();
try {
tx.commit();
} catch (Exception e) {
if(tx != null){
tx.rollback();
}
e.printStackTrace();
throw new RuntimeException(e.getMessage());
// TODO: handle exception
}finally{
if(session != null && session.isOpen()){
session.close();
}
}
return list;
}
/*统一的添加方法*/
public static void save(Object obj){
Session session = null;
Transaction tx = null;
session = getCurrentSession();
tx = session.beginTransaction();
session.save(obj);
try {
tx.commit();
} catch (Exception e) {
if(tx != null){
tx.rollback();
}
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}finally{
if(session != null && session.isOpen()){
session.close();
}
}
}
/*统一的更新删除*/
public static void updateAndDelete(String hql, String[] params){
Session session = null;
Transaction tx = null;
try {
session = getCurrentSession();
tx = session.beginTransaction();
Query query = session.createQuery(hql);
if(params!=null && params.length > 0){
for(int i=0; i<params.length; i++){
query.setString(i, params[i]);
}
}
query.executeUpdate();
tx.commit();
for(String s : params){
System.out.println(s);
}
// System.out.println(params.toString());
System.out.println("完成");
} catch (Exception e) {
if(tx != null){
tx.rollback();
}
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}finally{
if(session !=null && session.isOpen()){
session.close();
}
}
}
/*
* 获取一个对象的方法
*/
public static Object getObjectOnlyOne(String hql, String[] params){
Session session = null;
Transaction tx = null;
Object obj = null;
Query query = null;
try {
session = getCurrentSession();
tx = session.beginTransaction();
query = session.createQuery(hql);
if(params.length != 0){
for(int i=0; i < params.length; i ++){
query.setString(i, params[i]);
}
}
obj = query.uniqueResult();
tx.commit();
} catch (Exception e) {
if(tx != null){
tx.rollback();
}
throw new RuntimeException("查询一个对象方法 "+e.getMessage());
}finally{
if(session != null && session.isOpen()){
session.close();
}
}
return obj;
}
}
11-07
11-07
11-07