`
周英能
  • 浏览: 185909 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

DAO和Service公共抽象接口

 
阅读更多

1、所有Dao标志接口

 

public interface DataAccessObject {}

 

 2、所有实体标志父类

 

public class DominObject implements Serializable{}

 

 3、DAO基础接口,包括增删改查等

 

public interface BaseDao<Entity, PK extends Serializable> extends
		DataAccessObject {
	/**
	 * 增加实体
	 * 
	 * @param entity
	 * @return
	 */
	public Serializable save(Entity entity);

	/**
	 * 更新实体
	 */
	public void update(Entity entity);
	/**
	 * 根据ID判断保存或者更新
	 * 
	 */
	public void saveOrUpdate(Entity entity);
	/**
	 * 合并实体
	 */
	public void merge(Entity entity);

	/**
	 * 更新实体
	 */
	public void refresh(Entity entity);

	/**
	 * 删除实体
	 * 
	 * @param entity
	 */
	public void delete(Entity entity);

	/**
	 * 按ID删除对象
	 * 
	 * @param id
	 */
	public void delete(final PK id);

	/**
	 * 按ID查询对象
	 * 
	 * @param id
	 * @return
	 */
	public Entity findById(final PK id);

	/**
	 * 按ID查询多个对象
	 * 
	 * @param ids
	 * @return
	 */
	public List<Entity> findByIds(final Collection<PK> ids);

	/**
	 * 查询全部对象
	 * 
	 * @return
	 */
	public List<Entity> getAll();

	/**
	 * 获取全部对象, 支持按属性行序.
	 */
	public List<Entity> getAll(String orderByProperty, boolean isAsc);

	/**
	 * 按属性查找对象列表, 匹配方式为相等.
	 */
	public List<Entity> findByProperty(final String propertyName,
			final Object value);

	/**
	 * 按属性查找唯一对象, 匹配方式为相等.
	 */
	public Entity findUniqueByProperty(final String propertyName,
			final Object value);

	/**
	 * 判断对象的属性值在数据库内是否唯一.
	 * 
	 * 在修改对象的情景下,如果属性新修改的值(value)等于属性原来的值(orgValue)则不作比较.
	 */
	public boolean isPropertyUnique(final String propertyName,
			final Object newValue, final Object oldValue);

	/**
	 * 初始化对象. 使用load()方法得到的仅是对象Proxy, 在传到View层前需要进行初始化. 如果传入entity,
	 * 则只初始化entity的直接属性,但不会初始化延迟加载的关联集合和属性. 如需初始化关联属性,需执行:
	 * Hibernate.initialize(user.getRoles()),初始化User的直接属性和关联集合.
	 * Hibernate.initialize
	 * (user.getDescription()),初始化User的直接属性和延迟加载的Description属性.
	 */
	public void initProxyObject(Object proxy);
}

 

 4、DAO分页查询,复杂条件查询接口

 

public interface BaseEntityDao<Entity,PK extends Serializable> extends BaseDao<Entity,PK>{
	/**
	 * 分页获取全部对象.
	 */
	public Page<Entity> findAllByPage(final Page<Entity> page);
}

 5、service基础接口,返回值封装类,主要针对提供webservice接口

 

public class RetdObj<RealObj> implements Serializable {

    private static final long serialVersionUID = 132984918234911234L;

    public static final int PASSCODE_MIN = 200;
    
    public static final int PASSCODE_MAX = 299;

    private RealObj realObj;

    private RetdCodeType retdCodeType;
    
    public RetdObj(){
        this.setCode(RetdCodeType.NODEFINE);
    }

    public RetdObj(RealObj realObj) {
        this.setCode(RetdCodeType.NODEFINE);
        this.realObj = realObj;
    }

    /**
     * service layer set retdCodeType
     * 
     * @param retdCodeType
     */
    public void setCode(RetdCodeType retdCodeType) {
        this.retdCodeType = retdCodeType;
    }

    /**
     * the page or action use this method
     * 
     * @return
     */
    public int getCode() {
        return retdCodeType.getCode();
    }
    
    public void setRealObj(RealObj realObj) {
        this.realObj = realObj;
    }

    /**
     * the page or action use this method get the real object
     * @return
     */
    public RealObj getRealObj() {
        int retdCode = retdCodeType.getCode();
        if(retdCode>=PASSCODE_MIN&&retdCode<=PASSCODE_MAX)return realObj;
        else return null;
    }

}

 6、状态码封装类

 

/**
 *
 * 
 * 0--未定义--初始化
 * 200--成功
 * 201--成功,但是realobj无内容
 * 500--参数异常
 * 501--应用异常
 * 502--无权限
 * 600--未知异常
 */

public enum RetdCodeType {
    
    NODEFINE(0), PASS_OK(200), PASS_NODATA(201), EX_PARAM(500),EX_APP(501), EX_AUTH(502),EX_UNKNOWN(600);
    private int code;

    RetdCodeType(int code) {
        this.code = code;
    }

    public int getCode() {
        return this.code;
    }
    
    public static RetdCodeType valueOf(int code) {
    	switch(code) {
    	case 0 :
    		return NODEFINE;
    	case 200 :
    		return PASS_OK;
    	case 201 :
    		return PASS_NODATA;
    	case 500 :
    		return EX_PARAM;
    	case 501 :
    		return EX_APP;
    	case 502 :
    		return EX_AUTH;
    	case 600 :
    		return EX_UNKNOWN;
    	default :
    		return null;
    	}
    }
    
}

 7、service基础增删改查接口,针对webservice

 

public interface BaseService<Entity, PK extends Serializable> {
	/**
	 * 增加实体
	 * 
	 * @param entity
	 * @return
	 */
	public RetdObj<PK> save(Entity entity);

	/**
	 * 更新实体
	 */
	public RetdObj<PK> update(Entity entity);

	/**
	 * 合并实体
	 */
	public RetdObj<PK> merge(Entity entity);

	/**
	 * 更新实体
	 */
	public RetdObj<PK> refresh(Entity entity);

	/**
	 * 按ID删除对象
	 * 
	 * @param id
	 */
	public RetdObj<PK> delete(final PK id);

	/**
	 * 按ID查询对象
	 * 
	 * @param id
	 * @return
	 */
	public RetdObj<Entity> findById(final PK id);

	/**
	 * 按ID查询多个对象
	 * 
	 * @param ids
	 * @return
	 */
	public RetdObj<List<Entity>> findByIds(final Collection<PK> ids);

	/**
	 * 查询全部对象
	 * 
	 * @return
	 */
	public RetdObj<List<Entity>> getAll();

	/**
	 * 分页获取全部对象.
	 */
	public RetdObj<Page<Entity>> findAllByPage(final Page<Entity> page);
}

 8、对mvc前台提供的service基础接口

 

public interface FrontBaseService<Entity, PK extends Serializable> {
	/**
	 * 增加实体
	 * 
	 * @param entity
	 * @return
	 */
	public PK save(Entity entity);

	/**
	 * 更新实体
	 */
	public void update(Entity entity);
	public void saveOrUpdate(Entity entity);
	/**
	 * 合并实体
	 */
	public void merge(Entity entity);

	/**
	 * 更新实体
	 */
	public void refresh(Entity entity);

	/**
	 * 按ID删除对象
	 * 
	 * @param id
	 */
	public void delete(final PK id);

	/**
	 * 按ID查询对象
	 * 
	 * @param id
	 * @return
	 */
	public Entity findById(final PK id);

	/**
	 * 按ID查询多个对象
	 * 
	 * @param ids
	 * @return
	 */
	public List<Entity> findByIds(final Collection<PK> ids);

	/**
	 * 查询全部对象
	 * 
	 * @return
	 */
	public List<Entity> getAll();

	/**
	 * 分页获取全部对象.
	 */
	public Page<Entity> findAllByPage(final Page<Entity> page);

	/**
	 * 批量删除
	 */
	public void delete(PK[] ids);
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics