SpringBoot+VUE+Element UI实现分页操作(附源码)

SpringBoot+VUE+Element UI实现分页操作

引言:

         本文主要分享了分页的实现案例,在SpringBoot+VUE环境中,利用Element UI和Mybatis的分页插件PageHelper实现的前后端分离的分页功能;

1. 效果展示

本文主要分享分页功能的实现过程

在这里插入图片描述

2. 后端实现

后端使用的技术是SpringBoot,运用Mybatis的分页插件实现

  • 创建相应的SpringBoot项目

2.1 导入相应的依赖

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
    <exclusions>
        <exclusion>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </exclusion>
    </exclusions>
</dependency>

2.2 mapper接口

首先全查出所需要分页的数据

/**
 * Created by Kak 
 */
public interface ClassInfoMapper {
    public List<ClassInfo> findAllClass();
}

2.3 mapping的实现

<select id="findAllClass" resultType="com.sx.kak.po.ClassInfo">
    SELECT * FROM tb_classinfo
</select>

3.3 service层的实现

/**
 * Created by Kak
 */
public interface ClassInfoService {

    public PageInfo<ClassInfo> findByPageService(int pageCode, int pageSize);

}
/**
 * Created by Kak 
 */
@Service
public class ClassInfoServiceImpl implements ClassInfoService{
    @Autowired(required = false)
    private ClassInfoMapper classInfoMapper;

    @Override
    public PageInfo<ClassInfo> findByPageService(int pageCode, int pageSize) {
        //使用Mybatis分页插件
        PageHelper.startPage(pageCode,pageSize);
        //调用分页查询方法,其实就是查询所有数据,mybatis自动帮我们进行分页计算
        List<ClassInfo> classInfos = classInfoMapper.findAllClass();
        return new PageInfo<>(classInfos);
    }
}

3.4 Controller层的实现

/**
 * @author by kak
 */
@RestController
public class ClassInfoController {

    @Autowired(required = false)
    private ClassInfoService classInfoService;
    
    @CrossOrigin
    @RequestMapping(value = "/pagehelper/{pageCode}/{pageSize}",method = RequestMethod.GET)
    //分页
    public PageInfo<ClassInfo> findByPage(@PathVariable(value = "pageCode") int pageCode, @PathVariable(value = "pageSize") int pageSize) {
        System.out.println(pageCode+"...."+pageSize);
        PageInfo<ClassInfo> pageInfo = classInfoService.findByPageService(pageCode, pageSize);
        return pageInfo;
    }
}

在这里插入图片描述

3. 前端实现

前端使用了Element UI的分页实现,代码如下:

<template>
  <div>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.params.page"
        :page-sizes="[1, 2, 3, 4]"
        :page-size="this.params.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
      >
      </el-pagination>
  </div>
</template>

<script>
import Axios from "axios";
export default {
  data() {
    return {
      total:'',
      params: {
        page: 1,
        size: 6,
      },
    };
  },
  mounted() {
   this.pagehelper();
  },
 
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.params.size = val;
      this.pagehelper();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.params.page = val;
      this.pagehelper();
    },
    pagehelper:function(){
           this.$axios
      .get("/pagehelper/" + this.params.page + "/" + this.params.size)
      .then((res) => {
        console.log(res.data);
        this.datas = res.data.list;
        this.total = res.data.total;
      });
    }
  },
};
</script>

<style scoped>

</style>

自此分页功能实现!!!

  • 16
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
实现头像上传,可以结合Spring Boot后端框架,Vue前端框架以及Element UI组件库进行实现。 首先,在Vue前端页面中,可以使用Element UI中的Upload组件实现文件上传功能。可以在页面中定义一个Upload组件,设置action属性为上传接口的URL,设置headers属性为请求头部信息,设置on-success属性为上传成功后的回调函数。具体代码如下: ``` <template> <div> <el-upload class="avatar-uploader" action="/api/uploadAvatar" :headers="{ Authorization: 'Bearer ' + token }" :show-file-list="false" :on-success="handleSuccess"> <img v-if="imageUrl" :src="imageUrl" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> </div> </template> <script> import { getToken } from '@/utils/auth' export default { data() { return { imageUrl: '', token: getToken() } }, methods: { handleSuccess(response) { this.imageUrl = response.data.url } } } </script> ``` 其中,token是用于认证的令牌,可以通过getToken函数获取。handleSuccess函数是上传成功后的回调函数,其中response.data.url表示上传成功后的图片URL。 然后,在Spring Boot后端接口中,可以使用Spring MVC的注解@RequestParam来接收上传的文件。具体代码如下: ``` @RestController @RequestMapping("/api") public class UploadController { @PostMapping("/uploadAvatar") public JsonResult uploadAvatar(@RequestParam("file") MultipartFile file) throws IOException { // 处理上传的文件 return JsonResult.ok("url", "http://www.example.com/avatar.jpg"); } } ``` 其中,@PostMapping注解表示接收POST请求,@RequestParam("file")注解表示接收名为file的文件参数。处理上传的文件后,可以返回一个JsonResult对象,其中包含上传成功后的图片URL。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Willing卡卡

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

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

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

打赏作者

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

抵扣说明:

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

余额充值