package com.dj.baseDao;
import java.util.Collection;
import java.util.List;
import org.hibernate.Session;
import java.util.Iterator;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.hibernate.factory.HibernateSessionFactory;
public class BaseDAO {
private SessionFactory sessionFactory;
/**
* 功能:获得数据库连接的Session
* @return Session
*/
public Session getSession() {
return HibernateSessionFactory.14:20 2008-9-1
}
/**
* 功能:关闭session
* @param session
*/
protected void closeSession(Session session) {
if (session != null && session.isOpen()) {
session.close();
session = null;
}
}
/**
* 功能:添加新的记录
* @param entity 映射类
* @return true 添加记录成功
* false 添加记录失败
*/
public boolean add(Object entity) {
Session session = null;
Transaction tr = null;
boolean commitflag = false;
try {
session = this.getSession();
tr = session.beginTransaction();
session.save(entity);
tr.commit();
commitflag = true;
session.flush();
session.clear();
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (!commitflag) {
try {
if (tr != null)
tr.rollback();
} catch (Exception e1) {
e1.printStackTrace();
}
}
this.closeSession(session);
}
return true;
}
/**
* 功能:删除记录
* @param entity 映射类
* @return true 删除成功
* false删除失败
*/
public boolean delete(Object entity) {
Session session = null;
Transaction tr = null;
boolean commitflag = false;
try {
session = this.getSession();
tr = session.getTransaction();
tr.begin();
session.delete(entity);
tr.commit();
commitflag = true;
session.flush();
session.clear();
} catch (Exception e) {
if (tr != null)
tr.rollback();
System.out.println(" Class=" + entity.getClass().getName()+e);
return false;
} finally {
if (!commitflag) {
try {
if (tr != null)
tr.rollback();
} catch (Exception e) {
e.printStackTrace();
}
}
this.closeSession(session);
}
return true;
}
/**
* 通过LONG类型ID 删除一条数据库记录
* @param clazz 映射类.class
* @param id PK 值
* @return true 删除成功
* false 删除失败
*/
public boolean delete(Class clazz, long id) {
try {
Object entity = this.getByPk(clazz, id);
if (entity != null) {
this.delete(entity);
} else {
System.out.println(clazz.getName() + " 的关键字为 " + id+ " 的对象不存在 ");
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 通过int类型ID 删除一条数据库记录
* @param clazz 映射类.class
* @param id PK 值
* @return true 删除成功
* false 删除失败
*/
public boolean delete(Class clazz, int id) {
try {
Object entity = this.getByPk(clazz, id);
if (entity != null) {
this.delete(entity);
} else {
System.out.println(clazz.getName() + " 的关键字为 " + id+ " 的对象不存在 ");
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 通过String类型ID 删除一条数据库记录
* @param clazz 映射类.class
* @param id PK 值
* @return true 删除成功
* false 删除失败
*/
public boolean delete(Class clazz, String id) {
try {
Object entity = this.getByPk(clazz, id);
if (entity != null) {
this.delete(entity);
} else {
System.out.println(clazz.getName() + " 的关键字为 " + id+ " 的对象不存在 ");
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 删除某个表的所有数据
* @param clazz 实体类.class
* @return true 删除成功
* false 删除失败
*/
public boolean deleteTableData(Class entity) {
Session session = null;
Transaction tr = null;
boolean commitflag = false;
try {
session = this.getSession();
tr = session.beginTransaction();
Query query = session.createQuery(" delete from " + entity.getName());
query.executeUpdate();
tr.commit();
commitflag = true;
session.flush();
} catch (Exception e) {
System.out.println("从数据库中删除" + entity.getName() + "的所有实例失败!"+ e);
return false;
} finally {
if (!commitflag) {
try {
if (tr != null)
tr.rollback();
} catch (Exception e) {
e.printStackTrace();
}
}
this.closeSession(session);
}
return true;
}
/**
* 通过实体类集合 删除集合中的所有数据
* @param entities 实体类的集合(list vector Set等)
* @return true 删除成功 false 删除失败
*/
public boolean deleteByEntities(Collection entities) {
Session session = null;
Transaction tr = null;
boolean commitflag = false;
try {
session = this.getSession();
tr = session.beginTransaction();
Iterator ite = entities.iterator();
while (ite.hasNext()) {
session.delete(ite.next());
}
tr.commit();
commitflag = true;
session.flush();
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (!commitflag) {
try {
if (tr != null)
tr.rollback();
} catch (Exception e) {
e.printStackTrace();
}
}
this.closeSession(session);
}
return true;
}
/**
* 通过HQL查询数据
* @param strhql
* @return list
*/
public List find(String strhql) {
List result = null;
System.out.println("find(String) queryString=" + strhql);
Session session = null;
try {
session = this.getSession();
Query query = session.createQuery(strhql);
result = query.list();
} catch (Exception e) {
System.out.println("执行数据库中查询时失败,语句为:" + strhql+ e);
} finally {
this.closeSession(session);
}
return result;
}
/**
* 通过SQL查询数据
* @param strsql sql
* @return list
*/
public List findBySql(String strsql) {
Session session = null;
List result = null;
try {
session = this.getSession();
SQLQuery query = session.createSQLQuery(strsql);
result = query.list();
} catch (Exception e) {
e.printStackTrace();
} finally {
this.closeSession(session);
}
return result;
}
/**
* 功能:通过PK获取数据
* @param 实体类.class
* @param id PK
* @return entity
*/
public Object getByPk(Class clazz, int id) {
Object result = null;
Session session = null;
try {
session = this.getSession();
result = session.get(clazz, new Integer(id));
} catch (Exception e) {
e.printStackTrace();
} finally {
this.closeSession(session);
}
return result;
}
/**
* 功能:通过PK获取数据
* @param 实体类.class
* @param id PK
* @return entity
*/
public Object getByPk(Class clazz, long id) {
System.out.println("getByPk(Class, Long) Class=" + clazz.getName()+ ",id=" + id);
Object result = null;
Session session = null;
try {
session = this.getSession();
result = session.get(clazz, new Long(id));
} catch (Exception e) {
e.printStackTrace();
} finally {
this.closeSession(session);
}
return result;
}
/**
* 功能:通过PK获取数据
* @param 实体类.class
* @param id PK
* @return entity
*/
public Object getByPk(Class clazz, String id) {
System.out.println("getByPk(Class, String) Class=" + clazz.getName() + ",id=" + id);
Object result = null;
Session session = null;
try {
session = this.getSession();
result = session.get(clazz, id);
} catch (Exception e) {
e.printStackTrace();
} finally {
this.closeSession(session);
}
return result;
}
- 1
- 2
前往页