springboot将数据库数据分页显示

    技术2022-07-14  84

    1.bean 数据库中表名为score;按照表的字段来显示的。

    public class Score { private String scoreID; private String scoreName; ......

    2.mapper findScoreById:id参数是准备从url中获取的,到时候前端url输入:localhost:8080/…/id=…即可 findAll:传入参数RowBounds,为实现分页对象

    @Repository public interface ScoreMapper { @Select("select * from score where scoreID=#{id}") Score findScoreById(int id); @Select("select * from score ") List<Score> findAll(RowBounds rb); }

    3.service

    @Service public interface ScoreService { Score findScoreById(int id); List<Score> findAll(RowBounds rb); } @Service public class ScoreServiceImpl implements ScoreService { @Autowired ScoreMapper scoreMapper; @Override public Score findScoreById(int id) { return scoreMapper.findScoreById(id); } @Override public List<Score> findAll(RowBounds rowBounds) { return scoreMapper.findAll(rowBounds); } }

    4.controller findAll:传入参数rowBound, RowBounds rowBound = new RowBounds(1,5);

    @Controller public class ScoreController { @Autowired ScoreServiceImpl scoreService; @RequestMapping(value = "/findById/{id}") @ResponseBody public String findById(@PathVariable("id") int id){ Score score=scoreService.findScoreById(id); return score.getScoreName(); } @RequestMapping("/findAll") @ResponseBody public Object findAll(HttpServletRequest request){ RowBounds rowBound = new RowBounds(1,5); List<Score> score= scoreService.findAll(rowBound); return score; } }

    5.url中输入 loaclhost:8080/findById/ID=1001 loaclhost:8080/findAll实现分页,从1开始,每页显示5条数据;

    Processed: 0.011, SQL: 9