关于分页查询数据表中数据返回总记录数问题

本文介绍了在Java中进行分页查询时,如何通过Dao层的mapper接口和mapper.xml映射文件来获取数据表中的总记录数。重点强调了resultType属性的设置,对于返回具体字段与数值的情况,分别应该使用实体类全路径和数值对应的数据类型或其包装类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dao层

mapper接口

@MyBatisDao
public interface ExtendMapper {

    /**
     * 分页查询列表
     * @param sourceName 按sourceName模糊查询列表
     * @param sourceKey 按sourceKey模糊查询列表
     * @return 分页查询列表结果
     */
    List<ExtendKeyMapping> queryExtendKeyMappingList(@Param("sourceName") String sourceName,
                                                     @Param("sourceKey") String sourceKey,
                                                     @Param("pageSize") Integer pageSize,
                                                     @Param("offset") Integer offset);

     /**
     * 查询列表总记录数
     * @param sourceName
     * @param sourceKey
     * @return 列表总记录数
     */
    Integer queryExtendKeyMappingListCount(@Param("sourceName") String sourceName,
                                           @Param("sourceKey") String sourceKey);
}

mapper.xml映射文件

<!-- 分页查询列表内容 -->
<select id="queryExtendKeyMappingList" resultType="com.emrubik.domain.ExtendKeyMapping">
    select id,
           source_name,
           source_key,
           url,
           uc_app_id,
           uc_app_secret,
           app_key
    from wbmp.wbmp_app_extend_key_mapping_t
    where 1=1
     <if test="sourceName != null or sourceName != ''">
         and source_name like '%${sourceName}%'
     </if>
     <if test="sourceKey != null or sourceKey != ''">
         and source_key like '%${sourceKey}%'
     </if>
    order by id
    limit #{pageSize,jdbcType=integer} offset #{offset,jdbcType=integer}
</select>

<!-- 查询列表总记录数 -->
<select id="queryExtendKeyMappingListCount" resultType="Integer">
    select count(*)
    from wbmp.wbmp_app_extend_key_mapping_t
    where 1=1
        <if test="sourceName != null or sourceName != ''">
         and source_name like '%${sourceName}%'
     </if>
     <if test="sourceKey != null or sourceKey != ''">
         and source_key like '%${sourceKey}%'
     </if>
</select>
注意:resultType属性值为返回结果的数据类型
(1)当查询结果为具体字段时,resultType属性值为实体类全路径
(2)当查询结果为具体数值时,resultType属性值为数值对应的数据类型或其包装类

Service层

@Service
public class ExtendService {

    @Autowired
    ExtendMapper extendMapper;

   /**
     * 查询列表
     * @param sourceName 按sourceName模糊查询列表
     * @param sourceKey 按sourceKey 模糊查询列表
     * @param pageNo 当前页码
     * @param pageSize 当前页码记录数
     * @return 分页查询列表结果及列表总记录数
     * @throws WBMPException
     */
    public QueryResp<ExtendKeyMapping> queryExtendKeyMappingList(String sourceName,
                                                                 String sourceKey,
                                                                 Integer pageNo,
                                                                 Integer pageSize) 
        throws WBMPException {
        // 声明返回对象resp
        QueryResp<ExtendKeyMapping> resp = new QueryResp<>();
        try {
         	// 分页查询列表
            List<ExtendKeyMapping> mappingList = extendMapper.
            queryExtendKeyMappingList(sourceName,sourceKey, pageSize,(pageNo-1)*pageSize);
            // 查询列表总记录数                                                                                                                                                                            
            Integer mappingCount = extendMapper.
            queryExtendKeyMappingCount(sourceName, sourceKey);
            // 封装查询列表总记录数
            resp.setCount(Long.valueOf(mappingCount));
            // 封装分页查询列表
            resp.setQueryData(mappingList);
        } catch (Throwable e) {
            log.error("ExtendService.queryExtendKeyMappingList:queryList fail", e);
            throw new WBMPException("查询列表内容失败", e, WBMPCode.RESULT_CODE__INNER_ERROR);
        }
        // 返回分页查询列表结果及列表总记录数
        return resp;
    }
}

Controller层

@RestController
public class ExtendController {

    @Autowired
    ExtendService extendService;

    /**
     * 查询列表
     * @param sourceName 按sourceName模糊查询列表
     * @param sourceKey 按sourceKey 模糊查询列表
     * @param pageNo 当前页码
     * @param pageSize 当前页码记录数
     * @return 分页查询列表结果及列表总记录数
     * @throws WBMPException
     */
    @CRUDRest(type = CRUDRest.Type.Read)
    @ApiOperation(value = "查询列表", 
                  httpMethod = "GET", 
                  response = ExtendKeyMapping.class, 
                  notes = "查询列表",
                  responseContainer = "QueryResp", 
                  produces = "application/json; charset=utf-8")
    @ApiResponses({@ApiResponse(code = 200, 
                                message = "查询列表成功", 
                                responseContainer = "QueryResp",
                                response = ExtendKeyMapping.class)})
    public QueryResp<ExtendKeyMapping> queryExtendKeyMappingList(@RequestParam(value = "sourceName",required = false) String sourceName,
                                                                 @RequestParam(value = "sourceKey",required = false) String sourceKey,
                                                                 @RequestParam(value = "pageNo")  Integer pageNo,
                                                                 @RequestParam(value = "pageSize") Integer pageSize) throws WBMPException {
         return extendService.queryExtendKeyMappingList(sourceName,sourceKey,pageNo,pageSize);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值