package com.cy.utils; import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set; import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry; /**
* Hibernate辅助类
*
* @author renchang
*
*/
public class HibernateUtils { private static Configuration config; // 配置对象
private static ServiceRegistry ser; // 服务
private static SessionFactory sf; // session工厂
private static Session session; // session
private static Transaction transaction; // 事务 /**
* 1.读取配置到Configuration 2.注册服务 3.获得SessionFactory
*/
static {
config = new Configuration().configure();
ser = new StandardServiceRegistryBuilder().applySettings(
config.getProperties()).build();
sf = config.buildSessionFactory(ser);
} /**
* 获得Session
*
* @return session对象
*/
public static Session getSession() {
return session = sf.openSession();
} /**
* 获得事务对象
*
* @return Transaction对象
*/
public static Transaction getTransaction() {
return transaction = session.beginTransaction();
} /**
* 关闭资源
*/
public static void close() {
try {
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}
} /**
* 添加新数据
*
* @param obj
* 待添加的对象
*/
public static void save(Object obj) {
try {
getSession();
getTransaction();
session.save(obj);
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
} finally {
close();
}
} /**
* 修改数据
*
* @param obj
* 待修改的对象
*/
public static void update(Object obj) {
try {
getSession();
getTransaction();
session.update(obj);
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
} finally {
close();
}
} /**
* 添删除数据
*
* @param obj
* 待删除的对象
*/
public static void delete(Object obj) {
try {
getSession();
getTransaction();
session.delete(obj);
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
} finally {
close();
}
} /**
* 按条件插叙数据
*
* @param hql
* 插叙的hql语句
* @param whereMap
* 插叙条件的键值对
* @return 满足条件的对象集合
*/
public static List<?> find(String hql, Map<String, Object> whereMap) {
List<?> objList = null;
try {
getSession();
getTransaction();
getTransaction();
hql = setHql(hql, whereMap);
Query query = session.createQuery(hql);
objList = query.list();
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
} finally {
close();
}
return objList;
} /**
* 根据id获得对象信息
*
* @param cls
* 对象z
* @param id
* id
* @return 对象
*/
public static Object getObjById(Class<?> cls, Serializable id) {
Object obj = null;
try {
getSession();
getTransaction();
obj = session.get(cls, id);
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
} finally {
close();
}
return obj;
} /**
* 分页
*
* @param hql
* 分页语句
* @param pageSize
* 每页大小
* @param pageNo
* 当前页数
* @param whereMap
* 分页条件
* @return 满足条件的数据
*/
public static List<?> paging(String hql, int pageSize, int pageNo,
Map<String, Object> whereMap) {
List<?> objList = null;
try {
getSession();
getTransaction();
getTransaction();
hql = setHql(hql, whereMap);
Query query = session.createQuery(hql);
int beginRow = (pageNo - 1) * pageSize;
query.setFirstResult(beginRow);
query.setMaxResults(pageSize);
objList = query.list();
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
} finally {
close();
}
return objList;
} /**
* 获得条件下总条数
*
* @param hql
* hql语句
* @param whereMap
* 统计的总条数
* @return 总条数
*/
public static int getTotalRows(String hql, Map<String, Object> whereMap) {
int totalRows = 0;
try {
getSession();
getTransaction();
hql = setHql(hql, whereMap);
Query query = session.createQuery(hql);
totalRows = (new Integer(query.uniqueResult().toString()))
.intValue();
} catch (Exception e) {
e.printStackTrace();
} finally {
close();
}
return totalRows;
} /**
* 拼接hql语句
*
* @param hql
* 原始hql语句
* @param whereMap
* 拼接的条件
* @return 拼接后的hql
*/
private static String setHql(String hql, Map<String, Object> whereMap) {
StringBuffer sbHql = new StringBuffer(hql);
if (whereMap != null) {
Set<String> set = whereMap.keySet();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String key = it.next();
sbHql.append(" AND " + key + " LIKE '%" + whereMap.get(key)
+ "%'");
}
}
return sbHql.toString();
}
}