0


SpringBoot + Vue 项目创建详细步骤

一、本地开发环境

  1. 后端开发软件:IntelliJ IDEA 2022.1.3
  2. 后端框架:SpringBoot
  3. 前端框架:@vue/cli 5.0.8 + Element UI
  4. 后端语言:Java
  5. jdk版本:1.8.0_371
  6. 数据库:Oracle 19c
  7. 数据库操作:PLSQL Developer 15 (64 bit)
  8. 接口测试工具:Postman

二、后端创建

1、New Project 开始创建新项目

2、配置项目的名称、存储位置、语言、jdk等信息

3、选择对应的依赖,少选多选都没事,创建完了可以在配置文件里面加,选完了就点击 create

4、这是加载完成的情况

6、在pom文件里改一下 java 的版本,为了每个模块的版本不发生冲突,统一改成了适配 jdk1.8 的情况 (有的版本冲突是启动不了的)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.7.12</version> <!-- 降级到适用于 JDK 1.8 的版本 -->
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.rabbitsystem</groupId>
  12. <artifactId>carrot</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>rabbit-system</name>
  15. <description>rabbit-system</description>
  16. <url/>
  17. <licenses>
  18. <license/>
  19. </licenses>
  20. <developers>
  21. <developer/>
  22. </developers>
  23. <scm>
  24. <connection/>
  25. <developerConnection/>
  26. <tag/>
  27. <url/>
  28. </scm>
  29. <properties>
  30. <java.version>1.8</java.version> <!-- 保持 JDK 1.8 -->
  31. </properties>
  32. <dependencies>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-jdbc</artifactId>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-starter-web</artifactId>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.mybatis.spring.boot</groupId>
  43. <artifactId>mybatis-spring-boot-starter</artifactId>
  44. <version>2.2.0</version> <!-- 使用与 Spring Boot 2.7.x 兼容的 MyBatis 版本 -->
  45. </dependency>
  46. <dependency>
  47. <groupId>com.oracle.database.jdbc</groupId>
  48. <artifactId>ojdbc8</artifactId> <!-- 使用适合 JDK 1.8 的 ojdbc8 -->
  49. <scope>runtime</scope>
  50. </dependency>
  51. <dependency>
  52. <groupId>org.projectlombok</groupId>
  53. <artifactId>lombok</artifactId>
  54. <optional>true</optional>
  55. </dependency>
  56. <dependency>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-starter-test</artifactId>
  59. <scope>test</scope>
  60. </dependency>
  61. <dependency>
  62. <groupId>org.mybatis.spring.boot</groupId>
  63. <artifactId>mybatis-spring-boot-starter-test</artifactId>
  64. <version>2.2.0</version> <!-- 使用与 Spring Boot 2.7.x 兼容的 MyBatis 版本 -->
  65. <scope>test</scope>
  66. </dependency>
  67. <dependency>
  68. <groupId>org.apache.commons</groupId>
  69. <artifactId>commons-lang3</artifactId>
  70. <version>3.12.0</version>
  71. </dependency>
  72. <dependency>
  73. <groupId>log4j</groupId>
  74. <artifactId>log4j</artifactId>
  75. <version>1.2.16</version>
  76. </dependency>
  77. </dependencies>
  78. <build>
  79. <plugins>
  80. <plugin>
  81. <groupId>org.springframework.boot</groupId>
  82. <artifactId>spring-boot-maven-plugin</artifactId>
  83. <version>2.7.12</version> <!-- 降级到适用于 JDK 1.8 的版本 -->
  84. <configuration>
  85. <excludes>
  86. <exclude>
  87. <groupId>org.projectlombok</groupId>
  88. <artifactId>lombok</artifactId>
  89. </exclude>
  90. </excludes>
  91. </configuration>
  92. </plugin>
  93. </plugins>
  94. </build>
  95. </project>

7、把 application.properties 文件的后缀改成 yml,并进行项目配置

  1. ## 指定web容器访问端口号
  2. rabbit:
  3. name: 兔子系统
  4. version: v-1.0
  5. ## web容器端口号
  6. server:
  7. port: 8080
  8. ## 配置数据库连接
  9. spring:
  10. datasource:
  11. driver-class-name: oracle.jdbc.OracleDriver
  12. url: jdbc:oracle:thin:@//localhost:1521/ORCL
  13. username: 用户名
  14. password: 密码
  15. ## 配置mybatis中mapper.xml文件扫描
  16. mybatis:
  17. type-aliases-package: com.rabbitSystem.carrot.pojo.sel.*
  18. mapper-locations: classpath:mapper/*.xml # mapper.xml文件映射

8、在数据库建一个用户表(这里是拿来示范的,建完表可以加一些测试数据)

  1. -- 创建用户表
  2. CREATE TABLE CUSTOMER (
  3. ID VARCHAR2(32) NOT NULL,
  4. ACCOUNT VARCHAR2(9) NOT NULL,
  5. ROLE VARCHAR2(1) DEFAULT '2' NOT NULL,
  6. HEAD_PROTRAIT_NAME VARCHAR2(200) NOT NULL,
  7. HEAD_PROTRAIT_FILE VARCHAR2(200) NOT NULL,
  8. NICK_NAME VARCHAR2(32) NOT NULL,
  9. PASSWORD VARCHAR2(64) NOT NULL,
  10. SALT VARCHAR2(32) NOT NULL,
  11. BIRTHDAY Date,
  12. AGE VARCHAR2(3),
  13. SEX VARCHAR(1),
  14. SAFETY1_QUESTION VARCHAR2(64),
  15. SAFETY2_QUESTION VARCHAR2(64),
  16. SAFETY3_QUESTION VARCHAR2(64),
  17. SAFETY1_ANSWER VARCHAR2(32),
  18. SAFETY2_ANSWER VARCHAR2(32),
  19. SAFETY3_ANSWER VARCHAR2(32),
  20. CREATE_TIME Date DEFAULT SYSDATE NOT NULL,
  21. UPDATE_TIME Date DEFAULT SYSDATE NOT NULL,
  22. IS_DELETE varchar(1) DEFAULT 'N' NOT NULL
  23. );
  24. -- 各个字段的注释
  25. COMMENT ON TABLE CUSTOMER IS '用户表';
  26. COMMENT ON COLUMN CUSTOMER.ID IS '用户表ID';
  27. COMMENT ON COLUMN CUSTOMER.ACCOUNT IS '账号';
  28. COMMENT ON COLUMN CUSTOMER.ROLE IS '角色 0 超级管理员;1 管理员;2 普通用户';
  29. COMMENT ON COLUMN CUSTOMER.HEAD_PROTRAIT_NAME IS '头像图片名字';
  30. COMMENT ON COLUMN CUSTOMER.HEAD_PROTRAIT_FILE IS '头像二进制文件';
  31. COMMENT ON COLUMN CUSTOMER.NICK_NAME IS '昵称';
  32. COMMENT ON COLUMN CUSTOMER.PASSWORD IS '密码';
  33. COMMENT ON COLUMN CUSTOMER.SALT IS '盐加密';
  34. COMMENT ON COLUMN CUSTOMER.BIRTHDAY IS '出生日期';
  35. COMMENT ON COLUMN CUSTOMER.AGE IS '年龄';
  36. COMMENT ON COLUMN CUSTOMER.SEX IS '性别 1 女;0 男;2 保密';
  37. COMMENT ON COLUMN CUSTOMER.SAFETY1_QUESTION IS '安全问题1id';
  38. COMMENT ON COLUMN CUSTOMER.SAFETY2_QUESTION IS '安全问题2id';
  39. COMMENT ON COLUMN CUSTOMER.SAFETY3_QUESTION IS '安全问题3id';
  40. COMMENT ON COLUMN CUSTOMER.SAFETY1_ANSWER IS '安全问题1答案';
  41. COMMENT ON COLUMN CUSTOMER.SAFETY2_ANSWER IS '安全问题2答案';
  42. COMMENT ON COLUMN CUSTOMER.SAFETY3_ANSWER IS '安全问题3答案';
  43. COMMENT ON COLUMN CUSTOMER.CREATE_TIME IS '创建时间';
  44. COMMENT ON COLUMN CUSTOMER.UPDATE_TIME IS '更新时间';
  45. COMMENT ON COLUMN CUSTOMER.IS_DELETE IS '是否删除 Y 已删除;N 未删除';

9、连接数据库(我这里是Oracle) 这里面的sid可以在数据库里面查 这样就可以直接在 IDEA 里面看到Oracle数据库了

10、在运行类的同级目录下创建四个包:controller(控制层)、service(业务层)、mapper(映射层/持久层)、pojo(模型层)、resources 下面的数据访问层及对应的类(工具包里面的AjaxResult工具类下面会提供)

11、快捷生成实体类,右击数据库的表

  1. 选择生成的包下就可以了

11、使用@Data注解代替get/set方法(下面的那些就可以删掉了),我这里是单独建了一个查询用的类,因为有时候联查的时候字段是不止一个表的,所以之后查用户信息就直接用这个类,缺什么字段就补什么,不会乱

13、控制层

  1. 控制层会用到一个返回ajax类型数据的工具类,我提供在下面![](https://i-blog.csdnimg.cn/direct/025473b0d79d4a35a47a8784df06dbe0.png)
  1. package com.rabbitSystem.carrot.common.core;
  2. import com.rabbitSystem.carrot.common.utils.StringUtils;
  3. import java.util.HashMap;
  4. /**
  5. * 操作消息提醒
  6. *
  7. * @author ruoyi
  8. */
  9. public class AjaxResult extends HashMap<String, Object> {
  10. private static final long serialVersionUID = 1L;
  11. /**
  12. * 状态码
  13. */
  14. public static final String CODE_TAG = "code";
  15. /**
  16. * 返回内容
  17. */
  18. public static final String MSG_TAG = "msg";
  19. /**
  20. * 数据对象
  21. */
  22. public static final String DATA_TAG = "data";
  23. /**
  24. * 数据总数量
  25. */
  26. public static final String DATA_COUNT = "count";
  27. /**
  28. * 状态类型
  29. */
  30. public enum Type {
  31. /**
  32. * 成功
  33. */
  34. SUCCESS(200),
  35. /**
  36. * 警告
  37. */
  38. WARN(301),
  39. /**
  40. * 校验失败
  41. */
  42. FAILED(-1),
  43. /**
  44. * 错误
  45. */
  46. ERROR(500);
  47. private final int value;
  48. Type(int value) {
  49. this.value = value;
  50. }
  51. public int value() {
  52. return this.value;
  53. }
  54. }
  55. /**
  56. * 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。
  57. */
  58. public AjaxResult() {
  59. }
  60. /**
  61. * 初始化一个新创建的 AjaxResult 对象
  62. *
  63. * @param type 状态类型
  64. * @param msg 返回内容
  65. */
  66. public AjaxResult(Type type, String msg) {
  67. super.put(CODE_TAG, type.value);
  68. super.put(MSG_TAG, msg);
  69. }
  70. /**
  71. * 初始化一个新创建的 AjaxResult 对象
  72. *
  73. * @param type 状态类型
  74. * @param msg 返回内容
  75. * @param data 数据对象
  76. */
  77. public AjaxResult(Type type, String msg, Object data) {
  78. super.put(CODE_TAG, type.value);
  79. super.put(MSG_TAG, msg);
  80. if (StringUtils.isNotNull(data)) {
  81. super.put(DATA_TAG, data);
  82. }
  83. }
  84. /**
  85. * 初始化一个新创建的 AjaxResult 对象
  86. * @param type 状态类型
  87. * @param msg 返回内容
  88. * @param data 数据对象
  89. * @param count 数据总数量
  90. */
  91. public AjaxResult(Type type, String msg, Object data,Integer count) {
  92. super.put(CODE_TAG, type.value);
  93. super.put(MSG_TAG, msg);
  94. if (StringUtils.isNotNull(data)) {
  95. super.put(DATA_TAG, data);
  96. }
  97. if(count!=null){
  98. super.put(DATA_COUNT,count);
  99. }
  100. }
  101. /**
  102. * 方便链式调用
  103. *
  104. * @param key 键
  105. * @param value 值
  106. * @return 数据对象
  107. */
  108. @Override
  109. public AjaxResult put(String key, Object value) {
  110. super.put(key, value);
  111. return this;
  112. }
  113. /**
  114. * 返回成功消息
  115. *
  116. * @return 成功消息
  117. */
  118. public static AjaxResult success() {
  119. return AjaxResult.success("操作成功");
  120. }
  121. /**
  122. * 返回成功数据
  123. *
  124. * @return 成功消息
  125. */
  126. public static AjaxResult success(Object data) {
  127. return AjaxResult.success("操作成功", data);
  128. }
  129. /**
  130. * 返回成功消息
  131. *
  132. * @param msg 返回内容
  133. * @return 成功消息
  134. */
  135. public static AjaxResult success(String msg) {
  136. return AjaxResult.success(msg, null);
  137. }
  138. /**
  139. * 返回成功消息
  140. *
  141. * @param msg 返回内容
  142. * @param data 数据对象
  143. * @return 成功消息
  144. */
  145. public static AjaxResult success(String msg, Object data) {
  146. return new AjaxResult(Type.SUCCESS, msg, data);
  147. }
  148. /**
  149. * 返回警告消息
  150. *
  151. * @param msg 返回内容
  152. * @return 警告消息
  153. */
  154. public static AjaxResult warn(String msg) {
  155. return AjaxResult.warn(msg, null);
  156. }
  157. /**
  158. * 返回警告消息
  159. *
  160. * @param msg 返回内容
  161. * @param data 数据对象
  162. * @return 警告消息
  163. */
  164. public static AjaxResult warn(String msg, Object data) {
  165. return new AjaxResult(Type.WARN, msg, data);
  166. }
  167. /**
  168. * 返回错误消息
  169. *
  170. * @return
  171. */
  172. public static AjaxResult error() {
  173. return AjaxResult.error("操作失败");
  174. }
  175. /**
  176. * 返回错误消息
  177. *
  178. * @param msg 返回内容
  179. * @return 警告消息
  180. */
  181. public static AjaxResult error(String msg) {
  182. return AjaxResult.error(msg, null);
  183. }
  184. /**
  185. * 返回错误消息
  186. *
  187. * @param msg 返回内容
  188. * @param data 数据对象
  189. * @return 警告消息
  190. */
  191. public static AjaxResult error(String msg, Object data) {
  192. return new AjaxResult(Type.ERROR, msg, data);
  193. }
  194. }
  1. 控制层
  1. package com.rabbitSystem.carrot.controller;
  2. import com.rabbitSystem.carrot.common.core.AjaxResult;
  3. import com.rabbitSystem.carrot.pojo.sel.CustomerSelPojo;
  4. import com.rabbitSystem.carrot.service.CustomerService;
  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 javax.annotation.Resource;
  10. import java.util.List;
  11. /**
  12. * 标识控制层
  13. * @RestController 组合注解, @Controller + @ResponseBody ,
  14. * 在类头部指定@RestController注解,就表示该控制器下所有的方法均以 json格式类型从服务端响应给客户端(前台)
  15. */
  16. @RestController
  17. /**
  18. * 处理 HTTP 请求
  19. */
  20. @RequestMapping("/CustomerController")
  21. /**
  22. * 用户的控制层
  23. */
  24. public class CustomerController {
  25. /**
  26. * 调用业务访问层对象
  27. */
  28. @Resource
  29. private CustomerService customerService;
  30. /**
  31. * GetMapping:处理 GET 请求(查询一般用 get 请求)
  32. * 查询所有用户信息的方法
  33. */
  34. @GetMapping("/selCustomers")
  35. public AjaxResult selCustomers(){
  36. List<CustomerSelPojo> customerSelPojoList = customerService.selCustomers();
  37. int i = 200;
  38. if (customerSelPojoList == null){
  39. i = 0;
  40. };
  41. String msg = i == 200 ? "查询成功!" : "查询失败!";
  42. return new AjaxResult(i == 200 ? AjaxResult.Type.SUCCESS : AjaxResult.Type.ERROR,msg, customerSelPojoList);
  43. }
  44. }

14、业务层

  1. package com.rabbitSystem.carrot.service;
  2. import com.rabbitSystem.carrot.pojo.sel.CustomerSelPojo;
  3. import java.util.List;
  4. public interface CustomerService {
  5. List<CustomerSelPojo> selCustomers();
  6. }

15、业务层实现类

  1. package com.rabbitSystem.carrot.service.impl;
  2. import com.rabbitSystem.carrot.mapper.CustomerMapper;
  3. import com.rabbitSystem.carrot.pojo.sel.CustomerSelPojo;
  4. import com.rabbitSystem.carrot.service.CustomerService;
  5. import org.springframework.stereotype.Service;
  6. import javax.annotation.Resource;
  7. import java.util.List;
  8. @Service
  9. public class CustomerImpl implements CustomerService {
  10. //创建数据访问层对象
  11. @Resource
  12. private CustomerMapper customerMapper;
  13. @Override
  14. public List<CustomerSelPojo> selCustomers() {
  15. List<CustomerSelPojo> customerSelPojo= customerMapper.selCustomers();
  16. return customerSelPojo;
  17. }
  18. }

16、映射层

  1. package com.rabbitSystem.carrot.mapper;
  2. import com.rabbitSystem.carrot.pojo.sel.CustomerSelPojo;
  3. import org.apache.ibatis.annotations.Mapper;
  4. import java.util.List;
  5. @Mapper
  6. public interface CustomerMapper {
  7. List<CustomerSelPojo> selCustomers();
  8. }

17、数据访问层

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.rabbitSystem.carrot.mapper.CustomerMapper">
  6. <resultMap id="customerResultMap" type="com.rabbitSystem.carrot.pojo.sel.CustomerSelPojo">
  7. <id property="id" column="ID"/>
  8. <result property="account" column="ACCOUNT"/>
  9. <result property="role" column="ROLE"/>
  10. <result property="headProtraitName" column="HEAD_PROTRAIT_NAME"/>
  11. <result property="headProtraitFile" column="HEAD_PROTRAIT_FILE"/>
  12. <result property="nickName" column="NICK_NAME"/>
  13. <result property="password" column="PASSWORD"/>
  14. <result property="salt" column="SALT"/>
  15. <result property="birthday" column="BIRTHDAY"/>
  16. <result property="age" column="AGE"/>
  17. <result property="sex" column="SEX"/>
  18. <result property="safety1Question" column="SAFETY1_QUESTION"/>
  19. <result property="safety2Question" column="SAFETY2_QUESTION"/>
  20. <result property="safety3Question" column="SAFETY3_QUESTION"/>
  21. <result property="safety1Answer" column="SAFETY1_ANSWER"/>
  22. <result property="safety2Answer" column="SAFETY2_ANSWER"/>
  23. <result property="safety3Answer" column="SAFETY3_ANSWER"/>
  24. <result property="createTime" column="CREATE_TIME"/>
  25. <result property="updateTime" column="UPDATE_TIME"/>
  26. <result property="isDelete" column="IS_DELETE"/>
  27. </resultMap>
  28. <select id="selCustomers" resultMap="customerResultMap">
  29. select T.ID,T.ACCOUNT,T.ROLE,T.HEAD_PROTRAIT_NAME,
  30. T.HEAD_PROTRAIT_FILE,T.NICK_NAME,T.PASSWORD,T.SALT,TO_CHAR(T.BIRTHDAY, 'YYYY-MM-DD') AS BIRTHDAY,
  31. T.AGE,T.SEX,T.SAFETY1_QUESTION,T.SAFETY2_QUESTION,T.SAFETY3_QUESTION,
  32. T.SAFETY1_ANSWER,T.SAFETY2_ANSWER,T.SAFETY3_ANSWER,
  33. T.CREATE_TIME,T.UPDATE_TIME,
  34. T.IS_DELETE from CUSTOMER T
  35. </select>
  36. </mapper>

18、后端校验

  1. 启动服务,下面就是成功了![](https://i-blog.csdnimg.cn/direct/9f44a70576424cb389a53fd6173720dd.png)
  2. 打开 Postman 接口测试工具,输入地址(控制层对应的),然后点击 send 发送请求,返回的状态码是200就表示成功了

三、前端创建

1、检测一下vue是否安装好了,直接查看vue的版本,正常出来的话就没问题

2、构建vue

3、成功后,项目的目录下就会出现刚才创建的文件夹

4、我这里是觉得在IDEA里面比较方便,大家也可以用其他软件打开,然后进入命令行模式启动

5、这是启动好的样子,有两个网址,可以复制第一个地址去浏览器打开,看到 Welcome to Your Vue.js App 就表示成功了

6、关闭的时候,按 Ctrl + C ,然后输入 Y 回车就好了,关闭之后刚才的网页就打不开了,接下来就可以写前端的代码了

7、在 Vue 组件中使用

  1. axios

  1. fetch

发送 HTTP 请求。例如:安装

  1. axios,在命令行模式下执行这段语句
  1. npm install axios
  1. 安装好之后可以在该文件目录下找到这个文件![](https://i-blog.csdnimg.cn/direct/0a143e3568724bda82ba9584a659bb92.png)

8、安装 Element Plus(基于 Vue 3 的 UI 组件库,可以加快开发速度并提高用户界面的美观性和一致性)

  1. npm install element-plus

9、安装vue插件,这样可以更好的适配vue的语法结构


10、修改配置文件vue.config.js

  1. const { defineConfig } = require('@vue/cli-service');
  2. const { DefinePlugin } = require('webpack');
  3. module.exports = defineConfig({
  4. // 配置开发服务器
  5. devServer: {
  6. port: 8082, // 设置开发服务器的端口号为 8082
  7. },
  8. // 是否转译依赖(依赖于 babel)
  9. transpileDependencies: true,
  10. // 配置公共路径,根据环境变量设置不同的路径
  11. publicPath: process.env.NODE_ENV === 'production' ? '/myapp/' : '/',
  12. // 配置 webpack
  13. configureWebpack: {
  14. plugins: [
  15. new DefinePlugin({
  16. // 定义全局常量,用于在生产环境中关闭 Vue 的生产模式水合不匹配的详细信息
  17. __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: JSON.stringify(false)
  18. }),
  19. ],
  20. },
  21. });

11、修改配置文件main.js

  1. import { createApp } from 'vue';
  2. import RabbitSystemVueApp from './RabbitSystemVueApp'; // 引入主 Vue 组件
  3. import router from './router'; // 引入路由配置
  4. import ElementPlus from 'element-plus'; // 引入 Element Plus UI 库
  5. import 'element-plus/dist/index.css'; // 引入 Element Plus 的样式文件
  6. // 创建 Vue 应用实例
  7. const app = createApp(RabbitSystemVueApp);
  8. // 使用插件
  9. app.use(router); // 注册路由插件
  10. app.use(ElementPlus); // 注册 Element Plus 插件
  11. // 挂载应用到 DOM 元素
  12. app.mount('#app'); // 将 Vue 应用挂载到 id 为 'app' 的 DOM 元素上

12、这里看一下最后的结构

13、配置路由(访问地址)

  1. // src/router/index.js
  2. import { createRouter, createWebHistory } from 'vue-router';
  3. import App from '@/App'; // 示例主页组件
  4. // import RabbitSystemVueApp from '@/RabbitSystemVueApp'; // 示例根组件
  5. import CustomerPage from '@/module/rabbit-system/views/CustomerPage';
  6. import NotFound from '@/NotFoundPage';
  7. const routes = [
  8. { path: '/', component: App },
  9. // { path: '/app', component: RabbitSystemVueApp },
  10. { path: '/CustomerPage', component: CustomerPage },
  11. { path: '/:pathMatch(.*)*', component: NotFound } // 404 路由,匹配所有未定义的路径
  12. ];
  13. const router = createRouter({
  14. history: createWebHistory(process.env.BASE_URL),
  15. routes,
  16. });
  17. export default router;

14、前端代码CustomerPage.vue

  1. <template>
  2. <!-- 用户列表表格 -->
  3. <el-table v-loading="loading" :data="customerList">
  4. <el-table-column type="index" label="序号" width="100px" align="center" />
  5. <el-table-column label="账号" align="center" prop="account"/>
  6. <el-table-column label="角色" align="center" prop="role"/>
  7. <!-- <el-table-column label="头像" align="center" prop="customerHeadProtrait"/>-->
  8. <el-table-column label="昵称" align="center" prop="nickName"/>
  9. <el-table-column label="出生日期" align="center" prop="birthday"/>
  10. <el-table-column label="年龄" align="center" prop="age"/>
  11. <el-table-column label="性别" align="center" prop="sex"/>
  12. <el-table-column label="创建时间" align="center" prop="createTime"/>
  13. <el-table-column label="更新时间" align="center" prop="updateTime"/>
  14. <el-table-column label="是否正常使用" align="center" prop="isDelete"/>
  15. </el-table>
  16. </template>
  17. <script>
  18. import { ref, onMounted } from 'vue';
  19. import { selCustomersJs } from '@/module/rabbit-system/api/customer';
  20. export default {
  21. name: 'CustomerPage',
  22. setup() {
  23. const loading = ref(true);
  24. const customerList = ref([]);
  25. // 页面加载时调用方法
  26. onMounted(() => {
  27. selCustomersVue();
  28. });
  29. // 批量处理数字英文转换
  30. const processCustomerData = (customers) => {
  31. customers.forEach(customer => {
  32. // 处理 role
  33. if (customer.role === "1") {
  34. customer.role = "超级管理员";
  35. } else if (customer.role === "2") {
  36. customer.role = "管理员";
  37. } else if (customer.role === "3") {
  38. customer.role = "普通用户";
  39. }
  40. // 处理 sex
  41. if (customer.sex === "0") {
  42. customer.sex = "女";
  43. } else if (customer.sex === "1") {
  44. customer.sex = "男";
  45. } else if (customer.sex === "2") {
  46. customer.sex = "保密";
  47. }
  48. // 处理 isDelete
  49. if (customer.isDelete === "N") {
  50. customer.isDelete = "是";
  51. } else if (customer.isDelete === "Y") {
  52. customer.isDelete = "否";
  53. }
  54. });
  55. return customers;
  56. };
  57. // 获取用户数据
  58. const selCustomersVue = async () => {
  59. loading.value = true;
  60. try {
  61. const response = await selCustomersJs();
  62. // 批量处理数据
  63. const customers = processCustomerData(response.data.data);
  64. customerList.value = customers;
  65. } catch (error) {
  66. console.error('Error fetching customers:', error);
  67. } finally {
  68. loading.value = false;
  69. }
  70. };
  71. return {
  72. loading,
  73. customerList,
  74. };
  75. },
  76. };
  77. </script>
  78. <style scoped>
  79. /* 添加样式 */
  80. </style>

15、跳转后端用的 customer.js

  1. import axios from 'axios'; // 导入 axios 库,用于发起 HTTP 请求
  2. // 创建 axios 实例
  3. const instance = axios.create({
  4. baseURL: 'http://localhost:8081', // 设置请求的基础 URL
  5. timeout: 10000, // 请求超时时间(毫秒)
  6. });
  7. // 定义一个异步函数,用于获取客户数据
  8. export async function selCustomersJs() {
  9. try {
  10. // 发起 GET 请求,获取客户数据
  11. const response = await instance.get('/CustomerController/selCustomers', {
  12. headers: {
  13. 'Content-Type': 'application/json', // 设置请求头的内容类型为 JSON
  14. 'Authorization': 'Bearer YOUR_TOKEN' // 如果需要授权,设置 Bearer Token(请替换 YOUR_TOKEN 为实际的 Token)
  15. }
  16. });
  17. // 返回响应的数据
  18. return response.data;
  19. } catch (error) {
  20. // 捕获并处理请求错误
  21. console.error('Error fetching customers:', error); // 打印错误信息到控制台
  22. throw error; // 重新抛出错误,以便调用者可以进一步处理
  23. }
  24. }

16、App.vue

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

17、RabbitSystemVueApp.vue,这里就相当于一个组件,把其他页面加载到这里面(这里有点强迫症了,为了保留原来的 vue8080 页面,这里单独建了一个)

  1. <template>
  2. <router-view >
  3. </router-view> <!-- 渲染匹配的路由组件 -->
  4. </template>
  5. <script>
  6. </script>
  7. <style scoped>
  8. </style>

18、然后这边还配置了一个404的页面,返回首页会回到 CustomerPage 页面

  1. <!-- src/components/NotFound.vue -->
  2. <template>
  3. <div class="not-found">
  4. <div class="container">
  5. <h1>404</h1>
  6. <p>哎呀!我们找不到你要的页面。</p>
  7. <router-link class="home-link" to="/CustomerPage">返回首页</router-link>
  8. </div>
  9. </div>
  10. </template>
  11. <script setup>
  12. </script>
  13. <style scoped>
  14. .not-found {
  15. display: flex;
  16. align-items: center;
  17. justify-content: center;
  18. height: 100vh;
  19. background: linear-gradient(135deg, #61E7FFFF, #d7a1b2);
  20. color: #fff;
  21. font-family: 'Arial', sans-serif;
  22. }
  23. .container {
  24. text-align: center;
  25. background: rgba(0, 0, 0, 0.6);
  26. border-radius: 10px;
  27. padding: 40px;
  28. max-width: 500px;
  29. width: 100%;
  30. }
  31. h1 {
  32. font-size: 120px;
  33. margin: 0;
  34. font-weight: bold;
  35. text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
  36. }
  37. p {
  38. font-size: 18px;
  39. margin: 20px 0;
  40. line-height: 1.5;
  41. }
  42. .home-link {
  43. display: inline-block;
  44. padding: 10px 20px;
  45. margin-top: 20px;
  46. font-size: 16px;
  47. color: #fff;
  48. background-color: #42b983;
  49. border-radius: 5px;
  50. text-decoration: none;
  51. transition: background-color 0.3s, transform 0.3s;
  52. }
  53. .home-link:hover {
  54. background-color: #359d77;
  55. transform: scale(1.05);
  56. }
  57. .home-link:focus {
  58. outline: none;
  59. }
  60. </style>
  1. 当访问地址不正确的时候,会到这个页面![](https://i-blog.csdnimg.cn/direct/bf795b0d855643588de420496813c60a.png)

19、为了解决前后端跨域的问题,这里建了一个工具类

  1. package com.rabbitSystem.carrot.common.core;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.web.servlet.config.annotation.CorsRegistry;
  4. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  5. @Configuration
  6. /**
  7. * 解决跨域问题
  8. */
  9. public class CorsConfig implements WebMvcConfigurer {
  10. @Override
  11. public void addCorsMappings(CorsRegistry registry) {
  12. registry.addMapping("/**")
  13. .allowedOrigins("*") // 允许任意域名访问,或者替换为特定的域名
  14. .allowedMethods("GET", "POST", "PUT", "DELETE") // 允许的请求方法
  15. .allowedHeaders("Content-Type", "Authorization") // 允许的请求头
  16. .maxAge(3600); // 可选的,设置预检请求的缓存时间
  17. }
  18. }

20、这样就差不多了,最后重启一下前后端,这里展示一下最后页面的效果

  1. 由于篇幅的原因,这里就展示到这个位置(如果这边代码有遗漏的也可以在评论区说一下,后面会尽快补上),后续会继续完善代码,奉献给大家(因为之前学过,虽然现在没有做开发,但是还是不想荒废了,所以还是想练练,如果有不对或者有需要修改优化的还请大佬指教,谢谢)

结束...

标签: spring boot vue.js java

本文转载自: https://blog.csdn.net/weixin_54851039/article/details/141636291
版权归原作者 当代红领巾 所有, 如有侵权,请联系我们删除。

“SpringBoot + Vue 项目创建详细步骤”的评论:

还没有评论