保姆级教程:Spring Boot+Vue前后端分离开发(第四天)

第四天任务概要

在前三天的学习中,我们选择了一种布局,并完成了路由的设置。在本节中,将实现数据库数据的展示。

第一步:表格组件的选择

数据展示需要Element UI的Table组件,因此打开Element UI官网https://element.eleme.cn/#/zh-CN/component/table,复制该组件相应的代码:
在这里插入图片描述
然后将刚才复制的代码粘贴到Page1.vue中,Page1.vue完整代码如下:

在这里插入图片描述
Page1.vue:

<template>
	<div>
	    <el-table
	            :data="tableData"
	            border
	            style="width: 100%">
	        <el-table-column
	                fixed
	                prop="date" #对应tableData里的Date
	                label="日期"
	                width="150">
	        </el-table-column>
	        <el-table-column
	                prop="name" #对应tableData里的name,下面依次类推
	                label="姓名"
	                width="120">
	        </el-table-column>
	        <el-table-column
	                prop="province"
	                label="省份"
	                width="120">
	        </el-table-column>
	        <el-table-column
	                prop="city"
	                label="市区"
	                width="120">
	        </el-table-column>
	        <el-table-column
	                prop="address"
	                label="地址"
	                width="300">
	        </el-table-column>
	        <el-table-column
	                prop="zip"
	                label="邮编"
	                width="120">
	        </el-table-column>
	        <el-table-column
	                fixed="right"
	                label="操作"
	                width="100">
	            <template slot-scope="scope">
	                <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
	                <el-button type="text" size="small">编辑</el-button>
	            </template>
	        </el-table-column>
	    </el-table>
    </div>
</template>

<script>
    export default {
        name: "Page1",
        methods: {
            handleClick(row) {
                console.log(row);
            }
        },

        data() {
            return {
                tableData: [{
                    date: '2016-05-02',
                    name: '王小虎',
                    province: '上海',
                    city: '普陀区',
                    address: '上海市普陀区金沙江路 1518 弄',
                    zip: 200333
                }, {
                    date: '2016-05-04',
                    name: '王小虎',
                    province: '上海',
                    city: '普陀区',
                    address: '上海市普陀区金沙江路 1517 弄',
                    zip: 200333
                }, {
                    date: '2016-05-01',
                    name: '王小虎',
                    province: '上海',
                    city: '普陀区',
                    address: '上海市普陀区金沙江路 1519 弄',
                    zip: 200333
                }, {
                    date: '2016-05-03',
                    name: '王小虎',
                    province: '上海',
                    city: '普陀区',
                    address: '上海市普陀区金沙江路 1516 弄',
                    zip: 200333
                }]
            }
        }
    }
</script>

<style scoped>

</style>

第二步:添加分页插件

由于后台数据库中的数据量庞大,因此需要加载分页插件。打开Element UI官网https://element.eleme.cn/#/zh-CN/component/table,选择一个分页插件,并复制该分页组件相应的代码:
在这里插入图片描述
里面涉及到了div与script,因此将其复制到相应的标签中,此时page1.vue代码如下:

<template>
    <div>
        <el-table
                :data="tableData"
                border
                style="width: 100%">
            <el-table-column
                    fixed
                    prop="date" #对应tableData里的Date
                    label="日期"
                    width="150">
            </el-table-column>
            <el-table-column
                    prop="name" #对应tableData里的name,下面依次类推
                    label="姓名"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="province"
                    label="省份"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="city"
                    label="市区"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="address"
                    label="地址"
                    width="300">
            </el-table-column>
            <el-table-column
                    prop="zip"
                    label="邮编"
                    width="120">
            </el-table-column>
            <el-table-column
                    fixed="right"
                    label="操作"
                    width="100">
                <template slot-scope="scope">
                    <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
                    <el-button type="text" size="small">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>
        
        <el-switch v-model="value">
        </el-switch>
        <el-pagination
                :hide-on-single-page="value"
                :total="1000"
                layout="prev, pager, next"
                page-size="10" 
                @current-change="page"><!--page-size="10":每页有10条记录   @current-change:绑定页面切换的事件-->
        </el-pagination>
    </div>
</template>

<script>
    export default {
        name: "Page1",
        methods: {
            handleClick(row) {
                console.log(row);
            },
            page(currentPage){

            }
        },

        data() {
            return {
                value: false,
                tableData: [{
                    date: '2016-05-02',
                    name: '王小虎',
                    province: '上海',
                    city: '普陀区',
                    address: '上海市普陀区金沙江路 1518 弄',
                    zip: 200333
                }, {
                    date: '2016-05-04',
                    name: '王小虎',
                    province: '上海',
                    city: '普陀区',
                    address: '上海市普陀区金沙江路 1517 弄',
                    zip: 200333
                }, {
                    date: '2016-05-01',
                    name: '王小虎',
                    province: '上海',
                    city: '普陀区',
                    address: '上海市普陀区金沙江路 1519 弄',
                    zip: 200333
                }, {
                    date: '2016-05-03',
                    name: '王小虎',
                    province: '上海',
                    city: '普陀区',
                    address: '上海市普陀区金沙江路 1516 弄',
                    zip: 200333
                }]
            }
        }
    }
</script>

<style scoped>

</style>

在这里插入图片描述
前端编写完毕!

第三步:编写后端代码

打开第一天编写的后端代码,为BookController添加分页插件,BookController.java完整代码如下:

import com.cas.vuespringboot.entity.Book;
import com.cas.vuespringboot.repository.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookRepository bookRepository;

    @GetMapping("/findAll/{page}/{size}")
    public Page<Book> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){//由于前端进行了分页,因此后端也需要配置分页插件。
        Pageable pageable= PageRequest.of(page-1,size);//在后端分页插件中,第一页从0开始。而前端从1开始,因此需要进行page-1。
        return bookRepository.findAll(pageable);//返回相应页面的数据
    }
}

第四步:完善前端代码,与后端代码实现对接

打开前端项目,终端中输入vue add axios,安装axios组件。
在这里插入图片描述
在Page1.vue中添加axios实现前后端交互,同时修改列的属性,完整代码如下:

<template>
    <div>
        <el-table
                :data="tableData"
                border
                style="width: 100%">
            <el-table-column
                    fixed
                    prop="id"
                    label="编号"
                    width="150">
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="姓名"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="author"
                    label="作者"
                    width="120">
            </el-table-column>

            <el-table-column
                    fixed="right"
                    label="操作"
                    width="100">
                <template slot-scope="scope">
                    <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
                    <el-button type="text" size="small">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-switch v-model="value">
        </el-switch>
        <el-pagination
                :hide-on-single-page="value"
                :total="1000"
                layout="prev, pager, next"
                page-size="10"
                @current-change="page"><!--page-size="10":每页有10条记录   @current-change:绑定页面切换的事件-->
        </el-pagination>
    </div>
</template>

<script>
    export default {
        name: "Page1",
        methods: {
            handleClick(row) {
                console.log(row);
            },
            <!--实现后面页面的跳转-->
            page(currentPage){
                const _this=this
                <!--page()当中也添加axios.get()是为了实现分页跳转-->
                axios.get("http://localhost:8081/book/findAll/"+currentPage+"/10").then(function(resp){
                    _this.tableData=resp.data.content
                    _this.total=resp.data.totalElements
                })
            }
        },
        <!--打开链接时,先自动跳转到第一页-->
        created() {
            const _this=this
            axios.get("http://localhost:8081/book/findAll/1/10").then(function(resp){
                _this.tableData=resp.data.content
                _this.total=resp.data.totalElements
            })
        },

        data() {
            return {
                value: false,
                total: null,
                tableData: []
            }
        }
    }
</script>

<style scoped>

</style>

同时启动前后端项目,发现Page1.vue与后端已对接完成,完毕!
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

YZ122552

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

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

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

打赏作者

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

抵扣说明:

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

余额充值