hibernate 增删查改

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();
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值