Vue学习笔记十三:Vue+Bootstrap+vue-resource从接口获取数据库数据
前言
Vue学习笔记九的列表案例和Vue学习笔记十二vue-resource这两章结合一下,不在使用假数据了,这次从后台接口获取json数据。
Vue前端框架提供交互逻辑处理
Bootstrap前端css提供美化渲染
SpringBoot后端提供接口
MySQL数据库提供数据
SpringBoot提供后端接口
由于前端第九章我都写完了,等会复制着用,所以先把后端写好
先使用Spring JPA创建Entity类和自动映射数据库表,JPA参考我的文章Spring JPA学习笔记
Entity类
package com.vae.weatherforecast.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import javax.persistence.*;
@Entity
@Table(name = "person")
@AllArgsConstructor //全参数的构造函数
@NoArgsConstructor //无参数的构造函数
@Data //get和set方法
@Accessors(chain = true) //链式访问,使用链式创建的set方法有返回值
@SuppressWarnings("serial") //忽略黄色警告
public class test {
@Id
@GeneratedValue
@Column(name="id")
private Integer id;
private String name;
private String createtime;
}
JPA操作接口
package com.vae.weatherforecast.repository;
import com.vae.weatherforecast.bean.test;
import org.springframework.data.jpa.repository.JpaRepository;
public interface testRepository extends JpaRepository<test,Integer> {
}
配置文件
JPA的配置文件写在了properties里,其他的写在了yml里
server:
port: 80
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/vae?serverTimezone=UTC
username: root
password: 123
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true
数据库表自动映射,添加数据
运行SpringBoot,会自动创建表,现在来添加一些数据,如图

写提供数据的接口
新建controller,写几个操作数据的接口,我先写一个提供数据的接口,至于增删改查的增删改,下面再写。
@Autowired
testRepository testRepositorynew;
@CrossOrigin
@GetMapping("/getAllList")
public List<test> getAllList() {
List<test> lists = testRepositorynew.findAll();
for (test testnew : lists) {
System.out.println(testnew);
}
return lists;
}
跨域问题
使用Vue访问自己提供的接口的时候,会出现跨域问题的,解决办法很简单啊,SpringBoot为我们考虑了很多,直接在方法上加一个@CrossOrigin就可以了,这里注意写@GetMapping,Vue那里也使用get方式。至于jsonp方式我还不知道怎么使用。
前端修改
复制第九章的代码,修改后如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>蜀云泉</title>
<script type="text/javascript" src="../lib/vue-2.6.10.js"></script>
<script type="text/javascript" src="../lib/vue-resource.min.js"></script>
<link rel="stylesheet" href="../lib/bootstrap.min.css">
</head>
<body>
<!-- 这个div就是MVVM中的V,View -->
<div id="app">
<!-- 自定义按键码 -->
<!-- Vue.config.keyCodes.F2=113 -->
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">蜀云泉的小列表</h3>
</div>
<!-- form-inline是文字和输入框在同一行显示,form-control是设置默认宽度width为100% -->
<div class="panel-body form-inline">
<label>
id:<input type="text" class="form-control" v-model="id">
</label>
<label>
name:<input type="text" class="form-control" v-model="name" @keyup.enter="add">
</label>
<input type="button" value="添加" class="btn btn-primary" @click="add">
<label>
查询:<input type="text" class="form-control" v-model="keywords" v-focus v-color="'blue'">
</label>
</div>
</div>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>createtime</th>
<th>operation</th>
</tr>
</thead>
<tbody>
<tr v-for="item in search(keywords)" :key="item.id">
<td v-text="item.id"></td>
<td v-text="item.name"></td>
<td v-text="item.createtime"></td>
<td><a href="" @click.prevent="del(item.id)" class="btn btn-danger">删除</a></td>
</tr>
</tbody>
</table>
</div>
<script>
// 自定义全局的指令
Vue.directive('focus',{
// 第一个参数一定是el,el就是被绑定的js对象
// 当指令绑定到元素上的时候,执行bind函数,执行一次
bind:function(el){
},
// 当指令插入到DOM中的时候,执行inserted函数,执行一次
inserted:function(el){
el.focus()
},
// 当组件更新的时候,执行updated函数,可能会执行多次
updated:function(el){
}
})
// 这个vm就是MVVM中的VM,ViewModel
var vm=new Vue({
el: '#app',
// 这个data就是MVVM中的M,Model
data: {
id:'',
name:'',
keywords:'',
list:[]
},
created() {
//在Vue加载的时候执行
this.getAllList()
},
methods: {
getAllList(){
this.$http.get('http://localhost/getAllList').then(result=>{
console.log(result)
//成功了的回调函数
if(result.status===200){
this.list=result.body
}
else{
alert('获取数据失败!')
}
})
},
add(){
this.list.push({id:this.id,name:this.name,creattime:new Date().toLocaleString()})
},
del(id){
var index=this.list.findIndex(item=>{
if(item.id==id)
return true
})
this.list.splice(index,1)
},
search(keywords){
return this.list.filter(item=>{
if(item.name.includes(keywords))
return item
})
}
},
directives:{
"color":{
bind:function(el,binding){
el.style.color=binding.value
}
}
}
})
</script>
</body>
</html>
可以看到我没有使用假数据,使用了新学的vue-resource,get方式。这里遭遇的跨域请求已经在上面解释过了。
效果图
看看效果图

已经成功的从后台获取了数据,其实很简单,获取数据的接口已经完成了,那么接下来的删除,增加也很简单。
待续
待续...
我突然发现vue-resource已经不被官方推荐了....官方推荐的是axios.....
这篇文章我还是按照vue-resource来一个完整的增删改查,然后axios也来一版吧
防盗链接:本博客由蜀云泉发表
Vue学习笔记十三:Vue+Bootstrap+vue-resource从接口获取数据库数据的更多相关文章
- vue学习笔记(六) ----- vue组件
一.模块化与组件化 模块化的定义 模块化在Node中是一个规范,定义一些模块的相关的规则,从代码角度上来说,方便做区别,如果不使用模块化,写在js文件中不利于后期维护和扩展,从代码的层面上就把相关的功 ...
- vue学习笔记(四)——Vue实例以及生命周期
1.Vue实例API 1.构造器(实例化) var vm = new Vue({ //选项 |-------DOM(3) | |-------el (提供一个在页面上已存在的 DOM 元素作为 V ...
- Vue学习笔记一:初识Vue
目录 什么是Vue? 为什么要学习前端框架? MVC,MVP 和 MVVM 最简单的入门小案例 下载Vue.js 新建文件结构 写一个html 运行 可笑的小报错 Vue和MVVM 什么是Vue? V ...
- Vue学习笔记【25】——Vue组件(组件间传值)
父组件向子组件传值 组件实例定义方式,注意:一定要使用props属性来定义父组件传递过来的数据 <script> // 创建 Vue 实例,得到 ViewModel var ...
- Vue学习笔记【23】——Vue组件(组件的定义)
定义Vue组件 什么是组件: 组件的出现,就是为了拆分Vue实例的代码量的,能够让我们以不同的组件,来划分不同的功能模块,将来我们需要什么样的功能,就可以去调用对应的组件即可: 组件化和模块化的不同: ...
- Vue学习笔记【4】——Vue指令之v-on
Vue指令之v-on v-on指令介绍 直接使用指令v-on 使用简化指令@ 绑定事件代码:@事件名="methods中的方法名称" <!DOCTYPE html> & ...
- Vue学习笔记【31】——Vue路由(computed计算属性的使用)
computed计算属性的使用 默认只有getter的计算属性: <div id="app"> <input type="text" ...
- Vue学习笔记【30】——Vue路由(watch属性的使用)
考虑一个问题:想要实现 名 和 姓 两个文本框的内容改变,则全名的文本框中的值也跟着改变:(用以前的知识如何实现???) 监听data中属性的改变: <div id="app&quo ...
- Vue学习笔记【29】——Vue路由(命名视图实现经典布局)
命名视图实现经典布局 标签代码结构: <div id="app"> <router-view></router-view> < ...
随机推荐
- Backpack V
Description Given n items with size nums[i] which an integer array and all positive numbers. An inte ...
- 浏览器中点击链接,跳转qq添加好友的实现方式
做android三年了,都不知道到底干了啥,现在好好研究应该来得及,哈哈哈,希望看到文章的人共勉,哈哈哈(新手写文章,大佬轻喷,呜呜呜~) 好了,这篇只是记录下,项目中遇到的坑(MMP测试),哈哈哈, ...
- Acwing P284 金字塔 题解
Analysis 一棵树的每颗子树都对应着这棵树 DFS 序的一个区间.本题的序列虽然不是 DFS 序列,但也有该性质.本题中,我们以区间长度作为阶段, F[ l , r ] 表示序列 s[ l ~ ...
- AGC 030 B - Tree Burning 结论+枚举
考试 T2,是一个脑筋急转弯. 最暴力的贪心是每次先选左,再选右,再选左..... 然而这么做在一些情况下是错的. 但是,我们发现我们的选法一定是 $LLLLRLRLRLRLR$ 或 $RRRRLRL ...
- Vim颜色配置
最近迷上了Vim 主要原因是可以装逼 不过话说它自带的配色里面也就只有一个evening能勉强满足我的审美 于是我花了大概几天的时间翻了些百度贴吧,或者自己手动改属性后面的配色来实验这个属性到底对应哪 ...
- 洛谷 P1714 切蛋糕 题解
P1714 切蛋糕 题目描述 今天是小Z的生日,同学们为他带来了一块蛋糕.这块蛋糕是一个长方体,被用不同色彩分成了N个相同的小块,每小块都有对应的幸运值. 小Z作为寿星,自然希望吃到的第一块蛋糕的幸运 ...
- 【概率论】5-10:二维正态分布(The Bivariate Normal Distributions)
title: [概率论]5-10:二维正态分布(The Bivariate Normal Distributions) categories: - Mathematic - Probability k ...
- SDOI2015做题记录
由于我懒,并且这里面除了D2T3恶心以外都不难写,所以很多代码都没写-- 排序 对于某一个合法的操作序列(操作序列定义为每次交换的两组数),可以随意交换顺序,仍然合法.所以对于一个操作集合,答案就加\ ...
- php 数组元素加法
<?php//添加一个元素 $dirs[] = '1location';//再次添加一个元素 $dirs[] = '2location';//第三次添加一个元素 $dirs[] = '3loca ...
- ERROR 1067 (42000): Invalid default value for 'time'
修改sql_mode,去掉NO_ZERO_IN_DATE,NO_ZERO_DATE这两个参数 查看 root@:: [hmda]> show variables like 'sql_mode'; ...