Hibernate 通用接口设计

 

public interface CommonMng {

    /**
     * 根据主键进行单个对象查询
     * 
     * @author guduxing890
     * @param clazz
     *            EntityClass
     * @param pk
     *            主键
     * @return
     * @see [类、类#方法、类#成员]
     */
    public <T> T findEntityById(Class<T> clazz, Serializable pk);

    /**
     * 根据主键和 属性参数进行单个对象查询
     * 
     * @author guduxing890
     * @param clazz
     *            EntityClass
     * @param pk
     *            主键
     * @param properties
     *            属性
     * @param param
     *            参数
     * @return
     * @see [类、类#方法、类#成员]
     */
    public <T> T findByProperty(Class<T> clazz, Serializable pk,
	    String properties, Object param);

    /**
     * 查询实体所有记录
     * 
     * @author guduxing890
     * @param clazz
     *            EntityClass
     * @return
     * @see [类、类#方法、类#成员]
     */
    public <T> List<T> findByClass(Class<T> clazz);

    /**
     * 根据属性和参数,查询实体记录
     * 
     * @author guduxing890
     * @param clazz
     *            EntityClass
     * @param properties
     *            属性数组
     * @param params
     *            参数数组
     * @return
     * @see [类、类#方法、类#成员]
     */
    public <T> List<T> findByProperties(Class<T> clazz, String[] properties,
	    Object[] params);

    /**
     * 根据属性和参数, 查询实例记录,增加排序字段和排序方式 <br/>
     * 备注:如果排序字段和排序方式不对等,多余的排序字段自动使用 升序排列
     * 
     * @author guduxing890
     * @param clazz
     *            实体类
     * @param properties
     *            属性数组
     * @param params
     *            参数数组
     * @param orderProperty
     *            排序字段数组
     * @param sort
     *            排序方式
     * @return
     * @see [类、类#方法、类#成员]
     */
    public <T> List<T> findByProperties(Class<T> clazz, String[] properties,
	    Object[] params, String[] orderProperty, String[] sort);

    /**
     * 根据参数,查询数量
     * 
     * @author guduxing890
     * @param clazz
     * @param properties
     * @param params
     * @return
     * @see [类、类#方法、类#成员]
     */
    public <T> int count(Class<T> clazz, String[] properties, Object[] params);

    /**
     * 根据属性和参数, 查询实例记录,增加排序字段和排序方式 ,和返回最大记录数<br/>
     * 备注:如果排序字段和排序方式不对等,多余的排序字段自动使用 升序排列
     * 
     * @author guduxing890
     * @param clazz
     *            实体类
     * @param properties
     *            属性
     * @param params
     *            参数
     * @param orderProperty
     *            排序字段
     * @param sort
     *            排序方式
     * @param count
     *            最大返回数量
     * @return
     * @see [类、类#方法、类#成员]
     */
    public <T> List<T> findByProperties(Class<T> clazz, String[] properties,
	    Object[] params, String[] orderProperty, String[] sort, int count);

    /**
     * 根据属性和参数, 查询分页,增加排序字段和排序方式 ,和返回最大记录数<br/>
     * 备注:如果排序字段和排序方式不对等,多余的排序字段自动使用 升序排列
     * 
     * @author guduxing890
     * @param clazz
     *            实体类
     * @param properties
     *            属性
     * @param params
     *            参数
     * @param orderProperty
     *            排序字段
     * @param sort
     *            排序方式
     * @param pageNo
     *            页码
     * @param count
     *            最大返回数量
     * @return
     * @see [类、类#方法、类#成员]
     */
    public <T> Pagination<T> findPager(Class<T> clazz, String[] properties,
	    Object[] params, String[] orderProperty, String[] sort, int pageNo,
	    int count);

    /**
     * 根据条件查询对象
     * 
     * @author guduxing890
     * @param clazz
     *            实体类
     * @param properties
     *            属性
     * @param params
     *            参数
     * @param count
     *            最大返回数量
     * @return
     * @see [类、类#方法、类#成员]
     */
    public <T> List<T> findByProperties(Class<T> clazz, String[] properties,
	    Object[] params, int count);

    /**
     * 更新或修改对象
     * 
     * @author guduxing890
     * @param t
     *            实例对象
     * @see [类、类#方法、类#成员]
     */
    public <T> void saveOrUpdate(T t);

    /**
     * 新增对象
     * 
     * @author guduxing890
     * @param t
     *            实例对象
     * @see [类、类#方法、类#成员]
     */
    public <T> void save(T t);

    /**
     * 保存所有对象
     * 
     * @author guduxing890
     * @param t
     * @see [类、类#方法、类#成员]
     */
    <T> void saveAll(T... t);

    /**
     * 根据属性查询实体
     * 
     * @author guduxing890
     * @param clazz
     *            Hibernate映射实体
     * @param properties
     *            属性数组
     * @param params
     *            参数数组
     * @param orderProperty
     *            排序属性数组
     * @param sort
     *            排序方式
     * @param firstResult
     *            起始行
     * @param count
     *            最大返回结果数
     * @return
     * @see [类、类#方法、类#成员]
     */
    <T> List<T> findByProperties(Class<T> clazz, String[] properties,
	    Object[] params, String[] orderProperty, String[] sort,
	    int firstResult, int count);

    /**
     * 删除对象
     * 
     * @author guduxing890
     * @param t
     * @see [类、类#方法、类#成员]
     */
    <T> void delete(T t);

 

 

 

以上为Services 层

 

 dao 层代码实现

 

/*
 * 文 件 名:  CommonDaoImpl.java
 * 版    权:  XX Technologies Co., Ltd. Copyright YYYY-YYYY,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  guduxing890
 * 修改时间:  2013-3-22
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.xxx.web.dao.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.xxx.common.Constants;
import com.xxx.common.dao.HibernateSimpleDao;
import com.xxx.web.dao.CommonDao;
/**
 * 
 * @author guduxing890
 * @version [版本号, 2013-3-22]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Repository
@SuppressWarnings("rawtypes")
public class CommonDaoImpl extends HibernateSimpleDao implements CommonDao {

    private static final String OBJECT_SEP = ".";

    /**
     * 重载方法
     * 
     * @param clazz
     * @param pk
     * @return
     */
    @SuppressWarnings("unchecked")
    @Override
    public <T> T getEntityById(Class<T> clazz, Serializable pk) {
	return (T) getSession().get(clazz, pk);
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T> T findByProperty(Class<T> clazz, Serializable pk,
	    String properties, Object param) {
	Criteria criteria = getSession().createCriteria(clazz);
	criteria.add(Restrictions.eq(properties, param));
	criteria.add(Restrictions.idEq(pk));
	return (T) criteria.uniqueResult();
    }

    @Override
    public <T> List<T> findByClass(Class<T> clazz) {
	return findByProperties(clazz, null, null, null, null);
    }

    @Override
    public <T> List<T> findByProperties(Class<T> clazz, String[] properties,
	    Object[] params) {
	return findByProperties(clazz, properties, params, null, null);
    }

    @Override
    public <T> void saveOrUpdateAll(T... t) {
	for (T a : t) {
	    this.saveOrUpdate(a);
	}
    }

    /**
     * 重载方法
     * 
     * @param clazz
     * @param properties
     * @param params
     * @param orderProperty
     * @param sort
     * @param count
     * @return
     */
    @Override
    public <T> List<T> findByProperties(Class<T> clazz, String[] properties,
	    Object[] params, String[] orderProperty, String[] sort,
	    Integer count) {
	return this.findByProperties(clazz, properties, params, orderProperty,
		sort, null, count);

    }

    /**
     * 重载方法
     * 
     * @param clazz
     * @param properties
     * @param params
     * @return
     */
    @Override
    public <T> List<T> findByProperties(Class<T> clazz, String[] properties,
	    Object[] params, String[] orderProperty, String[] sort) {
	return findByProperties(clazz, properties, params, null, null, null);
    }

    /**
     * 保存对象
     * 
     * @author guduxing890
     * @param t
     * @see [类、类#方法、类#成员]
     */
    public <T> void save(T t) {
	if (t instanceof Collection) {
	    Collection c = (Collection) t;
	    this.getHibernateTemplate().saveOrUpdateAll(c);
	} else {
	    this.getSession().save(t);
	}
    }

    /**
     * 保存或更新对象
     * 
     * @author guduxing890
     * @param t
     * @see [类、类#方法、类#成员]
     */
    public <T> void saveOrUpdate(T t) {
	this.getSession().saveOrUpdate(t);
    }

    /**
     * 重载方法
     * 
     * @param clazz
     * @param properties
     * @param params
     * @return
     */
    @Override
    public <T> int count(Class<T> clazz, String[] properties, Object[] params) {

	Criteria criteria = getSession().createCriteria(clazz);
	if (null != properties) {
	    int paramLen = properties.length;
	    for (int i = 0; i < paramLen; i++) {
		// 进行属性处理
		String property = properties[i];

		// 把属性进行用 . 分割。 取出里面的每一个子对象。作为一个子 Criteria.进行关联查询
		if (property.indexOf(OBJECT_SEP) != -1) {
		    String[] props = StringUtils.split(property, OBJECT_SEP);

		    Criteria propertyCriteria = null;
		    for (int y = 0; y < props.length - 1; y++) {
			if (y == 0) {
			    propertyCriteria = criteria
				    .createCriteria(props[y]);
			} else {
			    propertyCriteria = propertyCriteria
				    .createCriteria(props[y]);
			}
		    }
		    propertyCriteria.add(Restrictions.eq(
			    props[props.length - 1], params[i]));
		} else {
		    criteria.add(Restrictions.eq(properties[i], params[i]));
		}

	    }
	}
	// 查询记录条数
	criteria.setProjection(Projections.rowCount());

	return Integer.parseInt(criteria.uniqueResult().toString());
    }

    /**
     * 重载方法
     * 
     * @param clazz
     * @param properties
     * @param params
     * @param orderProperty
     * @param sort
     * @param firstResult
     * @param count
     * @return
     */
    @SuppressWarnings("unchecked")
    @Override
    public <T> List<T> findByProperties(Class<T> clazz, String[] properties,
	    Object[] params, String[] orderProperty, String[] sort,
	    Integer firstResult, Integer count) {
	Criteria criteria = getSession().createCriteria(clazz);
	if (null != properties) {
	    int paramLen = properties.length;
	    for (int i = 0; i < paramLen; i++) {
		// 进行属性处理
		String property = properties[i];

		// 把属性进行用 . 分割。 取出里面的每一个子对象。作为一个子 Criteria.进行关联查询
		if (property.indexOf(OBJECT_SEP) != -1) {
		    String[] props = StringUtils.split(property, OBJECT_SEP);

		    Criteria propertyCriteria = null;
		    for (int y = 0; y < props.length - 1; y++) {
			if (y == 0) {
			    propertyCriteria = criteria
				    .createCriteria(props[y]);
			} else {
			    propertyCriteria = propertyCriteria
				    .createCriteria(props[y]);
			}
		    }
		    propertyCriteria.add(Restrictions.eq(
			    props[props.length - 1], params[i]));
		} else {
		    criteria.add(Restrictions.eq(properties[i], params[i]));
		}

	    }
	}
	// 增加排序字段
	if (null != orderProperty) {
	    int length = orderProperty.length;

	    for (int i = 0; i < length; i++) {
		Order order = null;
		if (null != sort && i < sort.length) {
		    order = Constants.SORT_TYPE_ASC.equalsIgnoreCase(sort[i]) ? Order
			    .asc(orderProperty[i]) : Order
			    .desc(orderProperty[i]);
		} else {
		    order = Order.asc(orderProperty[i]);
		}
		criteria.addOrder(order);

	    }
	}
	if (count == null) {
	    count = 5000;
	}
	if (null != firstResult) {
	    criteria.setFirstResult(firstResult);
	}
	criteria.setMaxResults(count);
	return (List<T>) criteria.list();
    }

    /**
     * 重载方法
     * 
     * @param t
     */
    @Override
    public <T> void delete(T t) {
	this.getSession().delete(t);
    }

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值