package com.biyeseng.common;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.sql.Clob;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.metadata.ClassMetadata;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
@SuppressWarnings("unchecked")
public abstract class HibernateGenericDao<T> extends HibernateDaoSupport {
protected Class<T> entityClass;
private JdbcTemplate jdbcTemplate;
public HibernateGenericDao() {
entityClass = GenericsUtils.getSuperClassGenricType(getClass());
}
protected Class<T> getEntityClass() {
return entityClass;
}
public T get(Serializable id) {
return (T) getHibernateTemplate().get(entityClass, id);
}
public Object get(Serializable id,Class clazz) {
return getHibernateTemplate().get(clazz, id);
}
public List<T> getAll() {
return getHibernateTemplate().loadAll(entityClass);
}
public List<T> getAll(String orderBy, boolean isAsc) {
Assert.hasText(orderBy);
if(isAsc)
return getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(entityClass).addOrder(Order.asc(orderBy)));
else
return getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(entityClass).addOrder(Order.desc(orderBy)));
}
public List<T> getByExample(Object o) {
List<T> results = getHibernateTemplate().findByExample(o);
return results;
}
public void save(Object o) {
Session session = null;
try {
session = getSession();
session.save(o);
session.flush();
}
catch(RuntimeException e) {
throw e;
}
finally {
if(session != null) {
this.releaseSession(session);
}
}
}
public void update(Object o) {
Session session = null;
try {
session = getSession();
session.merge(o);
session.flush();
}
catch(RuntimeException e) {
throw e;
}
finally {
if(session != null) {
this.releaseSession(session);
}
}
}
public void saveOrUpdate(Object o) {
Session session = null;
try {
session = getSession();
session.saveOrUpdate(o);
session.flush();
}
catch(RuntimeException e) {
throw e;
}
finally {
if(session != null) {
this.releaseSession(session);
}
}
}
public void remove(Object o) {
Session session = null;
try {
session = getSession();
session.delete(o);
session.flush();
}
catch(RuntimeException e) {
throw e;
}
finally {
if(session != null) {
this.releaseSession(session);
}
}
// getHibernateTemplate().delete(o);
}
public void removeById(Serializable id) {
remove(get(id));
}
public void flush() {
getHibernateTemplate().flush();
}
public void clear() {
getHibernateTemplate().clear();
}
public Query createQuery(Session session, String hql, Object... values) {
Assert.hasText(hql);
Query query = session.createQuery(hql);
for(int i = 0; i < values.length; i++) {
query.setParameter(i, values[i]);
}
return query;
}
public Criteria createCriteria(Session session, Criterion... criterions) {
Criteria criteria = session.createCriteria(entityClass);
for(Criterion c : criterions) {
criteria.add(c);
}
return criteria;
}
public Criteria createCriteria(Session session, String orderBy, boolean isAsc, Criterion... criterions) {
Assert.hasText(orderBy);
Criteria criteria = createCriteria(session, criterions);
if(isAsc)
criteria.addOrder(Order.asc(orderBy));
else
criteria.addOrder(Order.desc(orderBy));
return criteria;
}
public List find(String hql, Object... values) {
Assert.hasText(hql);
return getHibernateTemplate().find(hql, values);
}
public List<T> findBy(String propertyName, Object value) throws RuntimeException {
Assert.hasText(propertyName);
Session session = null;
List<T> ll = new ArrayList<T>();
try {
session = getSession();
ll = createCriteria(session, Restrictions.eq(propertyName, value)).list();
}
catch(RuntimeException e) {
throw e;
}
finally {
if(session != null) {
this.releaseSession(session);
}
}
return ll;
}
public List<T> findBy(String propertyName, Object value, String orderBy, boolean isAsc) {
Assert.hasText(propertyName);
Assert.hasText(orderBy);
Session session = null;
List<T> ll = new ArrayList<T>();
try {
session = getSession();
ll = createCriteria(session, orderBy, isAsc, Restrictions.eq(propertyName, value)).list();
}
catch(RuntimeException e) {
throw e;
}
finally {
if(session != null) {
this.releaseSession(session);
}
}
return ll;
}
public List<T> findByValues(String propertyName, Collection values) {
Assert.hasText(propertyName);
Session session = null;
List<T> ll = new ArrayList<T>();
try {
session = getSession();
ll = createCriteria(session, Restrictions.in(propertyName, values)).list();
}
catch(RuntimeException e) {
throw e;
}
finally {
if(session != null) {
this.releaseSession(session);
}
}
return ll;
}
public List<T> findByNamesAndValues(List<String> propertyNames, List values) {
List<T> ll = new ArrayList<T>();
Session session = getSession();
try {
Criteria criteria = session.createCriteria(entityClass);
for(int i = 0; i < propertyNames.size(); i++) {
String propertyName = propertyNames.get(i);
Object value = values.get(i);
if(value != null) {
criteria.add(Restrictions.eq(propertyName, value));
} else {
criteria.add(Restrictions.isNull(propertyName));
}
}
ll = criteria.list();
}
catch(RuntimeException e) {
throw e;
}
finally {
if(session != null) {
releaseSession(session);
}
}
return ll;
}
public T findUniqueBy(String propertyName, Object value) {
Assert.hasText(propertyName);
Session session = null;
Object obj = new Object();
try {
session = getSession();
obj = createCriteria(session, Restrictions.eq(propertyName, value)).uniqueResult();
}
catch(RuntimeException e) {
throw e;
}
finally {
if(session != null) {
this.releaseSession(session);
}
}
return (T) obj;
}
public boolean isUnique(Object entity, String uniquePropertyNames) {
Assert.hasText(uniquePropertyNames);
Session session = null;
String[] nameList = uniquePropertyNames.split(",");
session = getSession();
Criteria criteria = createCriteria(session).setProjection(Projections.rowCount());
try {
for(String name : nameList) {
criteria.add(Restrictions.eq(name, PropertyUtils.getProperty(entity, name)));
}
String idName = getIdName(entityClass);
Serializable id = getId(entity);
if(id != null)
criteria.add(Restrictions.not(Restrictions.eq(idName, id)));
}
catch(Exception e) {
ReflectionUtils.handleReflectionException(e);
}
boolean result = false;
try {
result = (Integer) criteria.uniqueResult() == 0;
}
catch(RuntimeException e) {
throw e;
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
毕业设计javajsp党校培训系统mysql-qlkrp源码工具包 页面是jsp,数据库mysql,jdk1.8,开发工具用ecplise、myecplise、sts、idea都可以 个人信息 干部信息 课程考勤信息 课程成绩信息 成绩查询 培训课程管理 包含:源码、数据库脚本、环境工具包、相同框架项目的安装教程(在说明文档中)
资源推荐
资源详情
资源评论
收起资源包目录
毕业设计javajsp党校培训系统mysql-qlkrp源码工具包 (942个子文件)
simple.html.bak 1015B
HibernateGenericDao.class 20KB
ChengjiWebAction.class 7KB
LoginuserWebAction.class 7KB
KaoqinWebAction.class 7KB
AnnotationSessionFactoryBeanEx.class 7KB
StringUtils.class 5KB
PeixunWebAction.class 5KB
DBManager.class 4KB
PageBean.class 4KB
LoginuserDAOImpl.class 4KB
ChengjiDAOImpl.class 4KB
KaoqinDAOImpl.class 4KB
PeixunDAOImpl.class 3KB
Loginuser.class 3KB
LoginuserServiceImpl.class 2KB
ChengjiServiceImpl.class 2KB
PeixunServiceImpl.class 2KB
KaoqinServiceImpl.class 2KB
GenericsUtils.class 2KB
Kaoqin.class 2KB
Chengji.class 2KB
Peixun.class 2KB
DateUtils.class 1KB
DataSource.class 1KB
Constants.class 1KB
LoginuserService.class 827B
LoginuserDAO.class 815B
ChengjiService.class 797B
ChengjiDAO.class 785B
PeixunService.class 782B
KaoqinService.class 782B
KaoqinDAO.class 770B
PeixunDAO.class 770B
.classpath 5KB
easyui.css 26KB
easyui.css 25KB
style.css 20KB
default.css 18KB
sophia_style.css 7KB
common.css 5KB
datepicker.css 5KB
datepicker.css 5KB
style.css 5KB
datagrid.css 5KB
datagrid.css 5KB
tabs.css 3KB
tabs.css 3KB
simple.css 3KB
calendar.css 3KB
calendar.css 3KB
alogin.css 3KB
tree.css 2KB
tree.css 2KB
layout.css 2KB
layout.css 2KB
select.css 2KB
panel.css 2KB
panel.css 2KB
linkbutton.css 1KB
linkbutton.css 1KB
window.css 1KB
window.css 1KB
menu.css 1KB
menu.css 1KB
icon.css 1KB
prettify.css 960B
pagination.css 933B
pagination.css 933B
combobox.css 872B
combobox.css 872B
splitbutton.css 733B
splitbutton.css 733B
spinner.css 732B
spinner.css 732B
combo.css 667B
combo.css 667B
combotree.css 666B
combotree.css 666B
validatebox.css 634B
validatebox.css 634B
messager.css 610B
messager.css 610B
dialog.css 422B
dialog.css 422B
accordion.css 421B
datebox.css 421B
accordion.css 421B
datebox.css 421B
menubutton.css 392B
menubutton.css 392B
index.css 282B
WdatePicker.css 158B
Thumbs.db 32KB
static.gif 35KB
login_02.gif 14KB
35.gif 13KB
42.gif 13KB
login_12.gif 10KB
40.gif 10KB
共 942 条
- 1
- 2
- 3
- 4
- 5
- 6
- 10
资源评论
言宇程序
- 粉丝: 2412
- 资源: 5237
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功