• 基于前后端分离的增删改查


    一.后端

    1.了解前后端

    至少两台服务器

    访问
    浏览器-->前端服务器-->后端服务器(注意端口不要一样)

    返回数据

    后端服务器-->前端服务器-->浏览器

    2.Controller

    1. package top.remained.controller;
    2. import io.swagger.annotations.Api;
    3. import lombok.extern.slf4j.Slf4j;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.web.bind.annotation.CrossOrigin;
    6. import org.springframework.web.bind.annotation.GetMapping;
    7. import org.springframework.web.bind.annotation.RequestMapping;
    8. import org.springframework.web.bind.annotation.RestController;
    9. import top.remained.bean.User;
    10. import top.remained.service.UserService;
    11. import java.util.HashMap;
    12. import java.util.List;
    13. import java.util.Map;
    14. /**
    15. * Project:before-after
    16. * Date:2022/8/18
    17. * Time:18:37
    18. * Description:TODO
    19. *
    20. * @author lmk
    21. * @version 1.0
    22. */
    23. @RestController
    24. @RequestMapping("user")
    25. @CrossOrigin(value = "http://localhost:8080",maxAge = 3600)
    26. @Slf4j
    27. @Api("user")
    28. public class UserController {
    29. @Autowired
    30. UserService userService;
    31. @GetMapping("/login")
    32. public User login(String uName, String pwd){
    33. log.info("登录"+uName);
    34. return userService.login(uName, pwd) ;
    35. }
    36. @GetMapping("/findAllUsers")
    37. public Map findAllUsers( Integer index){
    38. if (index == null) {
    39. index =0;
    40. }
    41. int count = userService.getCount();
    42. int finalPage = count%3==0? count/3:count/3+1;
    43. Map hashMap = new HashMap<>();
    44. List users = userService.findAllUsers(index, 3);
    45. hashMap.put("users",users);
    46. hashMap.put("finalPage",finalPage);
    47. log.info("进入findAllUsers");
    48. return hashMap;
    49. }
    50. @RequestMapping("/delUser")
    51. public int delUser(Integer uid){
    52. log.info("进入删除"+uid);
    53. if (uid != null) {
    54. userService.delUser(uid);
    55. return 1;
    56. }
    57. return 0;
    58. }
    59. @RequestMapping("/addUser")
    60. public int addUser(User user){
    61. log.info("添加用户"+user.getUId());
    62. return userService.addUser(user);
    63. }
    64. @RequestMapping("/updateUser")
    65. public int updateUser(User user){
    66. log.info("更改用户"+user.getUId());
    67. return userService.updateUser(user);
    68. }
    69. @RequestMapping("/findUserById")
    70. public User findUserById(Integer id){
    71. log.info("findUserById用户"+id);
    72. System.out.println( userService.findUserById(id).getUName());
    73. return userService.findUserById(id);
    74. }
    75. // 根据uName进行模糊查询
    76. @RequestMapping("/findUserByLikeName")
    77. public List findUserByLikeName(String uname){
    78. log.info("进入模糊查询"+uname);
    79. return userService.findUserByLikeName(uname);
    80. }
    81. }

    3.遇到的问题

    1.跨域问题

    value 是前端的路径

    @CrossOrigin(value = "http://localhost:8080",maxAge = 3600)

    2.为什么我第二个返回的是Map而不用List

    我要传末页

    List传回去的数据

    1. [{"pwd":"123","type":1,"uname":"admin","uid":1},
    2. {"pwd":"123456","type":2,"uname":"胡歌","uid":2},
    3. {"pwd":"123456","type":2,"uname":"张哈哈","uid":5}]

    前台获取数据

    this.users=req.data

    Map传的数据

    1. {"users":[{"pwd":"123","type":1,"uid":1,"uname":"admin"},
    2. {"pwd":"123456","type":2,"uid":2,"uname":"胡歌"},
    3. {"pwd":"123456","type":2,"uid":5,"uname":"张哈哈"}],
    4. "finalPage":8}

    前台获取数据

    this.users=req.data.users

    注:从后台给的数据 变量都是小写的 即uId=>uid

    二.前端

    1.login.vue

    1. <template>
    2. <div>
    3. <form>
    4. 用户名:<input type="text" name="uName" v-model="uName"><br>
    5. 密码:<input type="text" name="pwd" v-model="pwd"><br>
    6. <input type="button" value="登录" v-on:click="login" />
    7. form>
    8. div>
    9. template>
    10. <script>
    11. export default {
    12. name: `login`,
    13. // 用来封装数据的
    14. data(){
    15. return{
    16. uName:'',
    17. pwd:''
    18. }
    19. },
    20. methods: {
    21. login: function(){
    22. //变量为未被使用时报错
    23. //获取模型中的数据
    24. let uName = this.uName;
    25. let pwd = this.pwd;
    26. // const that=this
    27. //传给后台的数据
    28. this.$http.get("http://localhost:8081/user/login?uName="+uName+"&pwd="+pwd).then(req => {
    29. // 判断有没有返回对象
    30. if(typeof(req.data) == 'object'){
    31. this.$router.push({name:'user_list'})
    32. //,params:{sname: uName}
    33. }else {
    34. alert("用户名或密码错误");
    35. }
    36. }
    37. );
    38. }
    39. }
    40. }
    41. script>
    42. <style scoped>
    43. style>

    效果

     

    2.user_list.vue

    1. <template>
    2. <div id="body">
    3. <form>
    4. <input type="search" v-model="name">
    5. <input type="button" v-on:click="likeName" value="搜索">
    6. form>
    7. <table id="mytable" cellspacing="0">
    8. <caption> caption>
    9. <tr>
    10. <th scope="col">idth>
    11. <th scope="col">姓名th>
    12. <th scope="col">密码th>
    13. <th scope="col">类型th>
    14. <th scope="col">操作th>
    15. tr>
    16. <tr v-for="(item,index) in users" :key="index">
    17. <td class="row">{{item.uid}}td>
    18. <td class="row">{{item.uname}}td>
    19. <td class="row">{{item.pwd}}td>
    20. <td class="row">{{item.type}}td>
    21. <td class="row">
    22. <button id="del" class="btn btn-primary" v-on:click="del(item.uid)">删除button>
    23. <button v-on:click="update(item.uid)">修改button>
    24. td>
    25. tr>
    26. table>
    27. <button v-on:click="add()">添加button>
    28. <button v-if="index>0" v-on:click="firstPage(0)">首页button>
    29. <button v-if="index>0" v-on:click="upPage(index-1)">上一页button>
    30. <button v-if="index v-on:click="downPage(index+1)">下一页button>
    31. <button v-if="index v-on:click="endPage(finalPage)">末页button>
    32. <h1>{{ $route.params.sname}}h1>
    33. div>
    34. template>
    35. <script>
    36. export default {
    37. name: `user_list`,
    38. data(){
    39. return{
    40. users:[
    41. {
    42. //uId自动转换为uid
    43. uid:'',
    44. uname:'',
    45. pwd:'',
    46. type:''
    47. }
    48. ],
    49. index:0,
    50. finalPage:0,
    51. name:'输入要搜索的名字',
    52. }
    53. },
    54. mounted(){
    55. this.$http.get("http://localhost:8081/user/findAllUsers?index=0&size=2").then(req => {
    56. this.users=req.data.users
    57. //获取末页,加载时获取一次就行 第一页index=0
    58. this.finalPage=req.data.finalPage-1
    59. })
    60. },
    61. methods: {
    62. //页面操作
    63. firstPage: function (index) {
    64. this.$http.get("http://localhost:8081/user/findAllUsers?index="+index).then(req => {
    65. this.users=req.data.users
    66. this.index = index;
    67. });
    68. },
    69. upPage: function (index) {
    70. this.$http.get("http://localhost:8081/user/findAllUsers?index="+index).then(req => {
    71. this.users=req.data.users
    72. this.index = index;
    73. });
    74. },
    75. downPage: function (index) {
    76. this.$http.get("http://localhost:8081/user/findAllUsers?index="+index).then(req => {
    77. this.users=req.data.users
    78. this.index =index;
    79. });
    80. },
    81. endPage: function (index) {
    82. this.$http.get("http://localhost:8081/user/findAllUsers?index="+index).then(req => {
    83. this.users=req.data.users
    84. this.index =index;
    85. });
    86. },
    87. //删
    88. del: function (uid) {
    89. this.$http.get("http://localhost:8081/user/delUser?uid="+uid).then(req => {
    90. if (req.data==1) {
    91. alert("删除成功")
    92. }else {
    93. alert("删除失败")
    94. }
    95. location.reload()
    96. });
    97. },
    98. //增
    99. add: function () {
    100. this.$router.push({name:'user_add'})
    101. },
    102. //改
    103. update: function (uid) {
    104. //从后端返回的数据全部时小写的 传user对象
    105. this.$http.get("http://localhost:8081/user/findUserById?id="+uid).then(req => {
    106. this.$router.push({name:'update_user',params:{user: req.data} })
    107. });
    108. },
    109. //查
    110. likeName: function () {
    111. //从后端返回的数据全部时小写的 传user对象
    112. this.$http.get("http://localhost:8081/user/findUserByLikeName?uname="+this.name).then(req => {
    113. this.$router.push({name:'user_like_name',params:{users: req.data} })
    114. });
    115. }
    116. }
    117. }
    118. script>
    119. <style scoped>
    120. #body {
    121. text-align:center;
    122. font: normal 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
    123. color: #4f6b72;
    124. background: #E6EAE9;
    125. }
    126. a {
    127. color: #c75f3e;
    128. }
    129. #mytable {
    130. width: 700px;
    131. padding: 0;
    132. margin: 0;
    133. }
    134. caption {
    135. padding: 0 0 5px 0;
    136. width: 700px;
    137. font: italic 20px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
    138. text-align: right;
    139. }
    140. th {
    141. font: bold 20px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
    142. color: #4f6b72;
    143. border-right: 1px solid #C1DAD7;
    144. border-bottom: 1px solid #C1DAD7;
    145. border-top: 1px solid #C1DAD7;
    146. letter-spacing: 2px;
    147. text-transform: uppercase;
    148. text-align: left;
    149. padding: 6px 6px 6px 12px;
    150. background: #CAE8EA no-repeat;
    151. }
    152. th.nobg {
    153. border-top: 0;
    154. border-left: 0;
    155. border-right: 1px solid #C1DAD7;
    156. background: none;
    157. }
    158. td {
    159. border-right: 1px solid #C1DAD7;
    160. border-bottom: 1px solid #C1DAD7;
    161. background: #fff;
    162. font-size:20px;
    163. padding: 6px 6px 6px 12px;
    164. color: #4f6b72;
    165. }
    166. td.alt {
    167. background: #F5FAFA;
    168. color: #797268;
    169. }
    170. th.spec {
    171. border-left: 1px solid #C1DAD7;
    172. border-top: 0;
    173. background: #fff no-repeat;
    174. font: bold 10px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
    175. }
    176. th.specalt {
    177. border-left: 1px solid #C1DAD7;
    178. border-top: 0;
    179. background: #f5fafa no-repeat;
    180. font: bold 10px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
    181. color: #797268;
    182. }
    183. /*---------for IE 5.x bug*/
    184. html>body td{ font-size:11px;}
    185. body,td,th {
    186. font-family: 宋体, Arial;
    187. font-size: 12px;
    188. }
    189. .color1{
    190. color: blue;
    191. }
    192. style>

    效果

     3.user_add.vue

    1. <template>
    2. <div>
    3. <form>
    4. 用户名: <input type="text" v-model="uName" ><br>
    5. 密码: <input type="text" v-model="pwd" ><br>
    6. 类型: <input type="text" v-model="type" ><br>
    7. <input type="button" value="提交" v-on:click="add()">
    8. form>
    9. div>
    10. template>
    11. <script>
    12. export default {
    13. name: `user_add`,
    14. data(){
    15. return {
    16. uName: '',
    17. pwd: '',
    18. type: '',
    19. }
    20. },
    21. methods: {
    22. add: function(){
    23. this.$http.get("http://localhost:8081/user/addUser?uName="+this.uName+"&pwd="+this.pwd+"&type="+this.type).then(req => {
    24. if (req.data===1) {
    25. alert("添加成功")
    26. }else {
    27. alert("添加失败")
    28. }
    29. this.$router.push({name:'user_list'})
    30. }
    31. );
    32. }
    33. }
    34. }
    35. script>
    36. <style scoped>
    37. style>

    4.update.vue

    1. <template>
    2. <div>
    3. <form>
    4. <input type="hidden" v-model="uId">
    5. 用户名: <input type="text" v-model="uName" ><br>
    6. 密码: <input type="text" v-model="pwd"><br>
    7. 类型: <input type="text" v-model="type"><br>
    8. <input type="button" value="保存" v-on:click="save">
    9. form>
    10. div>
    11. template>
    12. <script>
    13. export default {
    14. name: `update_user`,
    15. data(){
    16. return {
    17. uName: '',
    18. pwd: '',
    19. type:'',
    20. uId:''
    21. }
    22. },mounted(){
    23. this.uName = this.$route.params.user.uname
    24. this.pwd = this.$route.params.user.pwd
    25. this.type = this.$route.params.user.type
    26. this.uId = this.$route.params.user.uid
    27. },
    28. methods: {
    29. save: function(){
    30. this.$http.get("http://localhost:8081/user/updateUser?uName="+this.uName+"&pwd="+this.pwd+"&type="+this.type+"&uId="+this.uId).then(req => {
    31. if (req.data==1){
    32. alert("更改成功")
    33. } else {
    34. alert("更改失败")
    35. }
    36. alert("跳转")
    37. this.$router.push({name:'user_list'})
    38. }
    39. );
    40. }
    41. }
    42. }
    43. script>
    44. <style scoped>
    45. style>

    5.user_list.vue

    1. <template>
    2. <div>
    3. <table id="mytable" cellspacing="0">
    4. <caption> caption>
    5. <tr>
    6. <th scope="col">idth>
    7. <th scope="col">姓名th>
    8. <th scope="col">密码th>
    9. <th scope="col">类型th>
    10. tr>
    11. <tr v-for="(item,index) in users" :key="index">
    12. <td class="row">{{item.uid}}td>
    13. <td class="row">{{item.uname}}td>
    14. <td class="row">{{item.pwd}}td>
    15. <td class="row">{{item.type}}td>
    16. tr>
    17. table>
    18. div>
    19. template>
    20. <script>
    21. export default {
    22. name: "user_like_name",
    23. data(){
    24. return{
    25. users:[
    26. {
    27. //uId自动转换为uid
    28. uid:'',
    29. uname:'',
    30. pwd:'',
    31. type:''
    32. }
    33. ],
    34. }
    35. },
    36. mounted(){
    37. this.users=this.$route.params.users
    38. },
    39. }
    40. script>
    41. <style scoped>
    42. style>

    6.index.js

    1. import Vue from 'vue'
    2. import Router from 'vue-router'
    3. import login from "@/components/login"
    4. import index from '@/components/index'
    5. import test from "@/components/test"
    6. import user_list from "@/components/user_list"
    7. import user_add from "@/components/user_add";
    8. import update_user from "@/components/update_user";
    9. import user_like_name from "@/components/user_like_name";
    10. //导包爆红,没让他自动导入依赖,通过Vue加载Router
    11. Vue.use(Router)
    12. //创建路由管理器 一个组件 切换不同模板
    13. export default new Router({
    14. // 创建路由的数组 一个资源对应一个router对应一个对象{}
    15. routes:[{
    16. path:'/',
    17. name:'login',
    18. component:login
    19. },{
    20. path:'/index',
    21. name:'index',
    22. component:index
    23. },
    24. {
    25. // 映射路径
    26. path:'/test',
    27. name:'test',
    28. // 对应import的名字
    29. component:test
    30. }, {
    31. path:'/user_list',
    32. name:'user_list',
    33. component:user_list
    34. }, {
    35. path:'/user_add',
    36. name:'user_add',
    37. component:user_add
    38. }, {
    39. path:'/update_user',
    40. name:'update_user',
    41. component:update_user
    42. }, {
    43. path:'/user_like_name',
    44. name:'user_like_name',
    45. component:user_like_name
    46. }]
    47. })

    7.main.js

    1. import Vue from 'vue'
    2. import App from './App.vue'
    3. import router from './router'
    4. import axios from 'axios'
    5. Vue.config.productionTip = false
    6. //给axios定义一个名字$http
    7. Vue.prototype.$http=axios
    8. //全局的 可以取全部插件
    9. //全局加载
    10. //动态加载不需要重启
    11. new Vue({
    12. render: h => h(App),
    13. router
    14. }).$mount('#app')

    8.App.vue

    1. <template>
    2. <div id="app">
    3. <router-view>router-view>
    4. div>
    5. template>
    6. <script>
    7. // import HelloWorld from './components/HelloWorld.vue'
    8. export default {
    9. name: 'App',
    10. components: {
    11. // HelloWorld
    12. }
    13. }
    14. script>
    15. <style>
    16. #app {
    17. font-family: Avenir, Helvetica, Arial, sans-serif;
    18. -webkit-font-smoothing: antialiased;
    19. -moz-osx-font-smoothing: grayscale;
    20. text-align: center;
    21. color: #2c3e50;
    22. margin-top: 60px;
    23. }
    24. style>

    9.遇到的问题

    1.地址栏出现?

     form表单你****的***。type=button不是submit,这样就行了

    2.创建vue的步骤

    ,1.配置路由index.js
       (1 import test from "@/components/test"引入
    (2  

    1. // 创建路由的数组 一个资源对应一个router对应一个对象{}
    2. routes:[{
    3. path:'/',
    4. name:'login',
    5. component:login
    6. },{
    7. path:'/index',
    8. name:'index',
    9. component:index
    10. },}


    2.script写入模板语法

    1. script>
    2. export default {
    3. name: `test`,
    4. data(){
    5. return{
    6. // 里面json数据
    7. msg: 'hello',
    8. books:[{
    9. id:1,
    10. name: 'ss',
    11. author: 'zz'
    12. },{
    13. id:2,
    14. name: 'aa',
    15. author: 'bb'
    16. }]
    17. }
    18. },
    19. //初始化时加载 mounted
    20. created(){
    21. alert(123)
    22. }, methods: {
    23. save: function(){
    24. this.$http.get("http://localhost:8081/user/updateUser?uName="+this.uName).then(req => {
    25. if (req.data==1){
    26. alert("更改成功")
    27. } else {
    28. alert("更改失败")
    29. }
    30. alert("跳转")
    31. this.$router.push({name:'user_list'})
    32. }
    33. );
    34. }
    35. }
    36. }

    注意v-model和`login`

  • 相关阅读:
    SQL数据库添加新账号,只操作指定数据库
    python多线程系列—Event对象(六)
    大数据之——Hive
    NLP之搭建RNN神经网络
    如何实现前端项目的自动化测试?
    使用OkHttp和Java来下载
    [尚硅谷React笔记]——第9章 ReactRouter6
    【精讲】vue中的集成动画及配置代理
    Rest Template 使用
    SpringCloud Alibaba —— nacos集群设置
  • 原文地址:https://blog.csdn.net/qq_46058550/article/details/126441290