📄 basedao.java
字号:
package com.laoer.comm.db;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Query;
/**
* <p>Title: 天乙软件工作室公共包</p>
* <p>Description: 天乙软件工作室公共包</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: 天乙软件工作室[LAOER.COM/TIANYISOFT.NET]</p>
* @author 龚天乙(Laoer)
* @version 1.0
*/
public abstract class BaseDAO {
protected void removeObj(Class c, Long id) throws DAOException {
Session session = null;
net.sf.hibernate.Transaction txc = null;
try {
session = HibernateUtil.currentSession();
txc = session.beginTransaction();
Object obj = session.load(c, id);
session.delete(obj);
txc.commit();
}
catch (Exception e) {
try {
txc.rollback();
}
catch (HibernateException he) {
he.printStackTrace();
}
throw new DAOException(e);
}
finally {
closeSession();
}
}
protected void removeObjs(String queryString) throws DAOException {
Session session = null;
net.sf.hibernate.Transaction txc = null;
try {
session = HibernateUtil.currentSession();
txc = session.beginTransaction();
session.delete(queryString);
txc.commit();
}
catch (HibernateException ex) {
try {
txc.rollback();
}
catch (HibernateException he) {
he.printStackTrace();
}
throw new DAOException(ex);
}
finally {
closeSession();
}
}
protected void removeObjs(String queryString, List values) throws
DAOException {
Session session = null;
net.sf.hibernate.Transaction txc = null;
try {
session = HibernateUtil.currentSession();
txc = session.beginTransaction();
Object[] os = values.toArray();
session.delete(queryString, os, List2Types.getTypes(os));
txc.commit();
}
catch (HibernateException ex) {
try {
txc.rollback();
}
catch (HibernateException he) {
he.printStackTrace();
}
throw new DAOException(ex);
}
finally {
closeSession();
}
}
protected Object retrieveObj(Class c, Long id) throws DAOException {
Object obj = null;
try {
Session session = HibernateUtil.currentSession();
//obj = session.load(c, id);
obj = session.get(c, id);
}
catch (HibernateException he) {
he.printStackTrace();
throw new DAOException(he);
}
finally {
closeSession();
return obj;
}
}
protected Object retrieveObj(String key) throws DAOException {
Object value = null;
return retrieveObj(key, value);
}
protected Object retrieveObj(String key, Object value) throws DAOException {
List objects = retrieveObjs(key, value);
if (objects != null) {
if (objects.size() == 0) {
return null;
}
else {
return objects.get(0);
}
}
else {
return null;
}
}
protected Object retrieveObj(String key, List value) throws DAOException {
List objects = retrieveObjs(key, value);
if (objects != null) {
if (objects.size() == 0) {
return null;
}
else {
return objects.get(0);
}
}
else {
return null;
}
}
protected List retrieveObjs(String queryString) throws DAOException {
Object value = null;
return this.retrieveObjs(queryString, value);
}
protected List retrieveObjs(String queryString, Object value) throws
DAOException {
List values = new ArrayList();
values.add(value);
return retrieveObjs(queryString, values);
}
protected List retrieveObjs(String queryString, List value) throws
DAOException {
try {
Session session = HibernateUtil.currentSession();
Query q = session.createQuery(queryString);
int len = value.size();
if (! (1 == len && null == value.get(0))) {
for (int i = 0; i < len; i++) {
q.setParameter(i, value.get(i));
}
}
List list = q.list();
return list;
}
catch (HibernateException he) {
he.printStackTrace();
throw new DAOException(he);
}
finally {
closeSession();
}
}
protected List retrieveObjs(String queryString, List value, long first,
long number) throws DAOException {
try {
Session session = HibernateUtil.currentSession();
Query q = session.createQuery(queryString);
int len = value.size();
if (! (1 == len && null == value.get(0))) {
for (int i = 0; i < len; i++) {
q.setParameter(i, value.get(i));
}
}
q.setFirstResult( (int) first);
q.setMaxResults( (int) number);
List list = q.list();
return list;
}
catch (HibernateException he) {
he.printStackTrace();
throw new DAOException(he);
}
finally {
closeSession();
}
}
protected List retrieveObjs(String queryString, Object value, long first,
long number) throws DAOException {
List values = new ArrayList();
values.add(value);
return retrieveObjs(queryString, values, first, number);
}
protected List retrieveObjs(String queryString, String p0, List value) throws
DAOException {
try {
if (value == null || value.size() == 0) {
return new ArrayList();
}
Session session = HibernateUtil.currentSession();
Query q = session.createQuery(queryString);
q.setParameterList(p0, value);
List list = q.list();
return list;
}
catch (HibernateException he) {
he.printStackTrace();
throw new DAOException(he);
}
finally {
closeSession();
}
}
protected void storeObj(Object obj) throws DAOException {
Session session = null;
net.sf.hibernate.Transaction txc = null;
try {
session = HibernateUtil.currentSession();
txc = session.beginTransaction();
session.saveOrUpdate(obj);
txc.commit();
}
catch (Exception e) {
try {
txc.rollback();
}
catch (HibernateException hex) {
hex.printStackTrace();
}
throw new DAOException(e);
}
finally {
closeSession();
}
}
protected void closeSession() {
try {
HibernateUtil.closeSession();
}
catch (HibernateException he) {
System.err.println(he.getMessage());
}
}
protected void rollback() throws DAOException {
try {
Session session = HibernateUtil.currentSession();
if (session != null) {
session.connection().rollback();
}
}
catch (HibernateException he) {
throw new DAOException(he);
}
catch (SQLException sqle) {
throw new DAOException(sqle);
}
}
protected long retrieveObjsCount(String queryString) throws DAOException {
List values = new ArrayList();
return retrieveObjsCount(queryString, values);
}
protected long retrieveObjsCount(String queryString, Object value) throws
DAOException {
List values = new ArrayList();
values.add(value);
return retrieveObjsCount(queryString, values);
}
protected long retrieveObjsCount(String queryString, List values) throws
DAOException {
try {
Session session = HibernateUtil.currentSession();
Query q = session.createQuery("select count(*) " + queryString);
int len = values.size();
if (len > 0) {
for (int i = 0; i < len; i++) {
q.setParameter(i, values.get(i));
}
}
return ( (Long) q.iterate().next()).longValue();
}
catch (HibernateException he) {
throw new DAOException(he);
}
finally {
closeSession();
}
}
protected List retrieveObjsByQuery(String queryString, List value,
long first, long max) throws DAOException {
List results = new ArrayList();
Iterator it = null;
try {
Session session = HibernateUtil.currentSession();
Query q = session.createQuery(queryString);
if (value != null) {
int len = value.size();
for (int i = 0; i < len; i++) {
q.setParameter(i, value.get(i));
}
}
q.setFirstResult( (int) first);
q.setMaxResults( (int) max);
it = q.iterate();
while (it.hasNext()) {
results.add(it.next());
}
}
catch (HibernateException he) {
throw new DAOException(he);
}
finally {
closeSession();
return results;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -