美文网首页
mybatis-plus分页查询

mybatis-plus分页查询

作者: 墨色尘埃 | 来源:发表于2018-12-18 16:03 被阅读47次

如果只是单表,那么分页查询就容易的多了
这里的@ModelAttribute注解可以将前端传过来的currentsize字段映射到Page对象中
BaseController中

    /**
     * @param page 查询一般传入参数为current和size, 例如/listPage?current=1&size=5,
     * @return 返回分页数据
     */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public ResponseObj<Page<T>> listPage(@ModelAttribute Page<T> page, @ModelAttribute T model) {
        Page<T> pageList = service.selectPage(page, new EntityWrapper<>(model));
        for (T eachObj : pageList.getRecords()) {
            queryFilter(eachObj);
        }
        return new ResponseObj<>(pageList, RetCode.SUCCESS);
    }

关联多表分页查询
PbBuildingController中

@TargetDataSource("ds3")
@RestController
@RequestMapping("/api/pbBuilding")
public class PbBuildingController extends /*SingleRecordControl<PbBuilding>*/
        BaseController<PbBuildingService,
                PbBuildingMapper,
                PbBuilding, Long> {
    /**
     * 多表关联分页查询
     *
     * @param page  分页参数
     * @param model 搜索条件
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseObj<Page<PbBuilding>> list(@ModelAttribute Page<PbBuilding> page,
                                              @ModelAttribute PbBuilding model) {
        Page<PbBuilding> pageList = service.list(page, new EntityWrapper<>(model));
        return new ResponseObj<>(pageList, RetCode.SUCCESS);
    }
}

PbBuildingService

@Service
public class PbBuildingService extends ServiceImpl<PbBuildingMapper, PbBuilding> {

    public Page<PbBuilding> list(Page<PbBuilding> page, Wrapper<PbBuilding> wrapper) {
        return page.setRecords(baseMapper.list(page, wrapper));
    }
}

PbBuildingMapper

@Mapper
public interface PbBuildingMapper extends BaseMapper<PbBuilding> {
    int deleteByPrimaryKey(String buildId);

//    int insert(PbBuilding record);

    int insertSelective(PbBuilding record);

    PbBuilding selectByPrimaryKey(String buildId);

    int updateByPrimaryKeySelective(PbBuilding record);

    int updateByPrimaryKey(PbBuilding record);

    List<PbBuilding> list(RowBounds rowBounds, @Param("ew") Wrapper<PbBuilding> wrapper);
}

PbBuildingMapper.xml

  <select id="list" resultType="com.cicdi.servertemplate.modules.baselibrary.model.PbBuilding">
    SELECT
        pb.*, pc.COMM_NAME,pc.COMM_CODE,
        px.XIAOQU_NAME
    FROM
        pb_building pb
    LEFT JOIN pb_community pc ON pb.COMMUNITY_ID = pc.COMMUNITY_ID
    AND pc.IS_DELETE = 0
    LEFT JOIN pb_xiaoqu px ON pb.XIAOQU_ID = px.XIAOQU_ID
    AND px.IS_DELETE = 0
    WHERE (pb.IS_DELETE = '0')
    <if test="ew!=null">
      <if test="ew.entity!=null">
        <if test="ew.entity.buildName!=null">
          AND pb.BUILD_NAME LIKE CONCAT('%',#{ew.entity.buildName},'%')
        </if>
        <if test="ew.entity.buildAddr!=null">
          AND pb.BUILD_ADDR LIKE CONCAT('%',#{ew.entity.buildAddr},'%')
        </if>
        <if test="ew.entity.tel!=null">
          AND pb.TEL LIKE CONCAT('%',#{ew.entity.tel},'%')
        </if>
        <if test="ew.entity.houseNo!=null">
          AND pb.HOUSE_NO LIKE CONCAT('%',#{ew.entity.houseNo},'%')
        </if>
      </if>
      <if test="ew.sqlSegment!=null">
        ${ew.sqlSegment}
      </if>
    </if>
  </select>

如果仅仅查列表而不需要分类,前端不需要传current和size参数,而后台不需要Page参数

    /**
     * 返回列表
     *
     * @return
     */
    @RequestMapping(value = "/list1", method = RequestMethod.GET)
    public ResponseObj<List<T>> list1(@ModelAttribute T model) {
        List<T> listModel = service.selectList(new EntityWrapper<>(model));
        for (T eachObj : listModel) {
            queryFilter(eachObj);
        }
        return new ResponseObj<>(listModel, RetCode.SUCCESS);
    }

相关文章

网友评论

      本文标题:mybatis-plus分页查询

      本文链接:https://www.haomeiwen.com/subject/ohdlkqtx.html