• Vue axios调用springboot接口获取数据库数据并显示到网页


    axios调用接口获取数据

    可以查看简述化的此文 点击 此文简述化文章
    PS**由于我自己的本次springboot项目内容很多,所以只是截取了其中关于axios调用接口获取数据的内容,还请大家了解工作原理即可**

    前端

    添加axios和vue2链接

     <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js">script>
        <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.7.10/vue.js">script>
    
    • 1
    • 2

    div

    div中使用vue语法v-for来循环列表数据item in list
    当然关于数据有很多种类嘛,如果不需要图片数据的读者可以掠过下面关于图片的内容
    PS关于图片的细节很多,可以学习springboot相关内容,也可以参考我之前的帖子 ,请点击:springboot图片的上传与显示
    不过经过了这么长时间,还是有所修改,尤其是关于存储在数据库的数值可以加入前缀http://localhost:8081/当然端口号是自定义的。
    控制器中修改upload的绝对路径以及添加前缀这样才方便显示图片
    img.transferTo(new File("D:\\大二上\\SpringBoot\\springboot01_10\\src\\main\\resources\\upload\\" + newFilename)); student.setPicUrl("http://localhost:8081/upload/"+newFilename
    这里要将upload文件夹放在resources下,并记得配置config防止被禁止访问404发生
    div代码参考

     <div class="table-responsive " id="Zjw">
                    <table class="table table-striped table-sm">
                        <thead>
                        <tr>
                            <th>学号th>
                            <th>姓名th>
                            <th>年龄th>
                            <th>照片th>
                        tr>
                        thead>
                        <tbody>
                        <tr v-for="item in all">
                            <td>{{item.cno}}td>
                            <td>{{item.name}}td>
                            <td>{{item.age}}td>
                            <td><img :src="item.picUrl" style="height: 130px;width: 130px">td>
                        tr>
    
                        tbody>
                    table>
                div>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    script

    getAll为springboot我们编写的后端接口

     new Vue({
            el: "#Zjw",
            data:{
                all: []
            },
            mounted() {
                this.getAl()
                //回调
                //Cqnu-zjw
            },
            methods: {
                getAl(){
                    //使用axios请求后台数据
                    axios.get("http://localhost:8081/getAll").//getAll后端接口
                    then(res => {
                        this.all = res.data
                    }).catch(err => {
                        console.log("获取不正常")
                    })
                }
            },
        })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    后端

    对于后端来说在这里就只需要有一个获取数据库的数据接口。yaml中自行配置端口,当然对于一个springboot项目来说配置是很多的,可以自行研究。

    controller

    getAll接口既然是获取数据接口就要加上@ResponseBody

    @ResponseBody
        @GetMapping("getAll")
        public List<Student> stu(Model model) {
            List<Student> students = stuService.getAll();
            return students;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    如果说是要进入页面的话

     @GetMapping("/student")
        public String student(Model model){
            List<Student> students=stuService.getAll();
            model.addAttribute("stu",students);
            return "students";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    结语

    那么到这里基本上运行项目后可以正常的获取数据库数据并显示到页面上了。当然这里所展示的仅仅是关于axios调用接口部分,其余的内容真的还差很多,总之完成一个项目还是任务艰难的。

  • 相关阅读:
    155. 最小栈
    Spring MVC Formatter(数据格式化)详解
    数据库表结构设计
    java-net-php-python-jspm早教中心系统查重PPT计算机毕业设计程序
    【面试经典 150 | 滑动窗口】串联所有单词的子串
    解决:使用MySQL Command Line Client时光标不显示的问题
    实例分享:低投资也可实现的企业信息化综合运维管理
    电子器件系列37:SD卡座(Push-Push和Push-Pull)
    已解决:树莓派外接硬盘 usb 或者sata 导致wifi无法链接 无线网卡无法使用问题
    疫情之下,企业管理如何做好?疫情防控管理系统助力企业战疫
  • 原文地址:https://blog.csdn.net/m0_67587248/article/details/133762821