import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class BaseData {
private static final Log log = LogFactory.getLog(BaseData.class);
Session session;
Transaction tx;
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
/**
* 执行HQL语句查询
* @param hql
* @return
* @throws Exception
*/
public List queryByHql(String hql)throws Exception{
List list = null;
try {
list = session.createQuery(hql).list();
}catch (Exception e) {
log.error("系统加载数据时出错!");
log.error(e.getStackTrace());
throw new Exception();
}finally{
//关闭Session
session.close();
}
return list;
}
/**
* 执行SQL语句查询
* @param hql
* @return
* @throws Exception
*/
public List queryBySql(String sql)throws Exception{
List list = null;
try {
list = session.createSQLQuery(sql).list();
}catch (Exception e) {
log.error("系统加载数据时出错!");
log.error(e.getStackTrace());
throw new Exception();
}finally{
//关闭Session
session.close();
}
return list;
}
/**
* 查询所有记录
* @param obj
* @return
* @throws Exception
*/
public List queryAll(Class cls)throws Exception{
List list = null;
try {
list = session.createCriteria(cls).list();
}catch (Exception e) {
log.error("系统加载数据时出错!");
log.error(e.getStackTrace());
throw new Exception();
}finally{
//关闭Session
session.close();
}
return list;
}
/**
* 通过ID查询
* @param cls
* @param id
* @return
* @throws Exception
*/
public Object queryById(Class cls, Serializable id)throws Exception{
Object obj = null;
try {
obj = session.get(cls,id);
}catch (Exception e) {
log.error("系统加载数据时出错!");
log.error(e.getStackTrace());
throw new Exception();
}finally{
//关闭Session
session.close();
}
return obj;
}
/**
* 通过参数查询
* @param hql
* @param params
* @return
* @throws Exception
*/
public Object get(String hql, Serializable params) throws Exception {
Object obj = null;
try {
obj = session.load(hql, params);
}catch (Exception e) {
log.error("系统加载数据时出错!");
log.error(e.getStackTrace());
throw new Exception();
}finally{
//关闭Session
session.close();
}
return obj;
}
//保存对象
public void saveData(Object obj) throws Exception {
try {
// 开始事务
tx = session.beginTransaction();
// 保存对象
session.save(obj);
//提交事务
commitDate();
}catch (Exception e) {
//事物回滚
rollbackDate();
log.error("系统加载数据时出错!");
log.error(e.getStackTrace());
throw new Exception();
}finally{
//关闭session
closeSession();
}
}
//更新对象
public void updateData(Object obj) throws Exception {
try {
// 开始事务
tx = session.beginTransaction();
// 更新对象
session.update(obj);
//提交事务
commitDate();
}catch (Exception e) {
//事物回滚
rollbackDate();
log.error("系统加载数据时出错!");
log.error(e.getStackTrace());
throw new Exception();
}finally{
//关闭session
closeSession();
}
}
//删除对象
public void deleteData(Object obj) throws Exception {
try {
// 开始事务
tx = session.beginTransaction();
// 删除对象
session.delete(obj);
//提交事务
commitDate();
}catch (Exception e) {
//事物回滚
rollbackDate();
log.error("系统加载数据时出错!");
log.error(e.getStackTrace());
throw new Exception();
}finally{
//关闭session
closeSession();
}
}
/**
* 多事务操作
* @throws Exception
*/
public void operatorData(List list)throws Exception{
try {
// 开始事务
tx = session.beginTransaction();
HashMap map = (HashMap)list.get(0);
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
String key = (String)entry.getKey();
if(key.indexOf("insert")!=-1){
session.save(entry.getValue());
}
else if(key.indexOf("update")!=-1){
session.update(entry.getValue());
}
else if(key.indexOf("delete")!=-1){
session.delete(entry.getValue());
}
}
//提交事物
commitDate();
}catch(Exception e){
//事物回滚
e.printStackTrace();
rollbackDate();
log.error("系统加载数据时出错!");
//log.error(e.getStackTrace());
throw new Exception();
}finally{
//关闭session
closeSession();
}
}
/**
* 多表操作事务操作
* @throws Exception
*/
public void operatorData(List insertlist,List updateList,List delList)throws Exception{
try {
// 开始事务
tx = session.beginTransaction();
if(insertlist != null && insertlist.size()>0){
for(int i =0; i<insertlist.size();i++){
session.save(insertlist.get(i));
}
}
if(updateList != null && updateList.size()>0){
for(int i =0; i<updateList.size();i++){
session.update(updateList.get(i));
}
}
if(delList != null && delList.size()>0){
for(int i =0; i<delList.size();i++){
session.delete(delList.get(i));
}
}
//提交事物
commitDate();
}catch(Exception e){
//事物回滚
e.printStackTrace();
rollbackDate();
log.error("系统加载数据时出错!");
//log.error(e.getStackTrace());
throw new Exception();
}finally{
//关闭session
closeSession();
}
}
//提交事务
public void commitDate() throws Exception{
tx.commit();
}
//回滚事务
public void rollbackDate() throws Exception{
tx.rollback();
}
//关闭session
public void closeSession() throws Exception{
session.close();
}
Hibernate
最新推荐文章于 2024-09-10 10:21:38 发布