package com.crm.dao.impl;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.crm.exception.HibernateDaoSupportException;
import com.crm.common.Pager;
import com.crm.common.Validity;
import com.crm.dao.IBaseDao;
public class BaseDao<T,PK extends Serializable> extends HibernateDaoSupport implements IBaseDao<T,PK> {
protected Logger log = LoggerFactory.getLogger(this.getClass());
private boolean isCacheQueries = true;
public boolean isCacheQueries() {
return isCacheQueries;
}
public void setCacheQueries(boolean isCacheQueries) {
this.isCacheQueries = isCacheQueries;
}
/**
* The <code>add(T entity)</code> method is add object to database.
*
* @param entity
* if you want to add entity.
*
* @throws HibernateDaoSupportException
* Throws HibernateDaoSupportException when accessing and
* manipulating database happen exception.
*/
public PK add(T entity) throws HibernateDaoSupportException {
if (null == entity) {
throw new HibernateDaoSupportException("Param(#"
+ this.getClass().getName() + ") with value null");
}
try {
PK pk = (PK)this.getHibernateTemplate().save(entity);
log.debug("Executing save method is successful!");
return pk;
} catch (DataAccessException e) {
log.error("Error saving entity:{}", e);
throw new HibernateDaoSupportException("Error saving (#"
+ this.getClass().getName() + "#):" + e);
}
}
/**
* 锟斤拷锟铰或保达拷实锟斤拷
* @param entity
*/
public void saveOrUpdateEntity(T entity){
this.getHibernateTemplate().saveOrUpdate(entity);
}
/**
* The <code>delete(T entity)</code> method is delete object to database.
*
* @param entity
* if you want to delete entity.
*
* @throws HibernateDaoSupportException
* Throws HibernateDaoSupportException when accessing and
* manipulating database happen exception.
*/
public void delete(T entity) throws HibernateDaoSupportException {
if (null == entity) {
throw new HibernateDaoSupportException("Param(#"
+ this.getClass().getName() + ") with value null");
}
try {
this.getHibernateTemplate().delete(entity);
log.debug("Execute delete method is successful!");
} catch (DataAccessException e) {
log.error("Error deleting entity:{}", e);
throw new HibernateDaoSupportException("Error deleting (#"
+ this.getClass().getName() + "#):" + e);
}
}
/**
* The <code>find(T entity)</code> method is find object according object
* type.
*
* @param entity
* if you want to find class condition.
* @return List<T> according entity to find object's connection.
* @throws HibernateDaoSupportException
* Throws HibernateDaoSupportException when accessing and
* manipulating database happen exception.
*
*/
@SuppressWarnings("unchecked")
public List<T> find(T entity) throws HibernateDaoSupportException {
if (null == entity) {
throw new HibernateDaoSupportException("Param(#"
+ this.getClass().getName() + ") with value null");
}
List lists = null;
try {
if (isCacheQueries) {
this.getHibernateTemplate().setCacheQueries(true);
} else {
isCacheQueries = true;
this.getHibernateTemplate().setCacheQueries(false);
}
lists = (List<T>) this.getHibernateTemplate().findByExample(entity);
log.debug("Execute find method is successful!");
} catch (DataAccessException e) {
log.error("Error finding entity: {}", e);
throw new HibernateDaoSupportException("Error finding (#"
+ this.getClass().getName() + "#):" + e);
}
return lists;
}
/**
* find object's collection with class
*
* @param clazz
* according class
* @return Object's connection
* @throws HibernateDaoSupportException
* when accessing and manipulating database happen exception
*/
@SuppressWarnings("unchecked")
public List<T> find(Class<T> clazz) throws HibernateDaoSupportException {
if (null == clazz) {
throw new HibernateDaoSupportException(
"Param(#clazz) with value null");
}
String hql = "FROM " + clazz.getName();
List<T> lists = null;
try {
if (isCacheQueries) {
this.getHibernateTemplate().setCacheQueries(true);
} else {
isCacheQueries = true;
this.getHibernateTemplate().setCacheQueries(false);
}
lists = (List<T>) this.getHibernateTemplate().find(hql);
log.debug("Execute find method is successful!");
} catch (DataAccessException e) {
log.error("Error finding entity:{}", e);
throw new HibernateDaoSupportException("Error finding (#"
+ this.getClass().getName() + "#):" + e);
}
return lists;
}
/**
* The <code>findById(PK id)</code> method is find object according
* primary key.
*
* @param id
* if you want to find object's primary key
* @return T insject object
* @throws HibernateDaoSupportException
* Throws HibernateDaoSupportException when accessing and
* manipulating database happen exception.
*/
@SuppressWarnings("unchecked")
public T findById(PK id, Class<T> clazz)
throws HibernateDaoSupportException {
if (null == id) {
throw new HibernateDaoSupportException("PK with value null");
}
T entity = null;
String hql = "FROM " + clazz.getName() + " n where n.id = ";
if(id instanceof String){
hql += "'"+id+"'";
}else{
hql += id;
}
try {
//use 2 leave cache
entity = (T) this.getUniqueBeanResult(hql);
log.debug("Execute findById method is successful!");
} catch (DataAccessException e) {
log.error("Error finding entity:{}", e);
throw new HibernateDaoSupportException("Error finding ("
+ this.getClass().getName() + "):" + e);
}
return entity;
}
/**
* The <code>queryList(PK startRecord, PK pageSize)</code> method is query
* objects according startRecord and pagesize're number, object type is
* according your implements this method's define type, and implements this
* interface abstract class must be override all method and inject entity
* type.
*
* @param startRecord
* Where from the beginning to show this record
* @param
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
CRM_T4+mysql数据库成功运行 (387个子文件)
BaseDao.class 17KB
TbCustomer.class 10KB
BaseAction.class 9KB
TbChance.class 6KB
TbService.class 6KB
TbUser.class 6KB
TbDict.class 5KB
TbLinkman.class 4KB
Validity.class 4KB
BaseService.class 4KB
TbLost.class 4KB
TbRight.class 3KB
TbRole.class 3KB
IBaseDao.class 3KB
TbActivity.class 3KB
Pager.class 3KB
TbPlan.class 3KB
RoleAction.class 2KB
UserAction.class 2KB
TbRoleRight.class 2KB
IPRequest.class 2KB
BaseDao$1.class 2KB
BaseDao$3.class 2KB
BaseDao$4.class 2KB
BaseDao$9.class 2KB
BaseDao$2.class 1KB
BaseDao$10.class 1KB
UserDaoImpl.class 1KB
BaseDao$6.class 1KB
BaseDao$5.class 1KB
BaseDao$7.class 1KB
RoleRightDaoImpl.class 1KB
BaseDao$8.class 1KB
UserServiceImpl.class 1KB
RoleRightServiceImpl.class 896B
ServiceException.class 740B
CustomerDaoImpl.class 575B
ActivityDaoImpl.class 575B
LinkManDaoImpl.class 568B
ServiceDaoImpl.class 568B
ChanceDaoImpl.class 561B
RightDaoImpl.class 556B
LostDaoImpl.class 547B
RoleDaoImpl.class 547B
DictDaoImpl.class 547B
PlanDaoImpl.class 547B
RetCode.class 499B
CustomerServiceImpl.class 493B
ActivityServiceImpl.class 493B
HibernateDaoSupportException.class 493B
LinkManServiceImpl.class 488B
ServiceServiceImpl.class 488B
ChanceServiceImpl.class 483B
RightServiceImpl.class 478B
LostServiceImpl.class 473B
PlanServiceImpl.class 473B
RoleServiceImpl.class 473B
DictServiceImpl.class 473B
RoleRightDao.class 449B
DAOException.class 385B
UserDao.class 372B
RoleRightService.class 340B
UserService.class 267B
CustomerDao.class 251B
ActivityDao.class 251B
ServiceDao.class 248B
LinkManDao.class 248B
ChanceDao.class 245B
RightDao.class 244B
PlanDao.class 239B
DictDao.class 239B
RoleDao.class 239B
LostDao.class 239B
ActivityService.class 127B
CustomerService.class 127B
LinkManService.class 125B
ServiceService.class 125B
ChanceService.class 123B
RightService.class 121B
PlanService.class 119B
LostService.class 119B
DictService.class 119B
RoleService.class 119B
.classpath 3KB
org.eclipse.wst.common.component 542B
org.eclipse.wst.jsdt.ui.superType.container 49B
style.css 12KB
menu.css 2KB
dtree.css 817B
Thumbs.db 20KB
Thumbs.db 15KB
welcome.gif 57KB
logo.gif 3KB
bt_linkman.gif 2KB
globe.gif 1KB
trash.gif 1KB
question.gif 1KB
base.gif 1KB
nolines_plus.gif 870B
nolines_minus.gif 861B
共 387 条
- 1
- 2
- 3
- 4
资源评论
ChihElena
- 粉丝: 0
- 资源: 26
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功