hs-web中通用controller在hsweb-commons-controller模块中
最基础的有四个接口:QueryController、UpdateController、CreateController和DeleteController
QueryController中定义了通用的方法,如根据条件查询、根据条件分页查询、根据条件统计、根据主键查询等。所有方法都注解了Authorize,不需要鉴权的方法则设置ignore为true。泛型查询条件则继承了Entity
/**
* 通用查询控制器。
*
* @param <E> 实体类型
* @param <PK> 主键类型
* @param <Q> 查询条件实体类型,默认提供{@link QueryParamEntity}实现
* @author zhouhao
* @see QueryParamEntity
* @see 3.0
*/
public interface QueryController<E, PK, Q extends Entity> {
@Authorize(ignore = true)
<T extends QueryByEntityService<E> & QueryService<E, PK>> T getService();
@Authorize(action = Permission.ACTION_QUERY)
@GetMapping
@ApiOperation(value = "根据动态条件查询", responseReference = "get")
default ResponseMessage<PagerResult<E>> list(Q param) {
return ok(getService().selectPager(param));
}
@Authorize(action = Permission.ACTION_QUERY)
@GetMapping("/no-paging")
@ApiOperation(value = "不分页动态查询", responseReference = "get")
default ResponseMessage<List<E>> listNoPaging(Q param) {
if (param instanceof QueryParamEntity) {
((QueryParamEntity) param).setPaging(false);
}
return ok(getService().select(param));
}
@Authorize(action = Permission.ACTION_QUERY)
@GetMapping("/count")
@ApiOperation(value = "根据动态条件统计", responseReference = "get")
default ResponseMessage<Integer> count(Q param) {
return ok(getService().count(param));
}
@Authorize(action = Permission.ACTION_GET)
@GetMapping(path = "/{id:.+}")
@ApiOperation("根据主键查询")
default ResponseMessage<E> getByPrimaryKey(@PathVariable PK id) {
return ok(assertNotNull(getService().selectByPk(id)));
}
@Authorize(action = Permission.ACTION_GET)
@GetMapping(path = "/ids")
@ApiOperation("根据主键查询多条记录")
default ResponseMessage<List<E>> getByPrimaryKey(@RequestParam List<PK> ids) {
return ok(assertNotNull(getService().selectByPk(ids)));
}
@Authorize(ignore = true)
static <T> T assertNotNull(T obj) {
if (null == obj) {
throw new NotFoundException("{data_not_exist}");
}
return obj;
}
}
CreateController、UpdateController、DeleteController也类似,增加了模型到实体转化的modelToEntity抽象方法
CrudController继承了四个基础的controller接口,提供了默认的模型到实体转化方法
public interface CrudController<E, PK, Q extends Entity, M>
extends QueryController<E, PK, Q>
, UpdateController<E, PK, M>
, CreateController<E, PK, M>
, DeleteController<E,PK> {
@Override
@SuppressWarnings("unchecked")
@Authorize(ignore = true)
CrudService<E, PK> getService();
@Override
@Authorize(ignore = true)
default E modelToEntity(M model, E entity) {
return FastBeanCopier.copy(model, entity);
}
}
SimpleCrudController和CrudController接口类似,不过modelToEntity方法直接返回了模型,不需要进行别的映射
public interface SimpleCrudController<E, PK, Q extends Entity>
extends QueryController<E, PK, Q>
, UpdateController<E, PK, E>
, CreateController<E, PK, E>
, DeleteController<E,PK> {
@Override
@SuppressWarnings("unchecked")
@Authorize(ignore = true)
CrudService<E, PK> getService();
@Override
@Authorize(ignore = true)
default E modelToEntity(E model, E entity) {
// model = entity
return model;
}
}
GenericEntityController接口继承了CrudController,对应的实体泛型继承的是GenericEntity
SimpleGenericEntityController接口继承了SimpleCrudController,对应的实体泛型继承的是GenericEntity
后续所有的controller都实现了这几个接口中的某一个,具体要看对应的业务逻辑
hs-web中通用service接口在hsweb-commons-service-api模块中
最基础的接口:QueryService、UpdateService、InsertService、DeleteService、CreateEntityService和QueryByEntityService。提供了基本的方法定义。它们都继承自标志接口Service
CrudService接口继承了上述6个基础接口
除了上面几个基础接口外,还提供了TreeService接口,提供了对树的基础操作
通用实现类在hsweb-commons-service-simple模块中
DefaultQueryByEntityService接口继承QueryByEntityService,提供了默认的实现
DefaultDSLQueryService接口继承DefaultQueryByEntityService和QueryService,增加了QueryService接口的默认实现
DefaultDSLUpdateService接口继承UpdateService,增加了createUpdate方法
DefaultDeleteService接口继承DeleteService,没有任何默认实现
DefaultDSLDeleteService接口继承DefaultDeleteService,增加了createDelete方法
GenericService接口继承DefaultDSLQueryService、DefaultDSLUpdateService、DefaultDSLDeleteService和CrudService
AbstractService抽象类实现了CreateEntityService接口。增加了通用的验证方法
GenericEntityService抽象类继承AbstractService,实现了GenericService接口
EnableCacheAllEvictGenericEntityService抽象类继承GenericEntityService接口,对每个方法增加了Cacheable或CacheEvict注解
类层次结构图如下

后续所有的service都会继承或实现上面提到的类或接口
hs-web中通用dao在hsweb-commons-dao-api模块中
通用接口有如下几个:InsertDao、DeleteDao、DeleteByEntityDao、QueryByEntityDao和UpdateByEntityDao
CrudDao接口为通用接口的集成