使用layui+springboot分页demo(第一版)

本文介绍如何使用layui分页插件实现前后端数据交互,包括前端代码配置、后端数据处理及数据库查询,展示了完整的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.前台使用layui分页,官方文档展示如下:

就是用这个分页条来进行分页

2.完成的效果如下:

不理解的部分可以看一下官方帮助文档,说白了就是将官方静态分页效果用后台数据进行填充而已

注意:分页条用的layui,所以需要引入一套layui,表格用的bootstrap表格的类,请引入bootstrap,当然首先必须引入jquery

3.下面是前台代码:前台向后台传递俩参数,一个curr,一个limit

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="js/layui/css/layui.css">
    <link rel="stylesheet" href="js/bootstrap-table.min.css"></link>
    <link rel="stylesheet" href="js/bootstrap.min.css"></link>
</head>
<body>
<table class="table table-striped">
    <thead>
    <th>优惠券类型</th>
    <th>发放对象</th>
    <th>发放时间</th>
    <th>发放面额</th>
    <th>每用户发放数量</th>
    <th>有效期</th>
    </thead>
    <tbody id="tbs">

    </tbody>
</table>

<div id="demo8"></div>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/bootstrap-table.min.js"></script>
<script src="js/layui/layui.js"></script>
<script>
    $(function(){
        fenye();
    })
    function fenye(){
        layui.use(['laypage', 'layer'], function(){
            var laypage = layui.laypage
                ,layer = layui.layer;
            //自定义排版
            $.ajax({
                type:'post',
                dataType:'json',
                data:{'curr':1,'limit':10},
                url:'../../DataShow3',
                success:function(data){
                    showData(data);
                    laypage.render({
                        elem: 'demo8'
                        ,count: data.ct
                        ,layout: ['limit', 'prev', 'page', 'next']
                        ,jump:function(obj){
                            //分页切换的回掉
                           $.ajax({
                               type:'post',
                               dataType:'json',
                               data:{'curr':obj.curr,'limit':obj.limit},
                               url:'../../DataShow3',
                               success:function(data){
                                   showData(data);
                               }
                           })
                        }
                    });
                }
            })
        });
    }
function showData(ds){
    $("#tbs").empty();
    var htmlStr="";
for(var i=0;i<ds.data.length;i++){
    htmlStr+='<tr><td>';
    htmlStr+=ds.data[i].id;
    htmlStr+='</td><td>';
    htmlStr+=ds.data[i].className;
    htmlStr+='</td><td>';
    htmlStr+=ds.data[i].classSumNum;
    htmlStr+='</td><td>';
    htmlStr+=ds.data[i].teacherName;
    htmlStr+='</td><td>';
    htmlStr+=ds.data[i].fdyName;
    htmlStr+='</td><td>';
    htmlStr+=ds.data[i].fdyNum;
    htmlStr+='</td></tr>'

}
    $("#tbs").append(htmlStr);
}
</script>
</body>
</html>

4.controller代码:

@ResponseBody
@RequestMapping("/DataShow3")
public Map<String,Object> fdf(int curr, int limit){
    Map<String,Object> map=new HashMap<String,Object>();
    List<ClassTable> ct=dataShowService.queryInfox(curr,limit);
    //总数居条数
    int countx=dataShowService.queryAllCountxx();
    map.put("data",ct);
    map.put("ct",countx);
    return map;
}

5.service代码:

public interface DataShowService {
    
    List<ClassTable> queryInfox(int curr, int limit);

    int queryAllCountxx();
}

6.serviceImpl代码

public class DataShowServiceImpl implements DataShowService {
    @Autowired
    private DataShowServiceMapper dataShowServiceMapper;
    

    @Override
    public List<ClassTable> queryInfox(int curr, int limit) {
         curr=(curr-1)*limit;
        return dataShowServiceMapper.queryInfox(curr,limit);
    }

    @Override
    public int queryAllCountxx() {
        return dataShowServiceMapper.queryAllCountxx();
    }
}

注意:这里根据当前页算出起始数据是第几条

7.dao代码:

public interface DataShowServiceMapper {

    List<ClassTable> queryInfox(@Param("curr")int curr, @Param("limit")int limit);

    int queryAllCountxx();
}

8.mapper代码:

 <select id="queryInfox" resultType="com.ansheng.entity.ClassTable">
select * from classtable limit #{curr},#{limit}
    </select>
    <select id="queryAllCountxx" resultType="java.lang.Integer">
          select count(*) from classtable
    </select>

9.结束了,是不是很简单

感兴趣的小伙伴可以扫码关注下公众号哦,公众号会分享高质量的技术文章哦

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

酒书

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

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

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

打赏作者

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

抵扣说明:

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

余额充值