• 整合vue elementui springboot mybatisplus前后端分离的 简单增加功能 删改查未实现


    涉及知识点

    1.springboot项目启动创建  配置yml文件

    2.mybatisplus的使用

    3.vue的vite文件配置

    4.vue springboot 前后端数据交互

    后端

    1.建立项目的配置文件

    src/main/resources/application.yml

    1. spring:
    2. datasource:
    3. type: com.alibaba.druid.pool.DruidDataSource
    4. druid:
    5. username: root
    6. password:
    7. url: jdbc:mysql:/dbwx
    8. driver-class-name: com.mysql.cj.jdbc.Driver
    2.建立项目

    pom.xml

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0modelVersion>
    6. <parent>
    7. <groupId>org.springframework.bootgroupId>
    8. <artifactId>spring-boot-starter-parentartifactId>
    9. <version>2.7.15version>
    10. <relativePath/>
    11. parent>
    12. <groupId>org.examplegroupId>
    13. <artifactId>ch10vuecrudapiartifactId>
    14. <version>1.0version>
    15. <properties>
    16. <java.version>17java.version>
    17. <maven.compiler.source>17maven.compiler.source>
    18. <maven.compiler.target>17maven.compiler.target>
    19. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    20. properties>
    21. <dependencies>
    22. <dependency>
    23. <groupId>com.baomidougroupId>
    24. <artifactId>mybatis-plus-boot-starterartifactId>
    25. <version>3.5.3.2version>
    26. <exclusions>
    27. <exclusion>
    28. <groupId>com.zaxxergroupId>
    29. <artifactId>HikariCPartifactId>
    30. exclusion>
    31. exclusions>
    32. dependency>
    33. <dependency>
    34. <groupId>mysqlgroupId>
    35. <artifactId>mysql-connector-javaartifactId>
    36. <version>8.0.33version>
    37. dependency>
    38. <dependency>
    39. <groupId>com.alibabagroupId>
    40. <artifactId>druid-spring-boot-starterartifactId>
    41. <version>1.2.19version>
    42. dependency>
    43. <dependency>
    44. <groupId>org.yamlgroupId>
    45. <artifactId>snakeyamlartifactId>
    46. <version>2.0version>
    47. dependency>
    48. <dependency>
    49. <groupId>org.springframework.bootgroupId>
    50. <artifactId>spring-boot-starter-webartifactId>
    51. dependency>
    52. <dependency>
    53. <groupId>org.springframework.bootgroupId>
    54. <artifactId>spring-boot-devtoolsartifactId>
    55. <scope>runtimescope>
    56. <optional>trueoptional>
    57. dependency>
    58. <dependency>
    59. <groupId>org.springframework.bootgroupId>
    60. <artifactId>spring-boot-configuration-processorartifactId>
    61. <optional>trueoptional>
    62. dependency>
    63. <dependency>
    64. <groupId>org.projectlombokgroupId>
    65. <artifactId>lombokartifactId>
    66. <optional>trueoptional>
    67. dependency>
    68. <dependency>
    69. <groupId>org.springframework.bootgroupId>
    70. <artifactId>spring-boot-starter-testartifactId>
    71. <scope>testscope>
    72. dependency>
    73. dependencies>
    74. <build>
    75. <plugins>
    76. <plugin>
    77. <groupId>org.springframework.bootgroupId>
    78. <artifactId>spring-boot-maven-pluginartifactId>
    79. <configuration>
    80. <excludes>
    81. <exclude>
    82. <groupId>org.projectlombokgroupId>
    83. <artifactId>lombokartifactId>
    84. exclude>
    85. excludes>
    86. configuration>
    87. plugin>
    88. plugins>
    89. build>
    90. project>

    3.建立数据库表
    1. CREATE DATABASE `dbwx` DEFAULT CHARACTER SET utf8mb3;
    2. use dbwx;
    3. CREATE TABLE `t_user` (
    4. `id` bigint NOT NULL,
    5. `account` varchar(50) DEFAULT NULL,
    6. `passwd` varchar(32) DEFAULT NULL,
    7. `name` varchar(50) DEFAULT NULL,
    8. `birthday` date DEFAULT NULL,
    9. PRIMARY KEY (`id`),
    10. UNIQUE KEY `account` (`account`)
    11. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
    4.建立实体类

    cn.webrx.pojo.User

    1. package cn.webrx.pojo;
    2. import com.baomidou.mybatisplus.annotation.TableField;
    3. import com.baomidou.mybatisplus.annotation.TableId;
    4. import com.baomidou.mybatisplus.annotation.TableName;
    5. import lombok.Data;
    6. import java.time.LocalDate;
    7. @Data @TableName("t_user")
    8. public class User {
    9. @TableId
    10. private Long id;
    11. private String account;
    12. @TableField("passwd")
    13. private String password;
    14. private String name;
    15. private LocalDate birthday;
    16. }

    5.建立项目入口程序App

    cn.webrx.App

    1. package cn.webrx;
    2. import cn.webrx.mapper.DbMapper;
    3. import cn.webrx.pojo.User;
    4. import cn.webrx.mapper.UserMapper;
    5. import cn.webrx.service.UserService;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.boot.SpringApplication;
    8. import org.springframework.boot.autoconfigure.SpringBootApplication;
    9. import org.springframework.web.bind.annotation.GetMapping;
    10. import org.springframework.web.bind.annotation.RestController;
    11. import javax.annotation.Resource;
    12. import java.util.List;
    13. import java.util.Set;
    14. @SpringBootApplication
    15. @RestController
    16. public class App {
    17. @Resource
    18. DbMapper dm;
    19. @Resource
    20. UserService us;
    21. @GetMapping("/dbs")
    22. public Set dbs(){
    23. return dm.dbs();
    24. }
    25. @GetMapping("/list")
    26. public List list(){
    27. return us.list();
    28. }
    29. @GetMapping("/users")
    30. public cn.webrx.beans.User getUser() {
    31. return new cn.webrx.beans.User(199, "李四六");
    32. }
    33. public static void main(String[] args) {
    34. SpringApplication.run(App.class, args);
    35. }
    36. }
    6.建立sevices

    1. //cn.webrx.mapper
    2. @Mapper
    3. public interface UserMapper extends BaseMapper {}
    4. //cn.webrx.service
    5. public interface UserService extends IService {}
    6. //cn.webrx.service
    7. @Service
    8. public class UserServiceImpl extends ServiceImpl implements UserService {
    9. }

    前端

    axios拦截器封装

    /src/lib/http.js

    1. //http.js request.js
    2. //1 导入
    3. import axios from 'axios'
    4. //2 配置
    5. axios.defaults.baseURL = '/api'
    6. axios.defaults.timeout = 5000
    7. //3 请求拦截器 - 添加请求拦截器
    8. axios.interceptors.request.use(config => {
    9. // 在发送请求之前做些什么
    10. config.headers.token = '11111111111111111111'
    11. return config;
    12. }, function (error) {
    13. // 对请求错误做些什么
    14. return Promise.reject(error);
    15. });
    16. //4 添加响应拦截器
    17. axios.interceptors.response.use(response => {
    18. // 对响应数据做点什么
    19. return response.data;
    20. }, function (error) {
    21. // 对响应错误做点什么
    22. ElMessage({
    23. type: 'error',
    24. message: '响应失败'
    25. })
    26. return Promise.reject(error)
    27. })
    28. export default axios

    ./src/lib/user.js

    1. import axios from './http'
    2. //添加
    3. export const addUser = async function (url, d, callback) {
    4. const data = await axios.post(url,d)
    5. await callback(data)
    6. }
    7. //删除
    8. //显示 url,data
    9. //修改
    10. export const getUsers = async function (url, params, callback) {
    11. const data = await axios.get(url,params)
    12. await callback(data)
    13. }

    APP.vue

    1. <template>
    2. <h3>{{ msg.name }}h3>
    3. <el-button @click="abc">getel-button>
    4. <el-row>
    5. <el-col :span="20" :offset="2">
    6. <el-form :model="form" label-width="120px">
    7. <el-form-item label="姓名">
    8. <el-input v-model="form.name"/>
    9. el-form-item>
    10. <el-form-item label="账号">
    11. <el-input type="text" v-model="form.account"/>
    12. el-form-item>
    13. <el-form-item label="密码">
    14. <el-input type="password" v-model="form.password"/>
    15. el-form-item>
    16. <el-form-item label="出生日期">
    17. <el-col :span="11">
    18. <el-date-picker
    19. v-model="form.birthday"
    20. type="date"
    21. placeholder="请选择出生日期"
    22. style="width: 100%"/>
    23. el-col>
    24. el-form-item>
    25. <el-form-item>
    26. <el-button type="primary" @click="add">添加新用户el-button>
    27. <el-button>取消el-button>
    28. el-form-item>
    29. el-form>
    30. el-col>
    31. el-row>
    32. template>
    33. <style scoped>
    34. style>

  • 相关阅读:
    交通目标检测-行人车辆检测流量计数 - 计算机竞赛
    【CSS按钮特效】css如何实现科技感好看按钮效果(尾附源码下载)
    天线测试常见问题汇总和解答
    MySQL----事务
    MQ - 08 基础篇_消费者客户端SDK设计(下)
    与 Dave 和 Ruba 一起探讨亚马逊云科技 2023 芯片创新日
    【游戏编程扯淡精粹】工作两年总结
    Java | 学习笔记03 基础语法总结
    STM32 I2C学习
    一本没有任何数学公式的自然语言处理入门书
  • 原文地址:https://blog.csdn.net/Wdasdasda/article/details/133100097