Springboot+vue后台管理系统-数据回显+增删改查的实现

x33g5p2x  于2022-02-07 转载在 Vue.js  
字(7.7k)|赞(0)|评价(0)|浏览(781)

上一篇文章已经基本搭建好了框架并且实现了与数据库的对接,我们这篇文章来讲解怎样完成数据的增删改查

首先是上一篇文章写的新增,但是数据没有回显在前端:

我们首先需要在后端写一个查询数据库数据的方法可以将数据显现出来:

我们这里是分页查询,所以需要给他传两个值,分别是当前页和每页条数:这里的我们默认不传参就是第一页然后十条数据

  1. /**
  2. * 查询数据 模糊查询
  3. */
  4. @GetMapping
  5. public Result<?> findPage(
  6. @RequestParam(defaultValue = "1") Integer pageNum
  7. ,@RequestParam(defaultValue = "10") Integer pageSize
  8. ,@RequestParam(defaultValue = "") String search){
  9. LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();
  10. if (StrUtil.isNotBlank(search)){
  11. wrapper.like(User::getNickName,search);
  12. }
  13. Page<User> userPage = userMapper.selectPage(new Page<>(pageNum, pageSize),wrapper);
  14. return Result.success(userPage);
  15. }

然后我们在网页上可以看到这样的现象:

然后我们接下来在前端接收这些值:

  1. // 加载数据库数据进行渲染
  2. load(){
  3. request.get("/user",{
  4. params:{
  5. pageNum:this.currentPage,
  6. pageSize:this.pageSize,
  7. search:this.search
  8. }
  9. }).then(res =>{
  10. console.log(res)
  11. this.tableData = res.data.records
  12. this.total = res.data.total
  13. })
  14. },

然后我们在初始的时候加载:

接下来就是增删改查的前后端代码:

后端:

  1. package com.example.demo.controller;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.example.demo.common.Result;
  7. import com.example.demo.entity.User;
  8. import com.example.demo.mapper.UserMapper;
  9. import org.springframework.web.bind.annotation.*;
  10. import javax.annotation.Resource;
  11. /**
  12. * @RestController:返回数据
  13. * @Controller:渲染界面
  14. */
  15. @RestController
  16. @RequestMapping("/user")
  17. public class UserController {
  18. @Resource
  19. UserMapper userMapper;
  20. /**
  21. * 新增 @RequestBody:前台传过来的json转换为java对象
  22. * @return
  23. */
  24. @PostMapping
  25. public Result<?> save(@RequestBody User user){
  26. if (user.getPassword() == null){
  27. user.setPassword("123456");
  28. }
  29. userMapper.insert(user);
  30. return Result.success();
  31. }
  32. /**
  33. * 查询数据 模糊查询
  34. */
  35. @GetMapping
  36. public Result<?> findPage(
  37. @RequestParam(defaultValue = "1") Integer pageNum
  38. ,@RequestParam(defaultValue = "10") Integer pageSize
  39. ,@RequestParam(defaultValue = "") String search){
  40. LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();
  41. if (StrUtil.isNotBlank(search)){
  42. wrapper.like(User::getNickName,search);
  43. }
  44. Page<User> userPage = userMapper.selectPage(new Page<>(pageNum, pageSize),wrapper);
  45. return Result.success(userPage);
  46. }
  47. /**
  48. * 更新操作
  49. */
  50. @PutMapping
  51. public Result<?> update(@RequestBody User user){
  52. userMapper.updateById(user);
  53. return Result.success();
  54. }
  55. /**
  56. * 删除操作
  57. */
  58. @DeleteMapping("/{id}")
  59. public Result<?> delete(@PathVariable Long id){
  60. userMapper.deleteById(id);
  61. return Result.success();
  62. }
  63. }

前端:

  1. <template>
  2. <div style="padding: 10px" >
  3. <!-- 功能区域-->
  4. <div style="margin: 10px 0px">
  5. <el-button type="primary" @click="add">新增</el-button>
  6. <el-button type="primary">导入</el-button>
  7. <el-button type="primary">导出</el-button>
  8. </div>
  9. <!-- 搜索区域-->
  10. <div style="margin: 10px 0px">
  11. <el-input v-model="search" placeholder="请输入关键字" style="width:20%" clearable></el-input>
  12. <el-button type="primary" style="margin-left: 5px" @click="load">查询</el-button>
  13. </div>
  14. <el-table :data="tableData" border style="width: 100%">
  15. <el-table-column
  16. prop="id"
  17. label="ID" sortable="sortable" >
  18. </el-table-column>
  19. <el-table-column
  20. prop="username"
  21. label="用户名" >
  22. </el-table-column>
  23. <el-table-column
  24. prop="nickName"
  25. label="昵称">
  26. </el-table-column>
  27. <el-table-column
  28. prop="age"
  29. label="年龄">
  30. </el-table-column>
  31. <el-table-column
  32. prop="sex"
  33. label="性别">
  34. </el-table-column>
  35. <el-table-column
  36. prop="address"
  37. label="地址">
  38. </el-table-column>
  39. <el-table-column fixed="right" label="操作" >
  40. <template #default="scope">
  41. <el-button @click="handleEdit(scope.row)" type="success">编辑</el-button>
  42. <el-popconfirm title="确定删除吗?" @confirm="handleDelete(scope.row.id)">
  43. <template #reference>
  44. <el-button type="danger">删除</el-button>
  45. </template>
  46. </el-popconfirm>
  47. </template>
  48. </el-table-column>
  49. </el-table>
  50. <div style="margin: 10px 0">
  51. <el-pagination
  52. @size-change="handleSizeChange"
  53. @current-change="handleCurrentChange"
  54. :current-page="currentPage"
  55. :page-sizes="[5, 10, 20]"
  56. :page-size="pageSize"
  57. layout="total, sizes, prev, pager, next, jumper"
  58. :total="total"
  59. >
  60. </el-pagination>
  61. <el-dialog title="提示" v-model="dialogVisible" width="30%">
  62. <el-form :model="form" label-width="120px">
  63. <el-form-item label="用户名">
  64. <el-input v-model="form.username" style="width: 80%"></el-input>
  65. </el-form-item>
  66. <el-form-item label="昵称">
  67. <el-input v-model="form.nickName" style="width: 80%"></el-input>
  68. </el-form-item>
  69. <el-form-item label="年龄">
  70. <el-input v-model="form.age" style="width: 80%"></el-input>
  71. </el-form-item>
  72. <el-form-item label="性别">
  73. <el-radio v-model="form.sex" label="男"></el-radio>
  74. <el-radio v-model="form.sex" label="女"></el-radio>
  75. </el-form-item>
  76. <el-form-item label="地址">
  77. <el-input type="textarea" v-model="form.address" style="width: 80%"></el-input>
  78. </el-form-item>
  79. </el-form>
  80. <template #footer>
  81. <span class="dialog-footer">
  82. <el-button @click="dialogVisible = false">取 消</el-button>
  83. <el-button type="primary" @click="save" >确 定</el-button>
  84. </span>
  85. </template>
  86. </el-dialog>
  87. </div>
  88. </div>
  89. </template>
  90. <script>
  91. import request from "@/util/request";
  92. export default {
  93. name: 'Home',
  94. components: {
  95. },
  96. data(){
  97. return{
  98. form:{},
  99. dialogVisible:false,
  100. search:'',
  101. currentPage:1,
  102. pageSize:10,
  103. total:0,
  104. tableData:[]
  105. }
  106. },
  107. // 页面加载
  108. created(){
  109. this.load()
  110. },
  111. methods:{
  112. // 加载数据库数据进行渲染
  113. load(){
  114. request.get("/user",{
  115. params:{
  116. pageNum:this.currentPage,
  117. pageSize:this.pageSize,
  118. search:this.search
  119. }
  120. }).then(res =>{
  121. console.log(res)
  122. this.tableData = res.data.records
  123. this.total = res.data.total
  124. })
  125. },
  126. // 新增用户
  127. add(){
  128. this.dialogVisible = true
  129. // 先清空表单域
  130. this.form = {}
  131. },
  132. // 保存用户传送给后台,使用常见的api:axios
  133. save(){
  134. if (this.form.id){ //更新
  135. request.put("/user",this.form).then(res => {
  136. console.log(res)
  137. if (res.code ==='0'){
  138. this.$message({
  139. type:"success",
  140. message:"更新成功!"
  141. })
  142. }else {
  143. this.$message({
  144. type:"error",
  145. message:res.msg
  146. })
  147. }
  148. this.load() //刷新表格数据
  149. this.dialogVisible=false
  150. })
  151. }else { //新增
  152. request.post("/user",this.form).then(res => {
  153. console.log(res)
  154. if (res.code ==='0'){
  155. this.$message({
  156. type:"success",
  157. message:"新增成功!"
  158. })
  159. }else {
  160. this.$message({
  161. type:"error",
  162. message:res.msg
  163. })
  164. }
  165. this.load() //刷新表格数据
  166. this.dialogVisible=false
  167. })
  168. }
  169. },
  170. handleEdit(row){
  171. // 设置弹窗内独立对象
  172. this.form = JSON.parse(JSON.stringify(row))
  173. this.dialogVisible=true
  174. },
  175. // 删除用户
  176. handleDelete(id){
  177. console.log(id)
  178. request.delete("/user/"+id).then(res =>{
  179. if (res.code ==='0'){
  180. this.$message({
  181. type:"success",
  182. message:"删除成功!"
  183. })
  184. }else {
  185. this.$message({
  186. type:"error",
  187. message:res.msg
  188. })
  189. }
  190. this.load() //重新加载表格数据
  191. })
  192. },
  193. handleSizeChange(pageSize){ //改变当前每页的个数触发
  194. this.pageSize = pageSize
  195. this.load()
  196. },
  197. handleCurrentChange(pageNum){ //改变当前页码触发
  198. this.currentPage = pageNum
  199. this.load()
  200. }
  201. }
  202. }
  203. </script>

项目截图:

到目前为止的项目地址:

  1. 链接:https://pan.baidu.com/s/1a-DBEUDuhAQB4JfmvKO3nw
  2. 提取码:jgcj
  3. --来自百度网盘超级会员V3的分享

相关文章