前端VUE通过请求后端实现增删改查,文末会有前端完整代码
一、实现三个条件一起查询
//分页查询
@GetMapping("/page")
public IPage<User> findPage(@RequestParam Integer pageNum,
@RequestParam Integer pageSize,
@RequestParam(defaultValue = "") String username,
@RequestParam(defaultValue = "") String email,
@RequestParam(defaultValue = "") String address){
return userService.findPage(pageNum,pageSize,username,email,address);
}
public IPage<User> findPage( Integer pageNum,
Integer pageSize,
String username,
String email,
String address) {
IPage<User> page = new Page<>(pageNum, pageSize);
QueryWrapper<User>queryWrapper=new QueryWrapper<>();
if (! "".equals(username)){
queryWrapper.like("username",username);
}
if (! "".equals(email)){
queryWrapper.like("email",email);
}
if (! "".equals(address)){
queryWrapper.like("address",address);
}
//增加的排在前面,倒序
queryWrapper.orderByDesc("id");
return this.page(page,queryWrapper);
}
//批量删除
@PostMapping ("/del/batch")
public boolean deleteBatch(@RequestBody List<Integer>ids){
return userService.removeBatchByIds(ids);
}
package com.xqh.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xqh.entity.User;
import com.xqh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@RestController //返回json
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
//插入和修改操作
@PostMapping
public boolean save(@RequestBody User user){
return userService.saveOrUpdate(user);
}
//查询所有数据
@GetMapping
public List<User> findAll(){
return userService.list();
}
//删除
@DeleteMapping("/{id}")
public boolean delete(@PathVariable Integer id){
return userService.removeById(id);
}
前端只需要请求响应的接口即可
npm install axios --save
import axios from 'axios'
const request = axios.create({
baseURL: 'http://localhost:8081', //这是请求后端的前面那串网址
timeout: 5000
})
// request 拦截器
// 可以自请求发送前对请求做一些处理
// 比如统一加token,对请求参数统一加密
request.interceptors.request.use(config => {
config.headers['Content-Type'] = 'application/json;charset=utf-8';
// config.headers['token'] = user.token; // 设置请求头
return config
}, error => {
return Promise.reject(error)
});
// response 拦截器
// 可以在接口响应后统一处理结果
request.interceptors.response.use(
response => {
let res = response.data;
// 如果是返回的文件
if (response.config.responseType === 'blob') {
return res
}
// 兼容服务端返回的字符串数据
if (typeof res === 'string') {
res = res ? JSON.parse(res) : res
}
return res;
},
error => {
console.log('err' + error) // for debug
return Promise.reject(error)
}
)
export default request
import request from "@/utils/request"
load(){
request.get("/user/page",{
params:{
pageNum:this.pageNum,
pageSize:this.pageSize,
username:this.username,
email:this.email,
address:this.address
}
}).then(res=>{
console.log(res)
this.tableData=res.records
this.total=res.total
})
}
Home.vue
Truthfully
个人信息
退出
首页
用户管理
搜索
重置
新增
批量删除
导入
导出
编辑
删除