package com.lcsssh.dao.base;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.lcsssh.bo.PageInfo;
public class HibernateBaseDAO extends HibernateDaoSupport implements BaseDao {
/**
* 根据主键ID查询对象
*
* @param c
* @param id
* @return Object
*/
public Object getById(Class c, Serializable id) {
return this.getHibernateTemplate().get(c, id);
}
/**
* 查询一个类的所有对象
*
* @param c
* @return List
*/
public List getAll(Class c) {
return this.getHibernateTemplate().loadAll(c);
}
/**
* 保存或者更新一个对象 当对象为临时状态时就保存对象 当对象为持久化或者游离状态时就更新对象
*
* @param o
*/
public void saveOrUpdate(Object o) {
this.getHibernateTemplate().saveOrUpdate(o);
}
/**
* 移除一个对象
*
* @param o
*/
public void delete(Object o) {
this.getHibernateTemplate().delete(o);
}
/**
* 使用给定的HQL字符串查询
*
* @param strHQL
* @return List
*/
public List findByHQL(final String strHQL) {
return this.getSession().createQuery(strHQL).list();
}
/**
* 根据给定的HQL语句与映射条件查询 用于复杂的查询语句 我们在代入查询参数的时候, 建议使用 name里面放的是HQL语句 eg: "from
* Courses where name = :name" map里面放的是你想绑定的参数实现集 eg: Map map = new
* HashMap(); map.put("name", "Tom"); 最后得到的结果就是 from Courses where name =
* Tom
*
* @param strHQL
* @param mapping
* @return List
*/
public List findByHQL(final String strHQL, final Map mappings) {
HibernateCallback hibernateCallback = new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(strHQL);
// 如果映射条件中不为空,则取出各条件拼接HQL语句
if (mappings != null || !mappings.isEmpty()) {
Iterator it = mappings.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
setParameterValues(query, (String) entry.getKey(),
entry.getValue());
}
}
List list = query.list();
return list;
}
};
return (List) this.getHibernateTemplate().execute(hibernateCallback);
}
/**
* 得到分页查询的记录
*
* @param hql
* @param values
* @param qEntity
* @return List
* @throws DataAccessException
*/
public List getQueryResult(final String strHQL, final Map mappings,
final PageInfo pageInfo) throws DataAccessException {
HibernateCallback hibernateCallback = new HibernateCallback() {
public Object doInHibernate(Session s) throws HibernateException,
SQLException {
String q = strHQL;
Query query = s.createQuery(q);
// 调用此方法向query对象中绑定查询参数
if (mappings != null && mappings.size() > 0) {
for (Iterator i = mappings.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
setParameterValues(query, (String) entry.getKey(), entry
.getValue());
}
}
// 分页控制 (根据当前的页数来得到要现在的记录集)
query.setFirstResult((pageInfo.getCurrentPage() - 1)
* pageInfo.getPerPageRows());
query.setMaxResults(pageInfo.getPerPageRows());
List list = query.list();
return list;
}
};
return this.getHibernateTemplate().executeFind(hibernateCallback);
}
/**
* 根据传入的查询语句的到记录的条数
*
* @param hql
* @param values
* @return int
* @throws DataAccessException
*/
public int getQueryCount(final String strHQL, final Map mappings)
throws DataAccessException {
HibernateCallback hibernateCallback = new HibernateCallback() {
public Object doInHibernate(Session s) throws HibernateException,
SQLException {
int fromIndex = strHQL.indexOf("from");
String q = "select count(*) " + strHQL.substring(fromIndex);
Query query = s.createQuery(q);
// 设置参数
if (mappings != null && mappings.size() > 0) {
for (Iterator i = mappings.entrySet().iterator(); i
.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
setParameterValues(query, (String) entry.getKey(),
entry.getValue());
}
}
Integer queryCount = (Integer) query.uniqueResult();
return queryCount;
}
};
return ((Integer) this.getHibernateTemplate().execute(hibernateCallback)).intValue();
}
/**
* 自动匹配数据类型
*
* @param query
* @param key
* @param value
*/
private void setParameterValues(Query query, String key, Object value) {
if (null == key || null == value) {
return;
} else if (value instanceof Boolean) {
query.setBoolean(key, ((Boolean) value).booleanValue());
} else if (value instanceof String) {
query.setString(key, (String) value);
} else if (value instanceof Integer) {
query.setInteger(key, ((Integer) value).intValue());
} else if (value instanceof Long) {
query.setLong(key, ((Long) value).longValue());
} else if (value instanceof Float) {
query.setFloat(key, ((Float) value).floatValue());
} else if (value instanceof Double) {
query.setDouble(key, ((Double) value).doubleValue());
} else if (value instanceof BigDecimal) {
query.setBigDecimal(key, (BigDecimal) value);
} else if (value instanceof Byte) {
query.setByte(key, ((Byte) value).byteValue());
} else if (value instanceof Calendar) {
query.setCalendar(key, (Calendar) value);
} else if (value instanceof Character) {
query.setCharacter(key, ((Character) value).charValue());
} else if (value instanceof Timestamp) {
query.setTimestamp(key, (Timestamp) value);
} else if (value instanceof Date) {
query.setDate(key, (Date) value);
} else if (value instanceof Short) {
query.setShort(key, ((Short) value).shortValue());
}
}
public List getAll(String strHQL) {
// TODO Auto-generated method stub
return null;
}
}
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.lcsssh.bo.PageInfo;
public class HibernateBaseDAO extends HibernateDaoSupport implements BaseDao {
/**
* 根据主键ID查询对象
*
* @param c
* @param id
* @return Object
*/
public Object getById(Class c, Serializable id) {
return this.getHibernateTemplate().get(c, id);
}
/**
* 查询一个类的所有对象
*
* @param c
* @return List
*/
public List getAll(Class c) {
return this.getHibernateTemplate().loadAll(c);
}
/**
* 保存或者更新一个对象 当对象为临时状态时就保存对象 当对象为持久化或者游离状态时就更新对象
*
* @param o
*/
public void saveOrUpdate(Object o) {
this.getHibernateTemplate().saveOrUpdate(o);
}
/**
* 移除一个对象
*
* @param o
*/
public void delete(Object o) {
this.getHibernateTemplate().delete(o);
}
/**
* 使用给定的HQL字符串查询
*
* @param strHQL
* @return List
*/
public List findByHQL(final String strHQL) {
return this.getSession().createQuery(strHQL).list();
}
/**
* 根据给定的HQL语句与映射条件查询 用于复杂的查询语句 我们在代入查询参数的时候, 建议使用 name里面放的是HQL语句 eg: "from
* Courses where name = :name" map里面放的是你想绑定的参数实现集 eg: Map map = new
* HashMap(); map.put("name", "Tom"); 最后得到的结果就是 from Courses where name =
* Tom
*
* @param strHQL
* @param mapping
* @return List
*/
public List findByHQL(final String strHQL, final Map mappings) {
HibernateCallback hibernateCallback = new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(strHQL);
// 如果映射条件中不为空,则取出各条件拼接HQL语句
if (mappings != null || !mappings.isEmpty()) {
Iterator it = mappings.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
setParameterValues(query, (String) entry.getKey(),
entry.getValue());
}
}
List list = query.list();
return list;
}
};
return (List) this.getHibernateTemplate().execute(hibernateCallback);
}
/**
* 得到分页查询的记录
*
* @param hql
* @param values
* @param qEntity
* @return List
* @throws DataAccessException
*/
public List getQueryResult(final String strHQL, final Map mappings,
final PageInfo pageInfo) throws DataAccessException {
HibernateCallback hibernateCallback = new HibernateCallback() {
public Object doInHibernate(Session s) throws HibernateException,
SQLException {
String q = strHQL;
Query query = s.createQuery(q);
// 调用此方法向query对象中绑定查询参数
if (mappings != null && mappings.size() > 0) {
for (Iterator i = mappings.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
setParameterValues(query, (String) entry.getKey(), entry
.getValue());
}
}
// 分页控制 (根据当前的页数来得到要现在的记录集)
query.setFirstResult((pageInfo.getCurrentPage() - 1)
* pageInfo.getPerPageRows());
query.setMaxResults(pageInfo.getPerPageRows());
List list = query.list();
return list;
}
};
return this.getHibernateTemplate().executeFind(hibernateCallback);
}
/**
* 根据传入的查询语句的到记录的条数
*
* @param hql
* @param values
* @return int
* @throws DataAccessException
*/
public int getQueryCount(final String strHQL, final Map mappings)
throws DataAccessException {
HibernateCallback hibernateCallback = new HibernateCallback() {
public Object doInHibernate(Session s) throws HibernateException,
SQLException {
int fromIndex = strHQL.indexOf("from");
String q = "select count(*) " + strHQL.substring(fromIndex);
Query query = s.createQuery(q);
// 设置参数
if (mappings != null && mappings.size() > 0) {
for (Iterator i = mappings.entrySet().iterator(); i
.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
setParameterValues(query, (String) entry.getKey(),
entry.getValue());
}
}
Integer queryCount = (Integer) query.uniqueResult();
return queryCount;
}
};
return ((Integer) this.getHibernateTemplate().execute(hibernateCallback)).intValue();
}
/**
* 自动匹配数据类型
*
* @param query
* @param key
* @param value
*/
private void setParameterValues(Query query, String key, Object value) {
if (null == key || null == value) {
return;
} else if (value instanceof Boolean) {
query.setBoolean(key, ((Boolean) value).booleanValue());
} else if (value instanceof String) {
query.setString(key, (String) value);
} else if (value instanceof Integer) {
query.setInteger(key, ((Integer) value).intValue());
} else if (value instanceof Long) {
query.setLong(key, ((Long) value).longValue());
} else if (value instanceof Float) {
query.setFloat(key, ((Float) value).floatValue());
} else if (value instanceof Double) {
query.setDouble(key, ((Double) value).doubleValue());
} else if (value instanceof BigDecimal) {
query.setBigDecimal(key, (BigDecimal) value);
} else if (value instanceof Byte) {
query.setByte(key, ((Byte) value).byteValue());
} else if (value instanceof Calendar) {
query.setCalendar(key, (Calendar) value);
} else if (value instanceof Character) {
query.setCharacter(key, ((Character) value).charValue());
} else if (value instanceof Timestamp) {
query.setTimestamp(key, (Timestamp) value);
} else if (value instanceof Date) {
query.setDate(key, (Date) value);
} else if (value instanceof Short) {
query.setShort(key, ((Short) value).shortValue());
}
}
public List getAll(String strHQL) {
// TODO Auto-generated method stub
return null;
}
}