springboot将数据库数据分页显示

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条数据;

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值