org.phoenix.basic.impl.BaseDao Maven / Gradle / Ivy
/**
*
*/
package org.phoenix.basic.impl;
import java.lang.reflect.ParameterizedType;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.phoenix.basic.dao.IBaseDao;
import org.phoenix.basic.paging.Pager;
import org.phoenix.basic.paging.SystemContext;
/**
* 数据库操作的基础类
* @author mengfeiyang
*
*/
@SuppressWarnings("unchecked")
public class BaseDao implements IBaseDao {
private SessionFactory sessionFactory;
/**
* 创建一个Class的对象来获取泛型的class
*/
private Class> clz;
public Class> getClz() {
if(clz==null) {
//获取泛型的Class对象
clz = ((Class>)
(((ParameterizedType)(this.getClass().getGenericSuperclass())).getActualTypeArguments()[0]));
}
return clz;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Inject
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
/*
* (non-Javadoc)
* @see org.phoenix.basic.dao.IBaseDao#add(java.lang.Object)
*/
@Override
public T add(T t) {
getSession().save(t);
return t;
}
/*
* (non-Javadoc)
* @see org.phoenix.basic.dao.IBaseDao#update(java.lang.Object)
*/
@Override
public void update(T t) {
getSession().update(t);
}
/*
* (non-Javadoc)
* @see org.phoenix.basic.dao.IBaseDao#delete(int)
*/
@Override
public void delete(int id) {
getSession().delete(this.load(id));
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#load(int)
*/
@Override
public T load(int id) {
return (T)getSession().load(getClz(), id);
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#list(java.lang.String, java.lang.Object[])
*/
public List list(String hql, Object[] args) {
return this.list(hql, args, null);
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#list(java.lang.String, java.lang.Object)
*/
public List list(String hql, Object arg) {
return this.list(hql, new Object[]{arg});
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#list(java.lang.String)
*/
public List list(String hql) {
return this.list(hql,null);
}
private String initSort(String hql) {
String order = SystemContext.getOrder();
String sort = SystemContext.getSort();
if(sort!=null&&!"".equals(sort.trim())) {
hql+=" order by "+sort;
if(!"desc".equals(order)) hql+=" asc";
else hql+=" desc";
}
return hql;
}
@SuppressWarnings("rawtypes")
private void setAliasParameter(Query query,Map alias) {
if(alias!=null) {
Set keys = alias.keySet();
for(String key:keys) {
Object val = alias.get(key);
if(val instanceof Collection) {
//查询条件是列表
query.setParameterList(key, (Collection)val);
} else {
query.setParameter(key, val);
}
}
}
}
private void setParameter(Query query,Object[] args) {
if(args!=null&&args.length>0) {
int index = 0;
for(Object arg:args) {
query.setParameter(index++, arg);
}
}
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#list(java.lang.String, java.lang.Object[], java.util.Map)
*/
public List list(String hql, Object[] args, Map alias) {
hql = initSort(hql);
Query query = getSession().createQuery(hql);
setAliasParameter(query, alias);
setParameter(query, args);
return query.list();
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#list(java.lang.String, java.util.Map)
*/
public List listByAlias(String hql, Map alias) {
return this.list(hql, null, alias);
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#find(java.lang.String, java.lang.Object[])
*/
public Pager find(String hql, Object[] args) {
return this.find(hql, args, null);
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#find(java.lang.String, java.lang.Object)
*/
public Pager find(String hql, Object arg) {
return this.find(hql, new Object[]{arg});
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#find(java.lang.String)
*/
public Pager find(String hql) {
return this.find(hql,null);
}
@SuppressWarnings("rawtypes")
private void setPagers(Query query,Pager pages) {
Integer pageSize = SystemContext.getPageSize();
Integer pageOffset = SystemContext.getPageOffset();
if(pageOffset==null||pageOffset<0) pageOffset = 0;
if(pageSize==null||pageSize<0) pageSize = 15;
pages.setOffset(pageOffset);
pages.setSize(pageSize);
query.setFirstResult(pageOffset).setMaxResults(pageSize);
}
private String getCountHql(String hql,boolean isHql) {
String e = hql.substring(hql.indexOf("from"));
String c = "select count(*) "+e;
if(isHql)
c = c.replaceAll("fetch", "");
return c;
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#find(java.lang.String, java.lang.Object[], java.util.Map)
*/
public Pager find(String hql, Object[] args, Map alias) {
hql = initSort(hql);
String cq = getCountHql(hql,true);
Query cquery = getSession().createQuery(cq);
Query query = getSession().createQuery(hql);
//设置别名参数
setAliasParameter(query, alias);
setAliasParameter(cquery, alias);
//设置参数
setParameter(query, args);
setParameter(cquery, args);
Pager pages = new Pager();
setPagers(query,pages);
List datas = query.list();
pages.setDatas(datas);
long total = (Long)cquery.uniqueResult();
pages.setTotal(total);
return pages;
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#find(java.lang.String, java.util.Map)
*/
public Pager findByAlias(String hql, Map alias) {
return this.find(hql,null, alias);
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#queryObject(java.lang.String, java.lang.Object[])
*/
public Object queryObject(String hql, Object[] args) {
return this.queryObject(hql, args,null);
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#queryObject(java.lang.String, java.lang.Object)
*/
public Object queryObject(String hql, Object arg) {
return this.queryObject(hql, new Object[]{arg});
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#queryObject(java.lang.String)
*/
public Object queryObject(String hql) {
return this.queryObject(hql,null);
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#updateByHql(java.lang.String, java.lang.Object[])
*/
public void updateByHql(String hql, Object[] args) {
Query query = getSession().createQuery(hql);
setParameter(query, args);
query.executeUpdate();
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#updateByHql(java.lang.String, java.lang.Object)
*/
public void updateByHql(String hql, Object arg) {
this.updateByHql(hql,new Object[]{arg});
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#updateByHql(java.lang.String)
*/
public void updateByHql(String hql) {
this.updateByHql(hql,null);
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#listBySql(java.lang.String, java.lang.Object[], java.lang.Class, boolean)
*/
public List listBySql(String sql, Object[] args, Class> clz,
boolean hasEntity) {
return this.listBySql(sql, args, null, clz, hasEntity);
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#listBySql(java.lang.String, java.lang.Object, java.lang.Class, boolean)
*/
public List listBySql(String sql, Object arg, Class> clz,
boolean hasEntity) {
return this.listBySql(sql, new Object[]{arg}, clz, hasEntity);
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#listBySql(java.lang.String, java.lang.Class, boolean)
*/
public List listBySql(String sql, Class> clz, boolean hasEntity) {
return this.listBySql(sql, null, clz, hasEntity);
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#listBySql(java.lang.String, java.lang.Object[], java.util.Map, java.lang.Class, boolean)
*/
public List listBySql(String sql, Object[] args,
Map alias, Class> clz, boolean hasEntity) {
sql = initSort(sql);
SQLQuery sq = getSession().createSQLQuery(sql);
setAliasParameter(sq, alias);
setParameter(sq, args);
if(hasEntity) {
sq.addEntity(clz);
} else
sq.setResultTransformer(Transformers.aliasToBean(clz));
return sq.list();
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#listBySql(java.lang.String, java.util.Map, java.lang.Class, boolean)
*/
public List listByAliasSql(String sql, Map alias,
Class> clz, boolean hasEntity) {
return this.listBySql(sql, null, alias, clz, hasEntity);
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#findBySql(java.lang.String, java.lang.Object[], java.lang.Class, boolean)
*/
public Pager findBySql(String sql, Object[] args, Class> clz,
boolean hasEntity) {
return this.findBySql(sql, args, null, clz, hasEntity);
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#findBySql(java.lang.String, java.lang.Object, java.lang.Class, boolean)
*/
public Pager findBySql(String sql, Object arg, Class> clz,
boolean hasEntity) {
return this.findBySql(sql, new Object[]{arg}, clz, hasEntity);
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#findBySql(java.lang.String, java.lang.Class, boolean)
*/
public Pager findBySql(String sql, Class> clz, boolean hasEntity) {
return this.findBySql(sql, null, clz, hasEntity);
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#findBySql(java.lang.String, java.lang.Object[], java.util.Map, java.lang.Class, boolean)
*/
public Pager findBySql(String sql, Object[] args,
Map alias, Class> clz, boolean hasEntity) {
sql = initSort(sql);
String cq = getCountHql(sql,false);
SQLQuery sq = getSession().createSQLQuery(sql);
SQLQuery cquery = getSession().createSQLQuery(cq);
setAliasParameter(sq, alias);
setAliasParameter(cquery, alias);
setParameter(sq, args);
setParameter(cquery, args);
Pager pages = new Pager();
setPagers(sq, pages);
if(hasEntity) {
sq.addEntity(clz);
} else {
sq.setResultTransformer(Transformers.aliasToBean(clz));
}
List datas = sq.list();
pages.setDatas(datas);
long total = ((BigInteger)cquery.uniqueResult()).longValue();
pages.setTotal(total);
return pages;
}
/* (non-Javadoc)
* @see org.phoenix.baisc.dao.IBaseDao#findBySql(java.lang.String, java.util.Map, java.lang.Class, boolean)
*/
public Pager findByAliasSql(String sql, Map alias,
Class> clz, boolean hasEntity) {
return this.findBySql(sql, null, alias, clz, hasEntity);
}
public Object queryObject(String hql, Object[] args,
Map alias) {
Query query = getSession().createQuery(hql);
setAliasParameter(query, alias);
setParameter(query, args);
return query.uniqueResult();
}
public Object queryObjectByAlias(String hql, Map alias) {
return this.queryObject(hql,null,alias);
}
@Override
public List loadAll() {
// TODO Auto-generated method stub
return null;
}
@Override
public List loadAll(String hql) {
// TODO Auto-generated method stub
return null;
}
@Override
public void addBatchData(List t) {
for(T it: t){
getSession().save(it);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy