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就是每页的大小

在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐