package com.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;
}
fina
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
家教平台管理系统 (839个子文件)
simple.html.bak 1015B
HibernateGenericDao.class 20KB
LingWebAction.class 7KB
XunWebAction.class 7KB
LoginuserWebAction.class 7KB
UserinfoWebAction.class 7KB
AnnotationSessionFactoryBeanEx.class 7KB
MessageWebAction.class 6KB
StringUtils.class 5KB
NewsWebAction.class 5KB
PingWebAction.class 4KB
PageBean.class 4KB
PingDAOImpl.class 4KB
upload.class 4KB
MessageDAOImpl.class 4KB
LingDAOImpl.class 4KB
XunDAOImpl.class 4KB
NewsDAOImpl.class 4KB
LoginuserDAOImpl.class 4KB
UserinfoDAOImpl.class 3KB
Loginuser.class 3KB
Userinfo.class 3KB
IndexWebAction.class 3KB
Ling.class 3KB
Xun.class 3KB
Ping.class 2KB
Message.class 2KB
UserinfoServiceImpl.class 2KB
MessageServiceImpl.class 2KB
IdentifierGeneratorImpl.class 2KB
GenericsUtils.class 2KB
NewsServiceImpl.class 2KB
PingServiceImpl.class 2KB
LingServiceImpl.class 2KB
XunServiceImpl.class 2KB
LoginuserServiceImpl.class 2KB
News.class 2KB
DateUtils.class 1KB
DataSource.class 901B
UserinfoService.class 704B
UserinfoDAO.class 692B
MessageService.class 689B
MessageDAO.class 677B
Constants.class 676B
LingService.class 644B
PingService.class 644B
NewsService.class 644B
LingDAO.class 632B
NewsDAO.class 632B
PingDAO.class 632B
XunService.class 629B
LoginuserService.class 623B
XunDAO.class 617B
LoginuserDAO.class 611B
.classpath 6KB
detail_fang.css 56KB
easyui.css 26KB
easyui.css 25KB
default.css 18KB
css.css 16KB
css.css 16KB
style.css 11KB
style.css 11KB
style.css 8KB
head.css 6KB
head.css 6KB
control.css 5KB
control.css 5KB
datepicker.css 5KB
datepicker.css 5KB
datagrid.css 5KB
datagrid.css 5KB
common1.css 4KB
common1.css 4KB
commons.css 4KB
tabs.css 3KB
tabs.css 3KB
simple.css 3KB
calendar.css 3KB
calendar.css 3KB
picture_preview.css 3KB
admin.css 2KB
tree.css 2KB
tree.css 2KB
layout.css 2KB
layout.css 2KB
panel.css 2KB
panel.css 2KB
style0.css 1KB
style0.css 1KB
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
共 839 条
- 1
- 2
- 3
- 4
- 5
- 6
- 9
资源评论
- lx443998302020-04-17骗子,同一个文件改名的
哈-哈=哈
- 粉丝: 13
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功