分页查询的俩种方式(QueryWrapper和xml分页)

1.单表查分页时推荐使用QueryWrapper

单表分页查询可以使用这种的,我用的是lambda表达式

QueryWrapper<Vergetable> wrapper = new QueryWrapper<>();

        Page<table> page = new Page<>(dto.getPageNo(), dto.getPageSize());
//        page.setOptimizeCountSql(false);

        //蔬菜编号
        if (StringUtils.isNotBlank(dto.getVegCode())) {
            wrapper.lambda().eq(table::getVegCode, dto.getVegCode());
        }
        //品种id!
        if (StringUtils.isNotBlank(dto.getTypeId())) {
            wrapper.lambda().eq(table::getTypeId, dto.getTypeId());
        }
  IPage<Vergetable> IPage = Mapper.selectPage(page, wrapper);

2.当使用关联查询时推荐使用xml的方式

  • 1.首次idea中要安装 mybatis plugin插件 这个是mapper生成xml 关联的工具

  • 这个是mapper.java的对应的格式,如果需要条件查询 就仿造第三个

															//id是impl中传过来的名称
    Page<需要返回的类> selectByList(Page<需要分页查询的主类表> page);
																			//与impl中的参数对应
    ContainerTypeCellVo selectByVo(@Param("id") String id);

	//通过条件查询 分页 示例
	Page<Vo> selectByList(Page<bo> page,@Param("dto") Dto dto);
  • 这个是xml 中对应的部分,因为我的字段在俩个表中有重复的,所以给字段配置了别名
 <select id="selectByList" resultMap="ContainerTypeCellVo">
        select c.*,
        p.length,p.width,p.high,p.node,p.code as pcode
         from container_type c,plant_cell_type p
    </select>
	
	//resultMap 与 下边 封装的参数  id 对应
	//id与上边mapper.java中的方法名称要一样
    <select id="selectByVo" resultMap="ContainerTypeCellVo">
        select c.*,
        p.length,p.width,p.high,p.node,p.code as pcode
         from container_type c left join plant_cell_type p on c.plant_cell_type_id = p.id
         where c.id = #{id}
    </select>

		//这个是需要封装到实体类的参数
		//type是封装类的路径
    <resultMap id="ContainerTypeCellVo" type="vo.Vo">
        <result column="id" property="id" />
        <result column="created_on" property="createdOn" />
        <result column="updated_on" property="updatedOn" />
        <result column="floor" property="floor" />
        <result column="col" property="col" />
        <result column="code" property="code" />
        <result column="plant_cell_type_id" property="plantCellTypeId" />
        <result column="pcode" property="cellCode" />
    </resultMap>

2.impl类中格式如下


    //分页查询
    public Page<ContainerTypeCellVo> selectByPage(Dto dto) {
        Page<ContainerType> page = new Page<>(dto.getPageNo(),dto.getPageSize());
        return Mapper.selectByList(page);
    }

Page中 total 就是查到的总数,records就是查到的集合,current就是当前页数,size就是每页的大小

在这里插入图片描述

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓦然回首的风度

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值