直接贴代码了
另一个
public class DaoBase<T extends AuditedObject, PK extends Serializable> extends
SimpleHibernateDao<T, PK> {//AuditedObject是dto
@Override
public void save(final T entity) {
entity.updateAuditInfo();
super.save(entity);
}
public void update(final T entity) {
entity.updateAuditInfo();
this.getSession().update(entity);
}
public void merge(final T entity) {
entity.updateAuditInfo();
this.getSession().merge(entity);
}
public void persist(final T entity) {
entity.updateAuditInfo();
this.getSession().persist(entity);
}
@SuppressWarnings("unchecked")
@Override
public T get(final PK id) {
return (T) getSession().get(entityClass, id);
}
@SuppressWarnings("unchecked")
public T load(final PK id) {
return (T) getSession().load(entityClass, id);
}
public static final int DESC = 1;
public static final int ASC = 0;
public void pagination(final int pageNo, final int pageList, final Query query) {
if (pageList > 0 && pageNo > 0) {
query.setMaxResults(pageList);
int beginIndex = (pageNo - 1) * pageList;
query.setFirstResult(beginIndex);
}
}
public void pagination(final int pageNo, final int pageList, final Criteria criteria) {
if (pageList > 0 && pageNo > 0) {
criteria.setMaxResults(pageList);
int beginIndex = (pageNo - 1) * pageList;
criteria.setFirstResult(beginIndex);
}
}
public void addOrder(final int order, final String sort, final Criteria criteria) {
if (sort != null) {
if (order == DESC) {
criteria.addOrder(Order.desc(sort));
} else {
criteria.addOrder(Order.asc(sort));
}
}
}
public void le(final String propertyName, final Object value, final Criteria criteria) {
if (value != null) {
Criterion criterion = Restrictions.le(propertyName, value);
criteria.add(criterion);
}
}
public void lt(final String propertyName, final Object value, final Criteria criteria) {
if (value != null) {
Criterion criterion = Restrictions.lt(propertyName, value);
criteria.add(criterion);
}
}
public void gt(final String propertyName, final Object value, final Criteria criteria) {
if (value != null) {
Criterion criterion = Restrictions.gt(propertyName, value);
criteria.add(criterion);
}
}
public void ge(final String propertyName, final Object value, final Criteria criteria) {
if (value != null) {
Criterion criterion = Restrictions.ge(propertyName, value);
criteria.add(criterion);
}
}
public void eq(final String propertyName, final Boolean value, final Criteria criteria) {
if (value != null) {
Criterion criterion = Restrictions.eq(propertyName, value);
criteria.add(criterion);
}
}
public void eq(final String propertyName, final Enum<?> value, final Criteria criteria) {
if (value != null) {
Criterion criterion = Restrictions.eq(propertyName, value);
criteria.add(criterion);
}
}
public void eq(final String propertyName, final String value, final Criteria criteria) {
if (StringUtils.isNotEmpty(value)) {
Criterion criterion = Restrictions.eq(propertyName, value);
criteria.add(criterion);
}
}
public void eq(final String propertyName, final int value, final Criteria criteria) {
if (value > 0) {
Criterion criterion = Restrictions.eq(propertyName, value);
criteria.add(criterion);
}
}
public void eq(final String propertyName, final Integer value, final Criteria criteria) {
if (value != null) {
Criterion criterion = Restrictions.eq(propertyName, value);
criteria.add(criterion);
}
}
public void like(final String propertyName, final String value, final Criteria criteria) {
if (StringUtils.isNotEmpty(value)) {
Criterion criterion = Restrictions.like(propertyName, value, MatchMode.ANYWHERE);
criteria.add(criterion);
}
}
public void in(final String propertyName, final String[] value, final Criteria criteria) {
if (value.length > 1) {
Criterion criterion = Restrictions.in(propertyName, value);
criteria.add(criterion);
}
}
// list
public void le(final String propertyName, final Object value, final List<Criterion> criterions) {
if (value != null) {
Criterion criterion = Restrictions.le(propertyName, value);
criterions.add(criterion);
}
}
public void lt(final String propertyName, final Object value, final List<Criterion> criterions) {
if (value != null) {
Criterion criterion = Restrictions.lt(propertyName, value);
criterions.add(criterion);
}
}
public void gt(final String propertyName, final Object value, final List<Criterion> criterions) {
if (value != null) {
Criterion criterion = Restrictions.gt(propertyName, value);
criterions.add(criterion);
}
}
public void ge(final String propertyName, final Object value, final List<Criterion> criterions) {
if (value != null) {
Criterion criterion = Restrictions.ge(propertyName, value);
criterions.add(criterion);
}
}
public void eq(final String propertyName, final Boolean value, final List<Criterion> criterions) {
if (value != null) {
Criterion criterion = Restrictions.eq(propertyName, value);
criterions.add(criterion);
}
}
public void eq(final String propertyName, final Enum<?> value, final List<Criterion> criterions) {
if (value != null) {
Criterion criterion = Restrictions.eq(propertyName, value);
criterions.add(criterion);
}
}
public void eq(final String propertyName, final String value, final List<Criterion> criterions) {
if (StringUtils.isNotEmpty(value)) {
Criterion criterion = Restrictions.eq(propertyName, value);
criterions.add(criterion);
}
}
public void eq(final String propertyName, final int value, final List<Criterion> criterions) {
if (value > 0) {
Criterion criterion = Restrictions.eq(propertyName, value);
criterions.add(criterion);
}
}
public void eq(final String propertyName, final Integer value, final List<Criterion> criterions) {
if (value != null) {
Criterion criterion = Restrictions.eq(propertyName, value);
criterions.add(criterion);
}
}
public void like(final String propertyName, final String value, final List<Criterion> criterions) {
if (StringUtils.isNotEmpty(value)) {
Criterion criterion = Restrictions.like(propertyName, value, MatchMode.ANYWHERE);
criterions.add(criterion);
}
}
public void in(final String propertyName, final String[] value, final List<Criterion> criterions) {
if (value.length > 1) {
Criterion criterion = Restrictions.in(propertyName, value);
criterions.add(criterion);
}
}
//
private static final ThreadLocal<Criteria> criteriaThread = new ThreadLocal<Criteria>();
private Criteria getCriteria() {
Criteria criteria = criteriaThread.get();
if (criteria == null) {
criteria = createCriteria();
criteriaThread.set(criteria);
}
return criteria;
}
public DaoBase<T, PK> isNull(final String propertyName) {
Criteria criteria = getCriteria();
Criterion criterion = Restrictions.isNull(propertyName);
criteria.add(criterion);
return this;
}
public DaoBase<T, PK> isNotNull(final String propertyName) {
Criteria criteria = getCriteria();
Criterion criterion = Restrictions.isNotNull(propertyName);
criteria.add(criterion);
return this;
}
public DaoBase<T, PK> isEmpty(final String propertyName) {
Criteria criteria = getCriteria();
Criterion criterion = Restrictions.isEmpty(propertyName);
criteria.add(criterion);
return this;
}
public DaoBase<T, PK> isNotEmpty(final String propertyName) {
Criteria criteria = getCriteria();
Criterion criterion = Restrictions.isNotEmpty(propertyName);
criteria.add(criterion);
return this;
}
public DaoBase<T, PK> pagination(final int pageNo, final int pageList) {
Criteria criteria = getCriteria();
if (pageList > 0 && pageNo > 0) {
criteria.setMaxResults(pageList);
int beginIndex = (pageNo - 1) * pageList;
criteria.setFirstResult(beginIndex);
}
return this;
}
public DaoBase<T, PK> addOrder(final int order, final String sort) {
Criteria criteria = getCriteria();
if (sort != null) {
if (order == DESC) {
criteria.addOrder(Order.desc(sort));
} else {
criteria.addOrder(Order.asc(sort));
}
}
return this;
}
public DaoBase<T, PK> le(final String propertyName, final Object value) {
Criteria criteria = getCriteria();
if (value != null) {
Criterion criterion = Restrictions.le(propertyName, value);
criteria.add(criterion);
}
return this;
}
public DaoBase<T, PK> lt(final String propertyName, final Object value) {
Criteria criteria = getCriteria();
if (value != null) {
Criterion criterion = Restrictions.lt(propertyName, value);
criteria.add(criterion);
}
return this;
}
public DaoBase<T, PK> gt(final String propertyName, final Object value) {
Criteria criteria = getCriteria();
if (value != null) {
Criterion criterion = Restrictions.gt(propertyName, value);
criteria.add(criterion);
}
return this;
}
public DaoBase<T, PK> ge(final String propertyName, final Object value) {
Criteria criteria = getCriteria();
if (value != null) {
Criterion criterion = Restrictions.ge(propertyName, value);
criteria.add(criterion);
}
return this;
}
public DaoBase<T, PK> eq(final String propertyName, final Boolean value) {
Criteria criteria = getCriteria();
if (value != null) {
Criterion criterion = Restrictions.eq(propertyName, value);
criteria.add(criterion);
}
return this;
}
public DaoBase<T, PK> eq(final String propertyName, final AuditedObject entity) {
Criteria criteria = getCriteria();
if (entity != null) {
Criterion criterion = Restrictions.eq(propertyName, entity);
criteria.add(criterion);
}
return this;
}
public DaoBase<T, PK> eq(final String propertyName, final Enum<?> value) {
Criteria criteria = getCriteria();
if (value != null) {
Criterion criterion = Restrictions.eq(propertyName, value);
criteria.add(criterion);
}
return this;
}
public DaoBase<T, PK> eq(final String propertyName, final String value) {
Criteria criteria = getCriteria();
if (StringUtils.isNotEmpty(value)) {
Criterion criterion = Restrictions.eq(propertyName, value);
criteria.add(criterion);
}
return this;
}
public DaoBase<T, PK> eq(final String propertyName, final int value) {
Criteria criteria = getCriteria();
if (value > 0) {
Criterion criterion = Restrictions.eq(propertyName, value);
criteria.add(criterion);
}
return this;
}
public DaoBase<T, PK> eq(final String propertyName, final Integer value) {
Criteria criteria = getCriteria();
if (value != null) {
Criterion criterion = Restrictions.eq(propertyName, value);
criteria.add(criterion);
}
return this;
}
public DaoBase<T, PK> eq(final String propertyName, final Date value) {
Criteria criteria = getCriteria();
if (value != null) {
Criterion criterion = Restrictions.eq(propertyName, value);
criteria.add(criterion);
}
return this;
}
public DaoBase<T, PK> ne(final String propertyName, final Object value) {
Criteria criteria = getCriteria();
if (value != null) {
Criterion criterion = Restrictions.ne(propertyName, value);
criteria.add(criterion);
}
return this;
}
public DaoBase<T, PK> or(final Criterion lhs, final Criterion rhs) {
Criteria criteria = getCriteria();
Criterion criterion = Restrictions.or(lhs, rhs);
criteria.add(criterion);
return this;
}
public DaoBase<T, PK> in(final String propertyName, final Object[] values) {
Criteria criteria = getCriteria();
if (values != null) {
Criterion criterion = Restrictions.in(propertyName, values);
criteria.add(criterion);
}
return this;
}
public DaoBase<T, PK> like(final String propertyName, final String value) {
Criteria criteria = getCriteria();
if (StringUtils.isNotEmpty(value)) {
Criterion criterion = Restrictions.like(propertyName, value, MatchMode.ANYWHERE);
criteria.add(criterion);
}
return this;
}
public DaoBase<T, PK> like(final String propertyName, final Object value) {
Criteria criteria = getCriteria();
Criterion criterion = Restrictions.like(propertyName, value);
criteria.add(criterion);
return this;
}
public DaoBase<T, PK> in(final String propertyName, final String[] value) {
Criteria criteria = getCriteria();
if (value.length >= 1) {
Criterion criterion = Restrictions.in(propertyName, value);
criteria.add(criterion);
}
return this;
}
public DaoBase<T, PK> in(final String propertyName, final Collection values) {
Criteria criteria = getCriteria();
if (values != null && values.size() >= 1) {
Criterion criterion = Restrictions.in(propertyName, values);
criteria.add(criterion);
}
return this;
}
public DaoBase<T, PK> createAlias(final String associationPath, final String alias) {
Criteria criteria = getCriteria();
criteria.createAlias(associationPath, alias);
return this;
}
public DaoBase<T, PK> addOrder(final Order order) {
Criteria criteria = getCriteria();
if (order != null) {
criteria.addOrder(order);
}
return this;
}
// 2. criteriaThread.remove();
public List<T> list() {
Criteria criteria = getCriteria();
criteriaThread.remove();
@SuppressWarnings("unchecked")
List<T> list = criteria.list();
return list;
}
// 4. criteriaThread.remove();
public T unique() {
Criteria criteria = getCriteria();
criteriaThread.remove();
@SuppressWarnings("unchecked")
T t = (T) criteria.uniqueResult();
return t;
}
}
另一个
public class SimpleHibernateDao<T, ID extends Serializable> {
protected Logger logger = LoggerFactory.getLogger(getClass());
protected SessionFactory sessionFactory;
protected Class<T> entityClass;
public SimpleHibernateDao() {
this.entityClass = ReflectionUtils.getSuperClassGenricType(getClass());
}//ReflectionUtils反射工具类
public SimpleHibernateDao(final Class<T> entityClass) {
this.entityClass = entityClass;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Autowired
public void setSessionFactory(final SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Session getSession() {
return sessionFactory.getCurrentSession();
}
public void save(final T entity) {
logger.debug("saving entity: {}", entity);
getSession().saveOrUpdate(entity);
}
public void delete(final T entity) {
getSession().delete(entity);
logger.debug("delete entity: {}", entity);
}
public void delete(final ID id) {
delete(get(id));
logger.debug("delete entity {},id is {}", entityClass.getSimpleName(), id);
}
@SuppressWarnings("unchecked")
public T get(final ID id) {
return (T) getSession().load(entityClass, id);
}
public List<T> get(final Collection<ID> ids) {
return find(Restrictions.in(getIdName(), ids));
}
public List<T> getAll() {
return find();
}
@SuppressWarnings("unchecked")
public List<T> getAll(final String orderByProperty, final boolean isAsc) {
Criteria c = createCriteria();
if (isAsc) {
c.addOrder(Order.asc(orderByProperty));
} else {
c.addOrder(Order.desc(orderByProperty));
}
return c.list();
}
public List<T> findBy(final String propertyName, final Object value) {
Criterion criterion = Restrictions.eq(propertyName, value);
return find(criterion);
}
@SuppressWarnings("unchecked")
public T findUniqueBy(final String propertyName, final Object value) {
Criterion criterion = Restrictions.eq(propertyName, value);
return (T) createCriteria(criterion).uniqueResult();
}
@SuppressWarnings("unchecked")
public <X> List<X> find(final String hql, final Object... values) {
return createQuery(hql, values).list();
}
@SuppressWarnings("unchecked")
public <X> List<X> find(final String hql, final Map<String, ?> values) {
return createQuery(hql, values).list();
}
@SuppressWarnings("unchecked")
public <X> X findUnique(final String hql, final Object... values) {
return (X) createQuery(hql, values).uniqueResult();
}
@SuppressWarnings("unchecked")
public <X> X findUnique(final String hql, final Map<String, ?> values) {
return (X) createQuery(hql, values).uniqueResult();
}
public int batchExecute(final String hql, final Object... values) {
return createQuery(hql, values).executeUpdate();
}
public int batchExecute(final String hql, final Map<String, ?> values) {
return createQuery(hql, values).executeUpdate();
}
public Query createQuery(final String queryString, final Object... values) {
Query query = getSession().createQuery(queryString);
if (values != null) {
for (int i = 0; i < values.length; i++) {
query.setParameter(i, values[i]);
}
}
return query;
}
public Query createQuery(final String queryString, final Map<String, ?> values) {
Query query = getSession().createQuery(queryString);
if (values != null) {
query.setProperties(values);
}
return query;
}
@SuppressWarnings("unchecked")
public List<T> find(final Criterion... criterions) {
return createCriteria(criterions).list();
}
@SuppressWarnings("unchecked")
public T findUnique(final Criterion... criterions) {
return (T) createCriteria(criterions).uniqueResult();
}
public Criteria createCriteria(final Criterion... criterions) {
Criteria criteria = getSession().createCriteria(entityClass);
for (Criterion c : criterions) {
criteria.add(c);
}
return criteria;
}
public void initProxyObject(final Object proxy) {
Hibernate.initialize(proxy);
}
public void flush() {
getSession().flush();
}
public Query distinct(final Query query) {
query.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
return query;
}
public Criteria distinct(final Criteria criteria) {
criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
return criteria;
}
public String getIdName() {
ClassMetadata meta = getSessionFactory().getClassMetadata(entityClass);
return meta.getIdentifierPropertyName();
}
public boolean isPropertyUnique(final String propertyName, final Object newValue,
final Object oldValue) {
if (newValue == null || newValue.equals(oldValue)) {
return true;
}
Object object = findUniqueBy(propertyName, newValue);
return (object == null);
}
}