axios的综合案例
需求:
- 用户发起请求:http://localhost:8080/vue/findAll,获取所有的user数据
- 通过Vue.js 要求在页面中展现数据,以表格的形式展现
- 为每行数据添加 修改/删除 的按钮
- 在一个新的div中编辑3个文本框 name/age/sex 通过提交按钮实现新增
- 如果用户点击修改按钮,则在全新的div中,回显数据
- 用户修改完成数据之后,通过提交按钮,实现数据的修改
- 当用户点击删除按钮时,要求实现数据的删除操作
1. 创建springboot项目,并导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
</dependencies>
2. 配置类
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/jt?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true&autoReconnect=true&allowMultiQueries=true
username: root
password: 123456
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
mapper-locations: classpath:/mappers/*.xml
type-aliases-package: com.jt.pojo
logging:
level:
com.jt.mapper: debug
3. 启动类
package com.jt;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.jt.mapper")
public class RunApp {
public static void main(String[] args) {
SpringApplication.run(RunApp.class, args);
}
}
4. 实体类 ----- User
package com.jt.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
@Data
@Accessors(chain = true)
@TableName("demo_user")
public class User implements Serializable {
@TableId(type = IdType.AUTO,value = "id")
private Integer id;
@TableField("name")
private String name;
@TableField("age")
private Integer age;
@TableField("sex")
private String sex;
}
5. 控制层
6. Mapper接口
7. 业务层
VueService package com.jt.service;
import com.jt.pojo.User;
import java.util.List;
public interface VueService {
List<User> findAll();
int insert(User user);
int deleteById(Integer id);
int updateById(User user);
}
VueServiceImpl package com.jt.service.impl;
import com.jt.mapper.VueMapper;
import com.jt.pojo.User;
import com.jt.service.VueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class VueServiceImpl implements VueService {
@Autowired
private VueMapper vueMapper;
@Override
public List<User> findAll() {
return vueMapper.selectList(null);
}
@Override
public int insert(User user) {
return vueMapper.insert(user);
}
@Override
public int deleteById(Integer id) {
return vueMapper.deleteById(id);
}
@Override
public int updateById(User user) {
return vueMapper.updateById(user);
}
}
8. 前端页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>综合案例</title>
</head>
<body>
<script src="../js/vue.js"></script>
<script src="../js/axios.js"></script>
<div id="app">
<div align="center">
<h3>用户新增</h3>
姓名:<input type="text" v-model="addUser.name" />
年龄:<input type="text" v-model="addUser.age" />
性别:<input type="text" v-model="addUser.sex" />
<button @click="addUserBtn">新增</button>
</div>
<hr />
<div align="center">
<h3>用户修改</h3>
编号:<input type="text" disabled="disabled" v-model="updateUser.id" />
姓名:<input type="text" v-model="updateUser.name" />
年龄:<input type="text" v-model="updateUser.age" />
性别:<input type="text" v-model="updateUser.sex" />
<button @click="updateUserMet()">修改</button>
</div>
<hr />
<div>
<table border="1px" width="80%" align="center">
<tr align="center"></tr>
<th colspan="5">用户列表</th>
<tr align="center">
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>操作</th>
</tr>
<tr align="center">
<th>101</th>
<th>嫦娥</th>
<th>20</th>
<th>女</th>
<th width="20%">
<button>修改</button>
<button @click="deleteBtn(user)">删除</button>
</th>
</tr>
<tr align="center" v-for="user in userList">
<td v-text="user.id"></td>
<td v-text="user.name"></td>
<td v-text="user.age"></td>
<td v-text="user.sex"></td>
<td width="20%">
<button @click="updateUserBtn(user)">修改</button>
<button @click="deleteBtn(user)">删除</button>
</td>
</tr>
</table>
</div>
</div>
<script>
axios.defaults.baseURL = "http://localhost:8080/vue"
const app = new Vue({
el:"#app",
data() {
return {
userList: [],
addUser: {
name: '',
age: '',
sex: ''
},
updateUser: {
id: '',
name: '',
age: '',
sex: ''
}
}
},
methods:{
getUserList(){
axios.get("/findAll").then(promise => {
this.userList = promise.data
})
},
addUserBtn(){
axios.post("/insert",this.addUser).then(promise => {
let msg = promise.data
alert(msg)
this.getUserList()
this.addUser = {}
})
},
deleteBtn(user){
axios.delete("/deleteById?id="+user.id).then(promise =>{
let msg = promise.data
alert(msg)
this.getUserList()
})
},
updateUserBtn(user){
this.updateUser = user
},
updateUserMet(){
axios.put("/updateUser",this.updateUser).then(promise => {
alert(promise.data)
this.getUserList()
this.updateUser = {}
})
}
},
created() {
alert("生命周期函数")
this.getUserList()
}
})
</script>
</body>
</html>
|