package org.service;
import java.io.Serializable;
import java.util.List;
/**
 * 通用业务逻辑层
 * 
 * @author Administrator
 * 
 * @param <T>
 */
public interface BaseServcice<T> {
	/**
	 * 添加实体对象
	 * @param entity
	 */
	public void addEntity(T entity);
	/**
	 * 级联添加实体对象
	 * @param entity 实体对象
	 */
	public void persistAddEntity(T entity);
	/**
	 * 批量添加实体对象
	 * @param entities 实体对象集合
	 */
	public void batchAddEntity(List<T> entities);
	/**
	 * 批量级联添加实体对象
	 * @param entity
	 */
	public void batchPersistAddEntity(List<T> entity);
	/**
	 * 删除实体对象
	 * @param entityId
	 */
	public void deleteEntity(Serializable entityId);
	
	/**
	 * 批量删除实体对象
	 * @param entityIds
	 */
	public void batchDeleteEntity(Serializable[] entityIds);
	/**
	 * 修改实体对象
	 * @param entity
	 */
	public void updateEntity(T entity);
	
	/**
	 * 按照指定的唯一标识获取实体对象
	 * @param entityId
	 * @return
	 */
	public T getEntity(Serializable entityId);
	
	/**
	 * 按照执行属性获取实体对象
	 * @param propName 属性名
	 * @param propValue 属性值
	 * @return
	 */
	public T getEntityByProp(String propName, Object propValue);
	/**
	 * 获取某个实体对象的某个属性值
	 * @param returnType 返回属性的类型
	 * @param returnPropName 返回属性名
	 * @param propName 属性名
	 * @param propValue 属性值
	 * @return
	 */
	public <E> E getValue(Class<E> returnType, String returnPropName,
			String propName, Object propValue);
	/**
	 * 获取所有实体对象
	 * @return
	 */
	public List<T> getEntityAll();
	
	/**
	 * 判断是否存在
	 * @param propName 属性名称
	 * @param propValue 属性值
	 * @return
	 */
	public boolean isExistsByProp(String propName, Object propValue);
	/**
	 * 判断是否可用
	 * @param propName 属性名称
	 * @param propValue 属性值
	 * @param propId 属性id
	 * @param idValue 值
	 * @return
	 */
	public boolean useByProp(String propName, Object propValue, String propId,
			Serializable idValue);
	/**
	 * 判断是否为空
	 * @return
	 */
	public boolean isNull();
}
package org.service.impl;
import java.io.Serializable;
import java.util.List;
import org.dao.BaseDao;
import org.service.BaseServcice;
import org.springframework.beans.factory.annotation.Autowired;
public class BaseServiceSupport<T> implements BaseServcice<T> {
	@Autowired
	private BaseDao<T> baseDao;
	@Override
	public void addEntity(T entity) {
		baseDao.save(entity);
	}
	@Override
	public void persistAddEntity(T entity) {
		baseDao.persist(entity);
	}
	@Override
	public void batchAddEntity(List<T> entities) {
		for (T t : entities) {
			baseDao.save(t);
		}
	}
	@Override
	public void batchPersistAddEntity(List<T> entity) {
		for (T t : entity) {
			baseDao.persist(t);
		}
	}
	@Override
	public void deleteEntity(Serializable entityId) {
		baseDao.remove(entityId);
	}
	@Override
	public void batchDeleteEntity(Serializable[] entityIds) {
		for (Serializable entityId : entityIds) {
			baseDao.remove(entityId);
		}
	}
	@Override
	public void updateEntity(T entity) {
		baseDao.update(entity);
	}
	@Override
	public T getEntity(Serializable entityId) {
		return baseDao.get(entityId);
	}
	@Override
	public T getEntityByProp(String propName, Object propValue) {
		return baseDao.get(propName + " = :" + propName, propValue);
	}
	@Override
	public <E> E getValue(Class<E> returnType, String returnPropName, String propName, Object propValue) {
		return baseDao.get(returnType, returnPropName,propName + " = :" + propName, propValue);
	}
	@Override
	public List<T> getEntityAll() {
		return baseDao.qeuryList();
	}
	@Override
	public boolean isExistsByProp(String propName, Object propValue) {
		return baseDao.count(propName + " =:" + propName, propValue) > 0;
	}
	@Override
	public boolean useByProp(String propName, Object propValue, String propId, Serializable idValue) {
		return baseDao.count(propName + " =:" + propName + " and " + propId +" != "+ propId, propId,idValue) > 0;
	}
	@Override
	public boolean isNull() {
		return baseDao.count() == 0;
	}
}
原文:http://www.cnblogs.com/stickitout/p/5208440.html