网络选图
package cn.hacz.edu.dao.impl;
import java.io.Serializable;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;
import cn.hacz.edu.dao.BaseDaoI;
import cn.hacz.edu.entity.base.BaseEntity;
import cn.hacz.edu.entity.base.PageHelper;
@Repository(value = "baseDaoI")
@SuppressWarnings("unchecked")
public class BaseDaoImpl<T extends BaseEntity> implements BaseDaoI<T> {
@PersistenceContext
private EntityManager entityManager;
@Override
public Session getCurrentSession() {
return entityManager.unwrap(Session.class);
}
@Override
public Serializable save(T o) {
if (o != null) {
o.setCreateDateTime(LocalDateTime.now());
o.setModifyDateTime(LocalDateTime.now());
o.setDeleted(false);
return getCurrentSession().save(o);
}
return null;
}
@Override
public void saveOrUpdate(T o) {
if (o != null) {
o.setCreateDateTime(LocalDateTime.now());
o.setModifyDateTime(LocalDateTime.now());
o.setDeleted(false);
getCurrentSession().saveOrUpdate(o);
}
}
@Override
public void delete(T o) {
if (o != null) {
getCurrentSession().delete(o);
}
}
@Override
public void update(T o) {
if (o != null) {
o.setModifyDateTime(LocalDateTime.now());
getCurrentSession().update(o);
}
}
@Override
public T get(Class<T> c, Serializable id) {
return (T) getCurrentSession().get(c, id);
}
@Override
public T get(String hql) {
Query query = getCurrentSession().createQuery(hql);
List<T> list = query.list();
if (list != null && list.size() > 0) {
return list.get(0);
}
return null;
}
@Override
public T get(String hql, Object... params) {
Query query = getCurrentSession().createQuery(hql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
query.setParameter(i, params[i]);
}
}
List<T> list = query.list();
if (list != null && list.size() > 0) {
return list.get(0);
}
return null;
}
@Override
public T get(String hql, Map<String, Object> params) {
Query query = getCurrentSession().createQuery(hql);
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
Object obj = params.get(key);
// 这里考虑传入的参数是什么类型,不同类型使用的方法不同
if (obj instanceof Collection<?>) {
query.setParameterList(key, (Collection<?>) obj);
} else if (obj instanceof Object[]) {
query.setParameterList(key, (Object[]) obj);
} else {
query.setParameter(key, obj);
}
}
List<T> l = query.list();
if (l != null && l.size() > 0) {
return l.get(0);
}
}
return null;
}
@Override
public List<T> find(String hql) {
Query query = getCurrentSession().createQuery(hql);
return query.list();
}
@Override
public List<T> find(String hql, PageHelper ph) {
Query query = getCurrentSession().createQuery(hql);
return query.setFirstResult(ph.getPage() - 1 + ph.getRows()).setMaxResults(ph.getRows()).list();
}
@Override
public List<T> find(String hql, Object... params) {
Query query = getCurrentSession().createQuery(hql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
query.setParameter(i, params[i]);
}
}
return query.list();
}
@Override
public List<T> find(String hql, Map<String, Object> params) {
Query query = getCurrentSession().createQuery(hql);
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
Object obj = params.get(key);
// 这里考虑传入的参数是什么类型,不同类型使用的方法不同
if (obj instanceof Collection<?>) {
query.setParameterList(key, (Collection<?>) obj);
} else if (obj instanceof Object[]) {
query.setParameterList(key, (Object[]) obj);
} else {
query.setParameter(key, obj);
}
}
}
return query.list();
}
@Override
public List<T> find(String hql, Map<String, Object> params, PageHelper ph) {
Query query = getCurrentSession().createQuery(hql);
if (params != null && params.isEmpty()) {
for (String key : params.keySet()) {
Object obj = params.get(key);
// 这里考虑传入的参数是什么类型,不同类型使用的方法不同
if (obj instanceof Collection<?>) {
query.setParameterList(key, (Collection<?>) obj);
} else if (obj instanceof Object[]) {
query.setParameterList(key, (Object[]) obj);
} else {
query.setParameter(key, obj);
}
}
}
return query.setFirstResult(ph.getPage() - 1 + ph.getRows()).setMaxResults(ph.getRows()).list();
}
@Override
public Long count(String hql) {
Query query = getCurrentSession().createQuery(hql);
return (Long) query.uniqueResult();
}
@Override
public Long count(String hql, Object... params) {
Query query = getCurrentSession().createQuery(hql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
query.setParameter(i, params[i]);
}
}
return (Long) query.uniqueResult();
}
@Override
public Long count(String hql, Map<String, Object> params) {
Query query = getCurrentSession().createQuery(hql);
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
Object obj = params.get(key);
// 这里考虑传入的参数是什么类型,不同类型使用的方法不同
if (obj instanceof Collection<?>) {
query.setParameterList(key, (Collection<?>) obj);
} else if (obj instanceof Object[]) {
query.setParameterList(key, (Object[]) obj);
} else {
query.setParameter(key, obj);
}
}
}
return (Long) query.uniqueResult();
}
@Override
public int execute(String hql) {
Query query = getCurrentSession().createQuery(hql);
return query.executeUpdate();
}
@Override
public int execute(String hql, Object... params) {
Query query = getCurrentSession().createQuery(hql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
query.setParameter(i, params[i]);
}
}
return query.executeUpdate();
}
@Override
public int execute(String hql, Map<String, Object> params) {
Query query = getCurrentSession().createQuery(hql);
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
Object obj = params.get(key);
// 这里考虑传入的参数是什么类型,不同类型使用的方法不同
if (obj instanceof Collection<?>) {
query.setParameterList(key, (Collection<?>) obj);
} else if (obj instanceof Object[]) {
query.setParameterList(key, (Object[]) obj);
} else {
query.setParameter(key, obj);
}
}
}
return query.executeUpdate();
}
@Override
public List<?> findBySql(String sql) {
SQLQuery sqlQuery = getCurrentSession().createSQLQuery(sql);
return sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
}
@Override
public List<?> findBySql(String sql, PageHelper ph) {
SQLQuery sqlQuery = getCurrentSession().createSQLQuery(sql);
return sqlQuery.setFirstResult((ph.getPage() - 1) * ph.getRows()).setMaxResults(ph.getRows()).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
}
@Override
public List<?> findBySql(String sql, List<Object> params) {
SQLQuery sqlQuery = getCurrentSession().createSQLQuery(sql);
if (params != null && params.size() > 0) {
for (int i = 0; i < params.size(); i++) {
sqlQuery.setParameter(i, params.get(i));
}
}
return sqlQuery.setFirstResult(0).setMaxResults(10).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
}
@Override
public List<?> findBySql(String sql, Map<String, Object> params) {
SQLQuery sqlQuery = getCurrentSession().createSQLQuery(sql);
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
Object object = params.get(key);
// 这里考虑传入的参数是什么类型,不同类型使用的方法不同
if (object instanceof Collection<?>) {
sqlQuery.setParameterList(key, (Collection<?>) object);
} else if (object instanceof Object[]) {
sqlQuery.setParameterList(key, (Object[]) object);
} else {
sqlQuery.setParameter(key, object);
}
}
}
return sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
}
@Override
public List<?> findBySql(String sql, Map<String, Object> params, PageHelper ph) {
SQLQuery sqlQuery = getCurrentSession().createSQLQuery(sql);
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
Object object = params.get(key);
// 这里考虑传入的参数是什么类型,不同类型使用的方法不同
if (object instanceof Collection<?>) {
sqlQuery.setParameterList(key, (Collection<?>) object);
} else if (object instanceof Object[]) {
sqlQuery.setParameterList(key, (Object[]) object);
} else {
sqlQuery.setParameter(key, object);
}
}
}
return sqlQuery.setFirstResult((ph.getPage() - 1) * ph.getRows()).setMaxResults(ph.getRows()).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
}
@Override
public BigInteger countBySql(String sql) {
SQLQuery sqlQuery = getCurrentSession().createSQLQuery(sql);
return (BigInteger) sqlQuery.uniqueResult();
}
@Override
public BigInteger countBySql(String sql, Map<String, Object> params) {
SQLQuery sqlQuery = getCurrentSession().createSQLQuery(sql);
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
Object obj = params.get(key);
// 这里考虑传入的参数是什么类型,不同类型使用的方法不同
if (obj instanceof Collection<?>) {
sqlQuery.setParameterList(key, (Collection<?>) obj);
} else if (obj instanceof Object[]) {
sqlQuery.setParameterList(key, (Object[]) obj);
} else {
sqlQuery.setParameter(key, obj);
}
}
}
return new BigInteger(String.valueOf(sqlQuery.uniqueResult()));
}
@Override
public int executeSql(String sql) {
SQLQuery sqlQuery = getCurrentSession().createSQLQuery(sql);
return sqlQuery.executeUpdate();
}
@Override
public int executeSql(String sql, Map<String, Object> params) {
SQLQuery sqlQuery = getCurrentSession().createSQLQuery(sql);
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
Object obj = params.get(key);
// 这里考虑传入的参数是什么类型,不同类型使用的方法不同
if (obj instanceof Collection<?>) {
sqlQuery.setParameterList(key, (Collection<?>) obj);
} else if (obj instanceof Object[]) {
sqlQuery.setParameterList(key, (Object[]) obj);
} else {
sqlQuery.setParameter(key, obj);
}
}
}
return sqlQuery.executeUpdate();
}
@Override
public void merge(T o) {
if (o != null) {
o.setCreateDateTime(LocalDateTime.now());
o.setModifyDateTime(LocalDateTime.now());
o.setDeleted(false);
getCurrentSession().merge(o);
}
}
}
网友评论