阅读更多
package org.ks4j.orm.hibernate;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.persistence.MappedSuperclass;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.ks4j.orm.Compositor;
import org.ks4j.orm.Filtration;
import org.ks4j.orm.PageData;
import org.ks4j.utils.HibernateUtils;
import org.ks4j.utils.ReflectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.Assert;
/**
* dao基类.
* 1:该类封装了最常见数据库操作的方法,你可以继承该类,添加自己喜欢的方法
* 2:当你有多个sessionFactory时,你也可以在你的子类中重写setSessionFactory()方法
*
*
* @param 实体类类型
*/
@SuppressWarnings("unchecked")
@MappedSuperclass
public class BaseDao
{
protected Logger logger = LoggerFactory.getLogger(getClass());
protected SessionFactory sessionFactory;
protected Class entityClass;
//操作SQL语句的模板
private JdbcTemplate jdbcTemplate;
//注入Spring JdbcTemplate 对象
@Resource(name="jdbcTemplate")
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
/**
* 构造方法
*/
public BaseDao()
{
this.entityClass = ReflectionUtils.getSuperClassGenricType(getClass());
}
/**
* 采用@Resource(name="xxx")按名称注入SessionFactory, 当有多个SesionFactory的时候Override本函数.
*/
@Resource
public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
/**
* 取得Session.
*/
public Session getSession()
{
return sessionFactory.getCurrentSession();
}
//--------------------------------------------------------------------------------------------------
/**
* 新增对象.
*/
public void save(T entity)
{
Assert.notNull(entity, "entity不能为空");
getSession().save(entity);
logger.debug("save entity: {}", entity);
}
/**
* 修改对象.
*/
public void update(T entity)
{
Assert.notNull(entity, "entity不能为空");
getSession().update(entity);
logger.debug("update entity: {}", entity);
}
/**
* 删除对象.
*/
public void delete(T entity)
{
Assert.notNull(entity, "entity不能为空");
getSession().delete(entity);
logger.debug("delete entity: {}", entity);
}
/**
* 删除对象.
*/
public void delete(String id)
{
delete(findById(id));
logger.debug("delete entity {},id is {}", entityClass.getSimpleName(), id);
}
/**
* 按id获取对象.
*/
public T findById(String id)
{
Assert.notNull(id, "id不能为空");
return (T) getSession().get(entityClass, id);
}
/**
* 按属性查找唯一对象,匹配方式为相等.
*/
public T find(String fieldName, Object fieldValue)
{
Assert.hasText(fieldName, "fieldName不能为空");
Criterion criterion = Restrictions.eq(fieldName, fieldValue);
return (T) HibernateUtils.createCriteria(getSession(), entityClass, criterion).uniqueResult();
}
/**
* 按属性查找对象列表,匹配方式为相等.
*/
public List findList(String fieldName, Object fieldValue)
{
Assert.hasText(fieldName, "fieldName不能为空");
Criterion criterion = Restrictions.eq(fieldName, fieldValue);
return HibernateUtils.createCriteria(getSession(), entityClass, criterion).list();
}
/**
* 按照过滤条件对象查找对象列表.
*/
public List findList(Filtration... filtrations)
{
Criteria criteria = HibernateUtils.createCriteria(getSession(), entityClass);
//设置过滤条件
criteria = HibernateUtils.setFiltrationParameter(criteria, filtrations);
return criteria.list();
}
/**
* 按照过滤条件对象查找对象列表.
*/
public List findList(List filtrationList)
{
Criteria criteria = HibernateUtils.createCriteria(getSession(), entityClass);
//设置过滤条件
criteria = HibernateUtils.setFiltrationParameter(criteria, filtrationList);
return criteria.list();
}
/**
* 按照过滤条件对象查找对象列表,支持排序.
*/
public List findList(Compositor compositor, Filtration... filtrations)
{
Criteria criteria = HibernateUtils.createCriteria(getSession(), entityClass);
//设置过滤条件
criteria = HibernateUtils.setFiltrationParameter(criteria, filtrations);
//设置排序
criteria = HibernateUtils.setCompositorParameter(criteria, compositor);
return criteria.list();
}
/**
* 按照过滤条件对象查找对象列表,支持排序.
*/
public List findList(Compositor compositor, List filtrationList)
{
Criteria criteria = HibernateUtils.createCriteria(getSession(), entityClass);
//设置过滤条件
criteria = HibernateUtils.setFiltrationParameter(criteria, filtrationList);
//设置排序
criteria = HibernateUtils.setCompositorParameter(criteria, compositor);
return criteria.list();
}
/**
* 获取全部对象.
*/
public List findAll()
{
List