package com.hfxt.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 org.springframework.transaction.annotation.Transactional;
import com.hfxt.common.Pager;
import com.hfxt.common.Validity;
import com.hfxt.dao.IBaseDao;
import com.hfxt.exception.HibernateDaoSupportException;
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
* W
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
java web 程序音乐播放器 (129个子文件)
BaseDao.class 17KB
MusicAction.class 6KB
BaseAction.class 6KB
Validity.class 4KB
MusicDaoImpl.class 3KB
IBaseDao.class 3KB
Pager.class 3KB
TbMusic.class 3KB
UserAction.class 2KB
TbUser.class 2KB
BaseDao$1.class 2KB
BaseDao$3.class 2KB
MusicServiceImpl.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
BaseDao$8.class 1KB
UserServiceImpl.class 1KB
BaseService.class 850B
ServiceException.class 742B
MusicService.class 728B
MusicDao.class 686B
RetCode.class 596B
HibernateDaoSupportException.class 496B
DAOException.class 388B
UserService.class 328B
UserDao.class 297B
.classpath 3KB
style.css 13KB
hibernate-core-3.6.0.Final.jar 2.94MB
spring.jar 2.78MB
aspectjweaver.jar 1.82MB
classes12.jar 1.53MB
lucene-core-3.0.2.jar 1009KB
xwork-core-2.2.1.jar 997KB
freemarker-2.3.16.jar 860KB
struts2-core-2.2.1.jar 755KB
c3p0-0.9.1.2.jar 596KB
commons-collections-3.1.jar 546KB
javassist-3.7.ga.jar 531KB
antlr-2.7.6.jar 433KB
log4j-1.2.15.jar 383KB
cglib-nodep-2.1_3.jar 317KB
dom4j-1.6.1.jar 307KB
ognl-3.0.jar 224KB
hibernate-search-analyzers-3.3.0.CR1.jar 139KB
commons-io-1.3.2.jar 86KB
hibernate-commons-annotations-3.2.0.Final.jar 70KB
persistence.jar 69KB
commons-logging.jar 59KB
commons-fileupload-1.2.1.jar 56KB
struts2-json-plugin-2.2.1.jar 55KB
slf4j-api-1.6.1.jar 25KB
struts2-spring-plugin-2.2.1.jar 21KB
jta-1.1.jar 15KB
slf4j-log4j12-1.6.1.jar 10KB
BaseDao.java 36KB
IBaseDao.java 11KB
Validity.java 10KB
MusicAction.java 6KB
BaseAction.java 5KB
Pager.java 4KB
MusicDaoImpl.java 2KB
TbMusic.java 2KB
DAOException.java 2KB
UserAction.java 1KB
TbUser.java 1KB
MusicServiceImpl.java 1KB
ServiceException.java 1KB
UserServiceImpl.java 961B
MusicService.java 911B
UserDaoImpl.java 890B
MusicDao.java 744B
RetCode.java 664B
UserService.java 495B
BaseService.java 464B
HibernateDaoSupportException.java 463B
UserDao.java 418B
background.jpg 314KB
logo.jpg 2KB
jquery-1.7.js 243KB
jquery.validate.js 30KB
jquery.form.js 28KB
jquery.select.js 5KB
function.js 5KB
util.js 5KB
jquery.dropdownPlain.js 374B
music-player.jsp 5KB
register.jsp 4KB
music-upload.jsp 3KB
login.jsp 3KB
taglibs.jsp 2KB
error.jsp 2KB
reg-result.jsp 1KB
index.jsp 1KB
共 129 条
- 1
- 2
清风de家
- 粉丝: 9
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于 Ant 的 Java 项目示例.zip
- 各种字符串相似度和距离算法的实现Levenshtein、Jaro-winkler、n-Gram、Q-Gram、Jaccard index、最长公共子序列编辑距离、余弦相似度…….zip
- 运用python生成的跳跃的爱心
- 包括用 Java 编写的程序 欢迎您在此做出贡献!.zip
- (源码)基于QT框架的学生管理系统.zip
- 功能齐全的 Java Socket.IO 客户端库,兼容 Socket.IO v1.0 及更高版本 .zip
- 功能性 javascript 研讨会 无需任何库(即无需下划线),只需 ES5 .zip
- 分享Java相关的东西 - Java安全漫谈笔记相关内容.zip
- 具有适合 Java 应用程序的顺序定义的 Cloud Native Buildpack.zip
- 网络建设运维资料库职业
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页