顶级类和接口的设计

/**
* @ClassName: GenericDao
* @Description: 所有自定义Dao的顶级接口, 封装常用的增删查改操作
* Entity : 代表数据库中的表 映射的Java对象类型
* PK :代表对象的主键类型
* Page:分页对象
* @author: yaozhenhua
* @date: 2019/3/20 11:08
*/
public interface GenericDao<Entity, PK, Page> {
/**
* 插入对象
*
* @param entity
* @author: yaozhenhua 2019/3/20 11:14
*/
int insertSelective(Entity entity);

/**
*更新对象
*
* @param entity
* @author: yaozhenhua 2019/3/20 11:16
*/
int updateSelectiveByPrimaryKey(Entity entity);

/**
*通过主键删除对象
*
* @param id
* @author: yaozhenhua 2019/3/20 11:18
*/
int deleteByPrimaryKey(@Param(value = "id") PK id);

/**
*通过主键查询对象
*
* @param id
* @author: yaozhenhua 2019/3/20 11:20
*/
Entity selectByPrimaryKey(@Param(value = "id") PK id);

/**
*查询列表
*
* @param
* @author: yaozhenhua 2019/3/20 11:26
*/
List<Entity> listSelective();

/**
*分页查询列表
*
* @param page
* @author: yaozhenhua 2019/3/20 11:26
*/
List<Entity> listSelectiveByPage(Page page);

/**
*查询个数
*
* @param
* @author: yaozhenhua 2019/3/20 11:28
*/
int countSelective();

}

/**
* @ClassName: GenericService
* @Description: 所有自定义Service的顶级接口, 封装常用的增删查改操作
* @author: yaozhenhua
* @date: 2019/3/20 11:29
*/
public abstract class GenericService<Entity, PK, Page> {
public abstract GenericDao<Entity, PK, Page> getDao();

/**
*插入对象
*
* @param entity
* @author: yaozhenhua 2019/3/20 12:07
*/
public int insert(Entity entity){
return getDao().insertSelective(entity);
}

/**
*更新对象
*
* @param entity
* @author: yaozhenhua 2019/3/20 12:09
*/
public int update(Entity entity){
return getDao().updateSelectiveByPrimaryKey(entity);
}

/**
*删除对象
*
* @param id
* @author: yaozhenhua 2019/3/20 12:10
*/
public int delete(PK id){
return getDao().deleteByPrimaryKey(id);
}

/**
*通过主键, 查询对象
*
* @param id
* @author: yaozhenhua 2019/3/20 12:11
*/
public Entity selectById(PK id){
return getDao().selectByPrimaryKey(id);
}

/**
*查询列表
*
* @param
* @author: yaozhenhua 2019/3/20 12:13
*/
public List<Entity> listSelective(){
return getDao().listSelective();
}

/**
*查询列表并分页
*
* @param page
* @author: yaozhenhua 2019/3/20 12:14
*/
public List<Entity> listSelectiveByPage(Page page){
return getDao().listSelectiveByPage(page);
}

/**
*查询个数
*
* @param
* @author: yaozhenhua 2019/3/20 12:15
*/
public int countSelective(){
return getDao().countSelective();
}
}

猜你喜欢

转载自www.cnblogs.com/gavin-yao/p/10567300.html