VUE+SpringBoot实现分页功能

本文主要介绍一下 Vue + SpringBoot 中如何实现一个分页列表数据。

1、效果展示

2、VUE代码

VUE之视图定义

<el-row>
            <el-table
                :data="tableData"
                style="width: 100%">
                <el-table-column
                    v-for="(data,index) in tableHeader"
                    :key="index"
                    :prop="data.prop"
                    :label="data.label"
                    :min-width="data['min-width']"
                    :align="data.align">
                </el-table-column>
                <el-table-column
                    label="操作"
                    min-width="240">
                    <template slot-scope="scope">
                        <el-button type="success" size="mini" @click="toRecharge(scope)">充值</el-button>
                        <el-button size="mini" @click="toView(scope)">查看</el-button>
                        <el-button type="primary" size="mini" @click="toEdit(scope)">修改</el-button>
                        <el-button type="danger" size="mini" @click="deleteCard(scope)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <br>
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pagination.pageIndex"
                :page-sizes="[5, 10, 20, 30, 40]"
                :page-size=pagination.pageSize
                layout="total, sizes, prev, pager, next, jumper"
                :total=pagination.total>
            </el-pagination>
</el-row>

重点说明:

数据类型定义:

tableData:定义后台数据模型定义。

tableHeader:定义表格与后台数据绑定关系。

pagination:定义分页数据模型,主要包含(pageIndex:当前页,pageSize:页大小, total:总记录数)

方法定义:

handleSizeChange:更新页大小

handleCurrentChange:更新当前页

VUE之模型定义(data)

tableData: [],
        pagination: {
            pageIndex: 1,
            pageSize: 10,
            total: 0,
        },
        tableHeader: [
                    {
                        prop: 'sid',
                        label: '编号',
                        align: 'left'
                    },
                    {
                        prop: 'password',
                        label: '密码',
                        align: 'left'
                    },
                    {
                        prop: 'state',
                        label: '状态',
                        align: 'left'
                    },
                    {
                        prop: 'money',
                        label: '金额',
                        align: 'left'
                    },
                    {
                        prop: 'studentSid',
                        label: '学生SID',
                        align: 'left'
                    }

                ]

VUE之数据初始化

VUE 方法定义:请求后台数据接口加载相关数据(method)

init () {
        var self = this
         this.$axios({
            method:'post',
            url:'/card/findPage',
            data:{"page":this.pagination.pageIndex,"limit":this.pagination.pageSize},
            headers:{
                'Content-Type':'application/json;charset=utf-8'      //改这里就好了
          }
        }).then(res => {
            console.log(res);
           self.pagination.total = res.data.data.total_count;
           self.tableData = res.data.data.list;

        })
          .catch(function (error) {
            console.log(error)
          })
        },
        handleSizeChange(val) {
                this.pagination.pageSize = val;
                this.pagination.pageIndex = 1;
                this.init();
        },
        handleCurrentChange(val) {
                this.pagination.pageIndex = val;
                this.init();
        },

VUE 声明周期函数定义:调用VUE的方法定义,完成数据初始化过程.

在VUE声明周期函数mounted ()中,调用init ,完成数据初始化过程。

mounted: function () {
      this.init()
 }

3、SpringBoot 代码

entity 定义

package com.zzg.entity;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.zzg.common.BaseModel;

public class TCard extends BaseModel {
    /**
  *
  */
 private static final long serialVersionUID = 1035674221133528445L;

 private Integer sid;

    private String password;

    private String state;

    private BigDecimal money;

    @DateTimeFormat(pattern="yyyy-MM-dd")
    @JsonFormat(pattern="yyyy-MM-dd",timezone="GMT+8")
    private Date starTime;

    @DateTimeFormat(pattern="yyyy-MM-dd")
    @JsonFormat(pattern="yyyy-MM-dd",timezone="GMT+8")
    private Date endTime;

    private Integer studentSid;

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state == null ? null : state.trim();
    }

    public BigDecimal getMoney() {
        return money;
    }

    public void setMoney(BigDecimal money) {
        this.money = money;
    }

    public Date getStarTime() {
        return starTime;
    }

    public void setStarTime(Date starTime) {
        this.starTime = starTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public Integer getStudentSid() {
        return studentSid;
    }

    public void setStudentSid(Integer studentSid) {
        this.studentSid = studentSid;
    }
}

mapper定义

package com.zzg.mapper;

import java.util.List;
import java.util.Map;

import com.zzg.entity.TCard;

public interface TCardMapper {
 int deleteByPrimaryKey(Integer sid);

 int insert(TCard record);

 int insertSelective(TCard record);

 TCard selectByPrimaryKey(Integer sid);

 int updateByPrimaryKeySelective(TCard record);

 int updateByPrimaryKey(TCard record);

 /**
  * 方法拓展
  */
 List<TCard> select(Map<String, Object> parame);

 Integer count(Map<String, Object> parame);

 void batchInsert(List<TCard> list);

 void batchUpdate(List<TCard> list);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zzg.mapper.TCardMapper">
  <resultMap id="BaseResultMap" type="com.zzg.entity.TCard">
    <id column="sid" jdbcType="INTEGER" property="sid" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="state" jdbcType="VARCHAR" property="state" />
    <result column="money" jdbcType="DECIMAL" property="money" />
    <result column="star_time" jdbcType="DATE" property="starTime" />
    <result column="end_time" jdbcType="DATE" property="endTime" />
    <result column="student_sid" jdbcType="INTEGER" property="studentSid" />
  </resultMap>
  <sql id="Base_Column_List">
    sid, password, state, money, star_time, end_time, student_sid
  </sql>
  <sql id="condition">
  </sql>
  <select id="select" parameterType="map" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from t_card
    where 1 = 1
    <include refid="condition"></include>
  </select>
  <select id="count" parameterType="map" resultType="java.lang.Integer">
    select
     count(1)
    from t_card
    where 1 = 1
    <include refid="condition"></include>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from t_card
    where sid = #{sid,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from t_card
    where sid = #{sid,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.zzg.entity.TCard">
    insert into t_card (sid, password, state,
      money, star_time, end_time,
      student_sid)
    values (#{sid,jdbcType=INTEGER}, #{password,jdbcType=VARCHAR}, #{state,jdbcType=VARCHAR},
      #{money,jdbcType=DECIMAL}, #{starTime,jdbcType=DATE}, #{endTime,jdbcType=DATE},
      #{studentSid,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.zzg.entity.TCard">
    insert into t_card
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="sid != null">
        sid,
      </if>
      <if test="password != null">
        password,
      </if>
      <if test="state != null">
        state,
      </if>
      <if test="money != null">
        money,
      </if>
      <if test="starTime != null">
        star_time,
      </if>
      <if test="endTime != null">
        end_time,
      </if>
      <if test="studentSid != null">
        student_sid,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="sid != null">
        #{sid,jdbcType=INTEGER},
      </if>
      <if test="password != null">
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="state != null">
        #{state,jdbcType=VARCHAR},
      </if>
      <if test="money != null">
        #{money,jdbcType=DECIMAL},
      </if>
      <if test="starTime != null">
        #{starTime,jdbcType=DATE},
      </if>
      <if test="endTime != null">
        #{endTime,jdbcType=DATE},
      </if>
      <if test="studentSid != null">
        #{studentSid,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zzg.entity.TCard">
    update t_card
    <set>
      <if test="password != null">
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="state != null">
        state = #{state,jdbcType=VARCHAR},
      </if>
      <if test="money != null">
        money = #{money,jdbcType=DECIMAL},
      </if>
      <if test="starTime != null">
        star_time = #{starTime,jdbcType=DATE},
      </if>
      <if test="endTime != null">
        end_time = #{endTime,jdbcType=DATE},
      </if>
      <if test="studentSid != null">
        student_sid = #{studentSid,jdbcType=INTEGER},
      </if>
    </set>
    where sid = #{sid,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.zzg.entity.TCard">
    update t_card
    set password = #{password,jdbcType=VARCHAR},
      state = #{state,jdbcType=VARCHAR},
      money = #{money,jdbcType=DECIMAL},
      star_time = #{starTime,jdbcType=DATE},
      end_time = #{endTime,jdbcType=DATE},
      student_sid = #{studentSid,jdbcType=INTEGER}
    where sid = #{sid,jdbcType=INTEGER}
  </update>
</mapper>

service 定义

package com.zzg.service;

import java.util.List;
import java.util.Map;

import com.zzg.common.BaseService;
import com.zzg.common.entity.PageDate;
import com.zzg.common.entity.PageParam;
import com.zzg.entity.TCard;

public interface TCardService extends BaseService<TCard> {
 /**
  * 自定义分页
  *
  * @param parame
  * @param rb
  * @return
  */
 public PageDate<TCard> selectPage(Map<String, Object> parame, PageParam rb);

 /**
    *    自定义查询
  * @param parame
  * @return
  */
 public List<TCard> select(Map<String, Object> parame);

 /**
  * 自定义统计
  * @param parame
  * @return
  */
 public Integer count(Map<String, Object> parame);

 /**
  * 自定义批量插入
  * @param list
  */
 public void batchInsert(List<TCard> list);

 /**
  * 自定义批量更新
  * @param list
  */
 public void batchUpdate(List<TCard> list);

 /**
  * 充值记录
  * @param tCard
  */
 public void recharge(TCard tCard);

}
package com.zzg.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzg.common.AbstractService;
import com.zzg.common.entity.PageDate;
import com.zzg.common.entity.PageParam;
import com.zzg.entity.TCard;
import com.zzg.mapper.TCardMapper;
import com.zzg.service.TCardService;

@Service
public class TCardServiceImpl extends AbstractService<TCard> implements TCardService {
 @Autowired
 TCardMapper mapper;

 public int insert(TCard record) {
  // TODO Auto-generated method stub
  return mapper.insert(record);
 }

 public int insertSelective(TCard record) {
  // TODO Auto-generated method stub
  return mapper.insertSelective(record);
 }

 public TCard selectByPrimaryKey(Integer sid) {
  // TODO Auto-generated method stub
  return mapper.selectByPrimaryKey(sid);
 }

 public int updateByPrimaryKeySelective(TCard record) {
  // TODO Auto-generated method stub
  return mapper.updateByPrimaryKeySelective(record);
 }

 public int updateByPrimaryKey(TCard record) {
  // TODO Auto-generated method stub
  return mapper.updateByPrimaryKey(record);
 }

 public void deleteByPrimaryKey(Integer sid) {
  // TODO Auto-generated method stub
  mapper.deleteByPrimaryKey(sid);
 }

 public PageDate<TCard> selectPage(Map<String, Object> parame, PageParam rb) {
  // TODO Auto-generated method stub
  PageHelper.startPage(rb.getPageNo(), rb.getLimit());
  List<TCard> rs = mapper.select(parame);
  PageInfo<TCard> pageInfo = new PageInfo<TCard>(rs);
  return super.page(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());
 }

 public List<TCard> select(Map<String, Object> parame) {
  // TODO Auto-generated method stub
  return mapper.select(parame);
 }

 public Integer count(Map<String, Object> parame) {
  // TODO Auto-generated method stub
  return mapper.count(parame);
 }

 public void batchInsert(List<TCard> list) {
  // TODO Auto-generated method stub
  mapper.batchInsert(list);
 }

 public void batchUpdate(List<TCard> list) {
  // TODO Auto-generated method stub
  mapper.batchUpdate(list);
 }

 public void recharge(TCard tCard) {
  // TODO Auto-generated method stub
  TCard object = mapper.selectByPrimaryKey(tCard.getSid());
  BigDecimal money = object.getMoney().add(tCard.getMoney());
  object.setMoney(money);
  mapper.updateByPrimaryKeySelective(object);
 }

}

controller定义

package com.zzg.controller;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zzg.common.AbstractController;
import com.zzg.common.entity.PageDate;
import com.zzg.common.entity.PageParam;
import com.zzg.common.entity.Result;
import com.zzg.entity.TCard;
import com.zzg.service.TCardService;

@Controller
@RequestMapping("/api/card")
public class CardController extends AbstractController {
 // 日志记录
  public static final Logger log = LoggerFactory.getLogger(CardController.class);

  @Autowired
  TCardService cardService;

  @RequestMapping(value = "/findPage", method = RequestMethod.POST)
  @ResponseBody
  public Result findPage(@RequestBody Map<String, Object> parame) {
   PageParam rb = super.initPageBounds(parame);
   PageDate<TCard> pageList = cardService.selectPage(parame, rb);
   return new Result().ok().setData(pageList);
  }

  @RequestMapping(value = "/find", method = RequestMethod.GET)
  @ResponseBody
  public Result find() {
   List<TCard> list = cardService.select(null);
   return new Result().ok().setData(list);
  }

  @RequestMapping(value = "/findBySid/{sid}", method = RequestMethod.GET)
  @ResponseBody
  public Result findBySid(@PathVariable("sid") Integer sid) {
   TCard object = cardService.selectByPrimaryKey(sid);
   return new Result().ok().setData(object);
  }

  @RequestMapping(value = "/deleteBySid/{sid}", method = RequestMethod.GET)
  @ResponseBody
  public Result deleteBySid(@PathVariable("sid") Integer sid) {
   cardService.deleteByPrimaryKey(sid);
   return new Result().ok();
  }

  @RequestMapping(value = "/update", method = RequestMethod.POST)
  @ResponseBody
  public Result update(@RequestBody TCard card) {
   int num = cardService.updateByPrimaryKeySelective(card);
   if (num > 0) {
    return new Result().ok();
   }
   return new Result().error("更新失败");
  }

  @RequestMapping(value = "/recharge", method = RequestMethod.POST)
  @ResponseBody
  public Result recharge(@RequestBody TCard card) {
   cardService.recharge(card);
   return new Result().error("充值成功");
  }

  @RequestMapping(value = "/insert", method = RequestMethod.POST)
  @ResponseBody
  public Result insert(@RequestBody TCard card) {
   int num = cardService.insertSelective(card);
   if (num > 0) {
    return new Result().ok();
   }
   return new Result().error("新增失败");
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 用Vue写一个分页器的示例代码

    之前一直想要自己试着实现一个分页器,但是一直拖,今天写完,大概照着网易云音乐的样子来完成.这个小例子很简单,通过这个小例子,可以学习到Vue计算属性的使用,并了解到写分页器需要区分的情况.这篇文章会慢慢从头来实现这个小例子,相信你一定会学会,而且看完了我的思路之后说不定会有更棒的思路和想法! 实现的效果是这样子的: 一.先简单布局 <template> <div class="pageContainer"> <ul class="pagesInn

  • vuejs2.0实现一个简单的分页示例

    最近几个项目用上vue了项目又刚好需要一个分页的功能.于是百度发现几篇文章介绍的实在方式有点复杂,没耐心看自己动手写了一个. 用js实现的分页结果如图所示: css .page-bar{ margin:40px; } ul,li{ margin: 0px; padding: 0px; } li{ list-style: none } .page-bar li:first-child>a { margin-left: 0px } .page-bar a{ border: 1px solid #dd

  • Vue2.5 结合 Element UI 之 Table 和 Pagination 组件实现分页功能

    2017年底了,总结了这一年多来的前端之路,Vue从入门到放弃,再二进宫,从 Vue1.0 持续跟踪到 Vue2.5.结合公司的一些实际项目,也封装了一些比较实用的组件. 由于现在公司管理平台主要运用Element UI,索性就结合组件Table 和 Pagination 封装了一个支持页面切换的Table组件,不啰嗦,直接上代码. 2.实现思路 2.1.Element UI 引入(整体引入) main.js // Element UI import Element from 'element-

  • 基于Vue.js的表格分页组件

    一.Vue.js简介 1.Vue的主要特点: (1) 简洁 (2) 轻量 (3)快速 (4) 数据驱动 (5) 模块友好 (6) 组件化 (1) 简洁 下面看一段Angular的实现双向绑定的代码 // html <body ng-app="myApp"> <div ng-controller="myCtrl"> <p>{{ note }}</p> <input type="text" ng-

  • Vue.js实现分页查询功能

    本文实例为大家分享了Vue.js实现分页查询的具体代码,供大家参考,具体内容如下 vue.js的使用如下: 1.引入vue.js <script src="~/js/vue2.2.4.js"></script> a.分页条 <ul class="pagination" id="pagination1"></ul> b.分页条js.css <link href="~/css/page.

  • 利用vue + element实现表格分页和前端搜索的方法

    前言 ElementUI是饿了么前端开源的一个基于Vue的前端框架,已经帮我们封装好了一系列功能性的组件,比如栅格系统.表格.表单.树形菜单.通知等.对于搞后台管理界面的项目,特别是不需要考虑兼容ie8.ie9以下的项目.ElementUI是一个不错的选择. 而且ElementUI的文档写得十分详尽,参照demo可以很快上手. 本文主要介绍了关于vue + element实现表格分页和前端搜索的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧. 实现思路 1.前端后台管理

  • Vue form 表单提交+ajax异步请求+分页效果

    废话不多说了,直接给大家贴代码了,具体代码如下所示: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1"> <meta charset="UTF-

  • Vue.js实现无限加载与分页功能开发

    本篇文章是一篇Vue.js的教程,目标在于用一种常见的业务场景--分页/无限加载,帮助读者更好的理解Vue.js中的一些设计思想.与许多Todo List类的入门教程相比,更全面的展示使用Vue.js完成一个需求的思考过程:与一些构建大型应用的高阶教程相比,又更专注于一些零碎细节的实现,方便读者快速掌握.致用. 需求分析 当一个页面中信息量过大时(例如一个新闻列表中有200条新闻需要展示),就会产生问题,例如: >数据量过大,影响加载速度 >用户体验差,很难定位到之前自己看过的某篇文章 >

  • Vue.js实现多条件筛选、搜索、排序及分页的表格功能

    与上篇实践教程一样,在这篇文章中,我将继续从一种常见的功能--表格入手,展示Vue.js中的一些优雅特性.同时也将对filter功能与computed属性进行对比,说明各自的适用场景,也为vue2.0版本中即将删除的部分filter功能做准备. 需求分析 还是先从需求入手,想想实现这样一个功能需要注意什么.大致流程如何.有哪些应用场景. 表格本身是一种非常常用的组件,用于展示一些复杂的数据时表现很好. 当数据比较多时,我们需要提供一些筛选条件,让用户更快列出他们关注的数据. 除了预设的一些筛选条

  • Vue+element-ui 实现表格的分页功能示例

    本文介绍了Vue+element-ui 实现表格的分页功能示例,分享给大家,具体如下: 实现效果如下图所示: template部分: <el-table :data="tempList" :header-cell-style="rowClass" stripe border style="margin-bottom:14px;" :empty-text="emptyText"> <el-table-colum

随机推荐