当前位置:   article > 正文

简单的spring boot+vue前后端分离项目(入门)_vuespringboot前后端分离项目搭建

vuespringboot前后端分离项目搭建

目录

1.搭建vue项目

前端结构如下

 index.js

HomeView.vue

login.vue

         App.vue

2.后端项目

 pom.xml文件

application.yml

UserController

user

UserMapper

UserService

UserServiceImpl

usermapper.xml

CrosConfig

数据库设计

运行结果


1.搭建vue项目

这个可以根据下面这篇文章完成http://t.csdn.cn/B7K00

搭建好vue项目之后先使用npm install axios指令安装axios,然后在用到axios的地方引入即可

npm install axios


前端结构如下

 index.js

修改router文件夹下面的index.js如下

  1. import Vue from 'vue'
  2. import VueRouter from 'vue-router'
  3. import HomeView from '../views/HomeView.vue'
  4. import login from "@/views/login";
  5. Vue.use(VueRouter)
  6. const routes = [
  7. {
  8. path: '/',
  9. name: 'login',
  10. component: login
  11. },
  12. {
  13. path: '/home',
  14. name: 'home',
  15. component: HomeView
  16. },
  17. {
  18. path: '/about',
  19. name: 'about',
  20. // route level code-splitting
  21. // this generates a separate chunk (about.[hash].js) for this route
  22. // which is lazy-loaded when the route is visited.
  23. component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  24. }
  25. ]
  26. const router = new VueRouter({
  27. mode: 'history',
  28. base: process.env.BASE_URL,
  29. routes
  30. })
  31. export default router

HomeView.vue

  1. import Vue from 'vue'
  2. import VueRouter from 'vue-router'
  3. import HomeView from '../views/HomeView.vue'
  4. import login from "@/views/login";
  5. Vue.use(VueRouter)
  6. const routes = [
  7. {
  8. path: '/',
  9. name: 'login',
  10. component: login
  11. },
  12. {
  13. path: '/home',
  14. name: 'home',
  15. component: HomeView
  16. },
  17. {
  18. path: '/about',
  19. name: 'about',
  20. // route level code-splitting
  21. // this generates a separate chunk (about.[hash].js) for this route
  22. // which is lazy-loaded when the route is visited.
  23. component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  24. }
  25. ]
  26. const router = new VueRouter({
  27. mode: 'history',
  28. base: process.env.BASE_URL,
  29. routes
  30. })
  31. export default router

login.vue

  1. <template>
  2. <div class="login">
  3. <div class="mylogin" align="center">
  4. <el-button onclick="location.href='./public/bigdata/index.html'">跳转</el-button>
  5. <h4>登录</h4>
  6. <el-form
  7. :model="loginForm"
  8. :rules="loginRules"
  9. ref="loginForm"
  10. label-width="0px"
  11. >
  12. <el-form-item label="" prop="account" style="margin-top: 10px">
  13. <el-row>
  14. <el-col :span="2">
  15. <span class="el-icon-s-custom"></span>
  16. </el-col>
  17. <el-col :span="22">
  18. <el-input
  19. class="inps"
  20. placeholder="账号"
  21. v-model="loginForm.account"
  22. >
  23. </el-input>
  24. </el-col>
  25. </el-row>
  26. </el-form-item>
  27. <el-form-item label="" prop="passWord">
  28. <el-row>
  29. <el-col :span="2">
  30. <span class="el-icon-lock"></span>
  31. </el-col>
  32. <el-col :span="22">
  33. <el-input
  34. class="inps"
  35. type="password"
  36. placeholder="密码"
  37. v-model="loginForm.passWord"
  38. ></el-input>
  39. </el-col>
  40. </el-row>
  41. </el-form-item>
  42. <el-form-item style="margin-top: 55px">
  43. <el-button type="primary" round class="submitBtn" @click="submitForm"
  44. >登录
  45. </el-button>
  46. </el-form-item>
  47. <div class="unlogin">
  48. <router-link :to="{ path: '/forgetpwd' }"> 忘记密码? </router-link>
  49. |
  50. <router-link :to="{ path: '/register' }">
  51. <a href="register.vue" target="_blank" align="right">注册新账号</a>
  52. </router-link>
  53. </div>
  54. </el-form>
  55. </div>
  56. </div>
  57. </template>
  58. <script>
  59. import { mapMutations } from "vuex";
  60. export default {
  61. name: "Login",
  62. data: function () {
  63. return {
  64. loginForm: {
  65. account: "",
  66. passWord: "",
  67. },
  68. loginRules: {
  69. account: [{ required: true, message: "请输入账号", trigger: "blur" }],
  70. passWord: [{ required: true, message: "请输入密码", trigger: "blur" }],
  71. },
  72. };
  73. },
  74. methods: {
  75. ...mapMutations(["changeLogin"]),
  76. submitForm() {
  77. const userAccount = this.loginForm.account;
  78. const userPassword = this.loginForm.passWord;
  79. if(userAccount==='admin'&&userPassword==='123456'){
  80. this.$router.push('/home');
  81. }
  82. else{
  83. alert('账号或密码错误')
  84. }
  85. // if (!userAccount) {
  86. // return this.$message({
  87. // type: "error",
  88. // message: "账号不能为空!",
  89. // });
  90. // }
  91. // if (!userPassword) {
  92. // return this.$message({
  93. // type: "error",
  94. // message: "密码不能为空!",
  95. // });
  96. // }
  97. console.log("用户输入的账号为:", userAccount);
  98. console.log("用户输入的密码为:", userPassword);
  99. },
  100. },
  101. };
  102. </script>
  103. <style>
  104. .login {
  105. height: 100vh;
  106. background-image: linear-gradient(to bottom right, #3F5EFB, #42b983);
  107. overflow: hidden;
  108. width: 100vw;
  109. padding: 0;
  110. margin: 0;
  111. font-size: 16px;
  112. background-position: left top;
  113. background-color: #242645;
  114. color: #fff;
  115. position: relative;
  116. }
  117. .mylogin {
  118. width: 240px;
  119. height: 280px;
  120. position: absolute;
  121. top: 0;
  122. left: 0;
  123. right: 0;
  124. bottom: 0;
  125. margin: auto;
  126. padding: 50px 40px 40px 40px;
  127. box-shadow: -15px 15px 15px rgba(6, 17, 47, 0.7);
  128. opacity: 1;
  129. background: linear-gradient(
  130. 230deg,
  131. rgba(53, 57, 74, 0) 0%,
  132. rgb(0, 0, 0) 100%
  133. );
  134. }
  135. .inps input {
  136. border: none;
  137. color: #fff;
  138. background-color: transparent;
  139. font-size: 12px;
  140. }
  141. .submitBtn {
  142. background-color: transparent;
  143. color: #39f;
  144. width: 200px;
  145. }
  146. </style>

App.vue

  1. <template>
  2. <div id="app">
  3. <router-view/>
  4. </div>
  5. </template>

在终端输入指令npm run serve运行vue项目,点击生成的链接即可

然后输入账号和密码进行登录(账号:admin密码123456),登录后进入主页面


2.后端项目

接着我们搭建后端项目,总体结构如图

首先新建一个springboot工程

 

 pom.xml文件

  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.8</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>springbootvue</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>springbootvue</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.mybatis.spring.boot</groupId>
  26. <artifactId>mybatis-spring-boot-starter</artifactId>
  27. <version>2.2.1</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>mysql</groupId>
  31. <artifactId>mysql-connector-java</artifactId>
  32. <version>8.0.28</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.projectlombok</groupId>
  36. <artifactId>lombok</artifactId>
  37. <optional>true</optional>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-starter-test</artifactId>
  42. <scope>test</scope>
  43. </dependency>
  44. <dependency>
  45. <groupId>com.baomidou</groupId>
  46. <artifactId>mybatis-plus-core</artifactId>
  47. <version>3.5.3</version>
  48. </dependency>
  49. </dependencies>
  50. <build>
  51. <resources>
  52. <resource>
  53. <directory>src/main/java</directory>
  54. <includes>
  55. <include>**/*.xml</include>
  56. </includes>
  57. </resource>
  58. </resources>
  59. <plugins>
  60. <plugin>
  61. <groupId>org.springframework.boot</groupId>
  62. <artifactId>spring-boot-maven-plugin</artifactId>
  63. <configuration>
  64. <excludes>
  65. <exclude>
  66. <groupId>org.projectlombok</groupId>
  67. <artifactId>lombok</artifactId>
  68. </exclude>
  69. </excludes>
  70. </configuration>
  71. </plugin>
  72. </plugins>
  73. </build>
  74. </project>

application.yml

application.yml文件(初始文件是application.properties,将其改为application.yml)

  1. spring:
  2. datasource:
  3. username: root
  4. password: root
  5. url: jdbc:mysql://localhost:3306/student?allowMultiQueries=true&characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false
  6. driver-class-name: com.mysql.cj.jdbc.Driver
  7. server:
  8. port:
  9. 8181
  10. mybatis:
  11. type-aliases-package: com.example.springbootvue.entity
  12. mapper-locations: classpath:mapper/*.xml

UserController

  1. package com.example.springbootvue.controller;
  2. import com.example.springbootvue.entity.user;
  3. import com.example.springbootvue.service.UserService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import java.util.List;
  9. @RestController
  10. @RequestMapping("/user")
  11. public class UserController {
  12. @Autowired
  13. UserService userService;
  14. @GetMapping("/findAll")
  15. public List<user> findAll() {
  16. return userService.queryuserList();
  17. }
  18. }

user

  1. package com.example.springbootvue.entity;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. @Data
  6. @NoArgsConstructor
  7. @AllArgsConstructor
  8. public class user {
  9. private Integer id;
  10. private String username;
  11. private String password;
  12. }

UserMapper

  1. package com.example.springbootvue.mapper;
  2. import com.example.springbootvue.entity.user;
  3. import org.apache.ibatis.annotations.Mapper;
  4. import org.springframework.stereotype.Repository;
  5. import java.util.List;
  6. @Mapper
  7. @Repository
  8. public interface UserMapper {
  9. public List<user> queryuserList();
  10. }

UserService

  1. package com.example.springbootvue.service;
  2. import com.example.springbootvue.entity.user;
  3. import java.util.List;
  4. public interface UserService {
  5. public List<user> queryuserList();
  6. }

UserServiceImpl

  1. package com.example.springbootvue.service;
  2. import com.example.springbootvue.entity.user;
  3. import com.example.springbootvue.mapper.UserMapper;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import java.util.List;
  7. @Service
  8. public class UserServiceImpl implements UserService{
  9. @Autowired
  10. UserMapper userMapper;
  11. @Override
  12. public List<user> queryuserList() {
  13. return userMapper.queryuserList();
  14. }
  15. }

usermapper.xml

  1. <?xml version="1.0" encoding="UTF8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.example.springbootvue.mapper.UserMapper">
  6. <select id="queryuserList" resultType="com.example.springbootvue.entity.user">
  7. select * from user
  8. </select>
  9. </mapper>

CrosConfig

  1. package com.example.springbootvue.config;
  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. public class CrosConfig implements WebMvcConfigurer {
  7. @Override
  8. public void addCorsMappings(CorsRegistry registry){
  9. registry.addMapping("/**")
  10. .allowedOriginPatterns("*")
  11. .allowedMethods("GET","HEAD","POST","PUT","DELETE","OPTIONS")
  12. .allowCredentials(true)
  13. .maxAge(3600)
  14. .allowedHeaders("*");
  15. }
  16. }

数据库设计

 

运行结果

运行后端项目和前端项目,发现数据库中的信息传到了前端页面,然后可以自己做样式

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/214379
推荐阅读
相关标签
  

闽ICP备14008679号