package com.yanhui.base;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class BaseDaoImpl<T> implements BaseDao<T> {
protected SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
// @Resource
// public void setSessionFactory0(SessionFactory sessionFactory) {
// super.setSessionFactory(sessionFactory);
//
//
// }
private Session getCurrentSession() {
return this.sessionFactory.getCurrentSession();
}
public Serializable save(T o) {
return this.getCurrentSession().save(o);
}
public T get(Class<T> c, Serializable id) {
return (T) this.getCurrentSession().get(c, id);
}
public T get(String hql) {
Query q = this.getCurrentSession().createQuery(hql);
List<T> l = q.list();
if (l != null && l.size() > 0) {
return l.get(0);
}
return null;
}
public T get(String hql, Map<String, Object> params) {
Query q = this.getCurrentSession().createQuery(hql);
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
q.setParameter(key, params.get(key));
}
}
List<T> l = q.list();
if (l != null && l.size() > 0) {
return l.get(0);
}
return null;
}
public void delete(T o) {
this.getCurrentSession().delete(o);
}
public void update(T o) {
this.getCurrentSession().update(o);
}
public void saveOrUpdate(T o) {
this.getCurrentSession().saveOrUpdate(o);
}
public List<T> find(String hql) {
Query q = this.getCurrentSession().createQuery(hql);
return q.list();
}
public List<T> find(String hql, Map<String, Object> params) {
Query q = this.getCurrentSession().createQuery(hql);
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
q.setParameter(key, params.get(key));
}
}
return q.list();
}
public List<T> find(String hql, Map<String, Object> params, int page, int rows) {
Query q = this.getCurrentSession().createQuery(hql);
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
q.setParameter(key, params.get(key));
}
}
return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
}
public List<T> find(String hql, int page, int rows) {
Query q = this.getCurrentSession().createQuery(hql);
return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
}
public Long count(String hql) {
Query q = this.getCurrentSession().createQuery(hql);
return (Long) q.uniqueResult();
}
public Long count(String hql, Map<String, Object> params) {
Query q = this.getCurrentSession().createQuery(hql);
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
q.setParameter(key, params.get(key));
}
}
return (Long) q.uniqueResult();
}
public int executeHql(String hql) {
Query q = this.getCurrentSession().createQuery(hql);
return q.executeUpdate();
}
public int executeHql(String hql, Map<String, Object> params) {
Query q = this.getCurrentSession().createQuery(hql);
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
q.setParameter(key, params.get(key));
}
}
return q.executeUpdate();
}
}
最近下载更多
2639895440 LV1
2022年9月21日
mamahaha LV3
2022年7月6日
zd1990 LV2
2022年4月13日
amour1 LV11
2022年1月24日
MyPary LV6
2021年11月14日
了大河家违法和 LV3
2021年2月16日
user_mingen LV1
2020年3月24日
huzh035 LV3
2020年3月17日
kevens10 LV1
2020年3月13日
那一年你在哪 LV13
2019年11月27日
最近浏览更多
微信网友_7525238679375872 LV1
5月25日
xianyu091012 LV5
2024年12月26日
wmxhahah LV7
2024年4月8日
帅涵123456 LV2
2023年11月6日
流连瓦盖法 LV7
2023年9月20日
baozhenghua LV2
2023年8月29日
2506793979 LV12
2023年7月2日
陆程江 LV2
2023年5月28日
纯爱战士 LV2
2023年5月28日
奶油栀子 LV1
2023年5月6日

