基于ssm框架实现layui分页效果
网络编程 2021-07-04 15:51www.168986.cn编程入门
这篇文章主要为大家详细介绍了基于ssm框架实现layui分页效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
今天完成了基于ssm框架下,layui的分页操作,现在把核心内容分享一下
1.前端页面的关键代码,基于html5
<!--自动渲染--> <table class="layui-table" lay-data="{cellMinWidth:100, height:'full-500', url:'meter/query', page:true, id:'idTest'}" lay-filter="demo"> <thead> <tr> <th lay-data="{type:'checkbox', fixed: 'left'}"></th> <th lay-data="{field:'id',sort:true}">序号</th> <th lay-data="{field:'appId', width:300,sort:true}">应用ID</th> <th lay-data="{field:'serviceId', sort: true}">服务ID</th> <th lay-data="{field:'deviceId', width:300,sort: true}">设备ID</th> <th lay-data="{field:'gatewayId', width:300,sort: true}">网关ID</th> <th lay-data="{field:'status', width:150,sort: true}">数据</th> <th lay-data="{field:'timestamp',width:200,sort: true}">时间日期</th> <th lay-data="{fixed: 'right', width:178, align:'center', toolbar: '#barDemo'}">数据操作</th> </tr> </thead> </table> <!--每一条记录的操作栏--> <script type="text/html" id="barDemo"> <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a> <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a> <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a> </script> <script src="statics/layui/layui.js" charset="utf-8"></script> <!-- 注意如果你直接复制所有代码到本地,上述js路径需要改成你本地的 --> <script> layui.use(['form','layer','layedit','laydate','upload','table'],function(){ var form = layui.form; layer = parent.layer === undefined ? layui.layer : .layer, laypage = layui.laypage, upload = layui.upload, layedit = layui.layedit, laydate = layui.laydate, $ = layui.jquery, table = layui.table; //监听表格复选框选择 table.on('checkbox(demo)', function(obj){ console.log(obj) }); //监听工具条 table.on('tool(demo)', function(obj){ var data = obj.data; if(obj.event === 'detail'){ layer.msg('ID'+ data.id + ' 的查看操作'); } else if(obj.event === 'del'){ layer.confirm('真的删除行么', function(index){ obj.del(); layer.close(index); }); } else if(obj.event === 'edit'){ layer.alert('编辑行<br>'+ JSON.stringify(data)) } }); var active = { getCheckData: function(){ //获取选中数据 var checkStatus = table.checkStatus('idTest') ,data = checkStatus.data; layer.alert(JSON.stringify(data)); } ,getCheckLength: function(){ //获取选中数目 var checkStatus = table.checkStatus('idTest') ,data = checkStatus.data; layer.msg('选中了'+ data.length + ' 个'); } ,isAll: function(){ //验证是否全选 var checkStatus = table.checkStatus('idTest'); layer.msg(checkStatus.isAll ? '全选': '未全选') } }; $('.demoTable .layui-btn').on('click', function(){ var type = $(this).data('type'); active[type] ? active[type].call(this) : ''; }); }); </script>
2.后端的关键代码
controller层代码
package ..controller; import ..model.Meter; import ..model.ResponseData; import ..service.MeterService; import ..testexampleid.service.dataCollection.MyQueryDeviceHistoryData; import .springframework.stereotype.Controller; import .springframework.web.bind.annotation.RequestMapping; import .springframework.web.bind.annotation.RequestMethod; import .springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; / Created by yankang on 2018/6/4. / @Controller @RequestMapping("/meter") public class MeterController { @Resource private MeterService meterService; @RequestMapping(value="/add",method = RequestMethod.GET) public void addBathMeter(){//方法参数必须可以获取到表单数据 System.out.println("进入addBathMeter的controll层方法"); List<Meter> meterList= new ArrayList<>(); try { meterList= MyQueryDeviceHistoryData.fun(); } catch (Exception e) { e.printStackTrace(); } meterService.addBathMeter(meterList); } @RequestMapping(value="/query",method = RequestMethod.GET) @ResponseBody public ResponseData queryData(int page,int limit){//方法参数必须可以获取到表单数据 System.out.println("进入controller层的meter/query方法"); return meterService.queryData(page,limit); } }
注意是controller层的第二个方法,page和limit是layui框架帮我们自动实现的,默认情况下,page=1,limit=10。page为起始记录的下标,limit为记录的下标,page并不是指页数。
service层代码
package ..service; import ..mapper.MeterMapper; import ..model.Meter; import ..model.ResponseData; import .springframework.stereotype.Service; import .springframework.transaction.annotation.Isolation; import .springframework.transaction.annotation.Propagation; import .springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.HashMap; import java.util.List; import java.util.Map; / Created by yankang on 2018/6/4. / @Service("meterService") public class MeterServiceImpl implements MeterService{ @Resource private MeterMapper meterMapper; @Override @Transactional(propagation = Propagation.REQUIRES_NEW,isolation= Isolation.DEFAULT,rollbackFor=Exception.class) public void addBathMeter(List<Meter> meterList) { System.out.println("进入addBathMeter的service层方法"); meterMapper.addBathClient(meterList); } @Override @Transactional(readOnly = true)//page默认是从1开始的 public ResponseData queryData(int page, int limit) { System.out.println("进入meterService层的queryData方法"); ResponseData rd=new ResponseData(); List<Meter> meterList; System.out.println("page的值"+page+""); page=(page-1)limit; System.out.println("page变换之后的值"+page+""); System.out.println("limit的值"+limit+""); try { rd.setCode("0"); int num=meterMapper.queryCount(); System.out.println("num的值为" + num + ""); String snum=num+""; rd.setCount(snum);//获取记录总数 Map<String,Integer> map = new HashMap<>(); map.put("page",page);//从第几页开始 map.put("limit",limit);//每页显示多少条记录 meterList = meterMapper.queryData(map); rd.setData(meterList); rd.setMsg("请求成功"); }catch(Exception ex){ ex.printStackTrace(); } return rd; } }
由于page并不是指的页码数,所以分页时需要换算,具体怎么换算见上面的代码。
以下是mapper层代码
package ..mapper; import ..model.Client; import ..model.Meter; import .springframework.stereotype.Component; import java.util.HashMap; import java.util.List; import java.util.Map; / Created by Administrator on 2018-04-17. / @Component("MeterMapper") public interface MeterMapper { //增加客户信息 void addBathClient(List<Meter> meterList); //分页查询数据 List<Meter> queryData(Map<String,Integer> map); //查询数据总数 int queryCount(); }
xml对应文件见狼蚁网站SEO优化
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.//DTD Mapper 3.0//EN" "http://mybatis./dtd/mybatis-3-mapper.dtd"> <mapper namespace="..mapper.MeterMapper"> <!--批量增加客户信息--> <insert id="addBathClient" parameterType="java.util.ArrayList" > insert into tb_meter (appId,serviceId,deviceId,gatewayId,status,timestamp) VALUES <foreach collection="list" item="item" separator=","> (#{item.appId},#{item.serviceId},#{item.deviceId},#{item.gatewayId},#{item.status},#{item.timestamp}) </foreach> </insert> <!--分页查询数据,根据limit和page进行分页--> <select id="queryData" parameterType="Map" resultType="..model.Meter"> SELECT id,appId,serviceId,deviceId,gatewayId,status,timestamp FROM tb_meter <if test="page!=null and limit!=null"> limit #{page},#{limit} </if> </select> <!--查询记录总数--> <select id="queryCount" resultType="java.lang.Integer"> SELECT count() FROM tb_meter </select> </mapper>
model层代码
public class Meter implements Serializable{ private Integer id; private String appId; private String serviceId; private String deviceId; private String gatewayId; private Integer status; private String timestamp; public Meter() { } public Meter(Integer id, String serviceId, String appId, String deviceId, String gatewayId, Integer status, String timestamp) { this.id = id; this.serviceId = serviceId; this.appId = appId; this.deviceId = deviceId; this.gatewayId = gatewayId; this.status = status; this.timestamp = timestamp; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getAppId() { return appId; } public void setAppId(String appId) { this.appId = appId; } public String getServiceId() { return serviceId; } public void setServiceId(String serviceId) { this.serviceId = serviceId; } public String getDeviceId() { return deviceId; } public void setDeviceId(String deviceId) { this.deviceId = deviceId; } public String getGatewayId() { return gatewayId; } public void setGatewayId(String gatewayId) { this.gatewayId = gatewayId; } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } public String getTimestamp() { return timestamp; } public void setTimestamp(String timestamp) { this.timestamp = timestamp; } @Override public String toString() { return "Meter{" + "appId='" + appId + '\'' + ", serviceId='" + serviceId + '\'' + ", deviceId='" + deviceId + '\'' + ", gatewayId='" + gatewayId + '\'' + ", status=" + status + ", timestamp='" + timestamp + '\'' + '}'; } }
ResponseData的代码
package ..model; import java.io.Serializable; / Created by yankang on 2018/5/31. / public class ResponseData implements Serializable{ private String code;//成功的状态码,默认0 private String msg;//状态信息的字段名称,默认msg private String count;//数据总数的字段名称,默认count private Object data;//数据列表的字段名称,默认data public ResponseData() { } public ResponseData(String code, String msg, String count, Object data) { this.code = code; this.msg = msg; this.count = count; this.data = data; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCount() { return count; } public void setCount(String count) { this.count = count; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } @Override public String toString() { return "ResponseData{" + "code='" + code + '\'' + ", msg='" + msg + '\'' + ", count='" + count + '\'' + ", data=" + data + '}'; } }
展示效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程