基于本框架中的封装实现的分页逻辑,用一个例子来说明,为了简单整体结构分为三层(controller、service、dao),省略掉了grpc。以下注释中有******的是和业务相关的写法,需自定义。
@Slf4j
@RestController
@RequestMapping("/datadbase")
public class DatabaseController {
@Autowired
private IDatabaseService iDatabaseService;
/**分页查询数据库:
参数:@RequestBody(required = false) PageParamReq page 中的required可省略,省略后默认为必填项
*/
@PostMapping("/v1/page")
public PageResponse<List<DatabaseVo>> page(@RequestBody(required = false) PageParamReq page,
@RequestParam(name = "name", required = false) String name,
@RequestParam(name = "type", required = false) String type,
@RequestParam(name = "status", required = false) String status) {
//CommonVoTranslator和DatabaseVoTranslator是用MapStruct实现的实体转换类,固定写法
PageBo<DatabaseBo> pageBo = iDatabaseService.pageSearch(CommonVoTranslator.INSTANCE.pageReq2PageQueryBo(page), name, type, status);
//********转换Bo结果数据为Vo,需自定义转换类********
List<DatabaseVo> databaseVos = DatabaseVoTranslator.INSTANCE.boToVos(pageBo.getData());
//定义HTTP响应对象并,封装返回数据,固定写法
PageResponse<List<DatabaseVo>> response = PageUtil.convertPageBo(pageBo);
response.setData(databaseVos);
//返回给页面端
return response;
}
public interface DatabaseVoTranslator {
DatabaseVoTranslator INSTANCE = Mappers.getMapper(DatabaseVoTranslator.class);
DatabaseVo boToVo(DatabaseBo var1);
List<DatabaseVo> boToVos(List<DatabaseBo> var1);
}
public PageBo<DatabaseBo> pageSearch(PageQueryBo pageParam, String dataBaseName, String dataBaseType, String status) {
Integer dataBaseTypeV = null;
Integer statusV = null;
if(StrUtil.isNotEmpty(dataBaseType)){
dataBaseTypeV = DataServerStructMapperUtil.getDataBaseType(dataBaseType);
}
if(StrUtil.isNotEmpty(status)){
statusV = DataServerStructMapperUtil.getStatus(status);
}
//构建查询参数,固定写法
IPage<DatabaseEntity> iPage = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());
//********查询数据********
iPage = iDatabaseDao.pageDatabase(iPage, dataBaseName, dataBaseTypeV, statusV);
//处理返回结果,固定写法
PageBo<DatabaseBo> reuslt = PageUtil.convertMyabtisIpage(iPage);
//********转换Bo结果数据为Vo,需自定义转换类********
List<DatabaseBo> databaseBos = DatabaseTranslator.INSTANCE.entityToBos(iPage.getRecords());
reuslt.setData(databaseBos);
return reuslt;
}
public interface DatabaseTranslator {
DatabaseTranslator INSTANCE = Mappers.getMapper(DatabaseTranslator.class);
DatabaseBo entityToBo(DatabaseEntity var1);
List<DatabaseBo> entityToBos(List<DatabaseEntity> var1);
}
DAO实现中的Mapper为可选,一般没有特殊查询参数时可以直接硬编码的方式实现。
public IPage<DatabaseEntity> pageDatabase(IPage<DatabaseEntity> iPage, String name, Integer type, Integer status) {
//********需自定义实现********
IPage<DatabaseEntity> result = this.getBaseMapper().pageDatabase(iPage, name, type, status);
return result;
}
public interface DatabaseMapper extends BaseMapper<DatabaseEntity> {
IPage<DatabaseEntity> pageDatabase(IPage<DatabaseEntity> iPage,
@Param("databaseName") String name,
@Param("type") Integer type,
@Param("status") Integer status);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="XXXXXXXXXX.DatabaseMapper">
<select id="pageDatabase" resultType="XXXXXXXXXX.DatabaseEntity">
select
t_database.*
from
t_database
where 1=1
<if test = "databaseName!=null">
and upper(t_database.name) LIKE upper(CONCAT('%',#{databaseName},'%'))
</if>
<if test = "type!=null">
and t_database.type = #{type}
</if>
<if test = "status!=null">
and t_database.status = #{status}
</if>
</select>
</mapper>