从0开始搭建一个简单的前后端分离的XX系统-vue+Springboot+mybatis-plus+mysql

news2024/10/6 8:37:49

一、准备工作

1.安装node

2.idea 旗舰版**

idea**教程
上述教程中的idea**工具

3.安装mysql任意版本

mysql

4.安装mysql workbench(没用上)

5.安装navicate

参考文章:百度-从小白到架构(作者)-Navicat16** Navicat最新版**教程「永久,亲测有效」

navicate16**工具-百度网盘下载

6.安装postman

postman下载

可能需要新建一个工作区后,点下图中圈内的加号,即可使用postman
在这里插入图片描述

7.jdk17(根据springboot的版本的需要)

jdk17-installer

二、Vue前端

强推这两篇博文作为参考:
参考博文-CSDN-Joshua_HIT(作者)-零基础Vue入门学习记录(1)
参考博文-CSDN-Joshua_HIT(作者)-零基础Vue入门学习记录(2)

(一)Vue配置:

# npm换源,加速安装过程 !!!重要重要
npm config set registry http://registry.npm.taobao.org

# 如果需要安装vue3,把下面语句中的2删掉就行
npm install vue2 -g 

npm install @vue/cli -g
npm i -g @vue/cli-init 

创建一个项目

# 注意项目名称不能有大写字母
vue init webpack preject_name
# 我这里
cd preject_name
npm install
# 前后端通信用到的库
Vue add axios 
npm run dev

(二)前后端通信

1.新建一个页面

在这里插入图片描述

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import login from '@/components/login'
Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    },
    {
      path: '/login',
      name: 'login',
      component: login
    }
  ]
})

2.login页面

<template>
  <div>
    <h1>show me something different</h1>
    <input type="text" v-model="inputText.id" placeholder="请输入学生ID">
    <br>
    <input type="text" v-model="inputText.name" placeholder="请输入学生姓名">
    <br>
    <input type="text" v-model="inputText.benValue" placeholder="请输入学生笨蛋值">
      <br>
    <button @click="sendRequest">向数据库写入学生信息</button>
    <br>
    <button @click="showData">获取数据库中学生信息</button>
    <p v-if="students.length > 0">获取到的学生数据:</p>
    <ul>
      <li v-for="student in students" >{{ student.id}} -{{ student.name }} -笨蛋值: {{ student.benValue }}</li>
    </ul>
  </div>
</template>
<script>
import axios from 'axios'
export default {
  name: 'login',
  data() {
    return {
      inputText: {
        id: '',
        name: '',
        benValue: ''
      },
      students: []
    }
  },
  methods: {
    sendRequest() {
      
      if(this.inputText.id == '' || this.inputText.name == '' || this.inputText.benValue == '' )
      {
        alert('请填写完整信息')
        return;
      }
      axios.post('http://127.0.0.1:8022/insertStudent', { id: this.inputText.id, name:this.inputText.name, benValue: parseInt(this.inputText.benValue) })
        .then(response => {
          this.inputText = ''
          console.log(response.data)
          alert('发送成功')
        })
        .catch(error => {
          console.log(error)
        })
    },
    showData() {
      axios.get('http://127.0.0.1:8022/getStudentInfo')
        .then(response => {
          this.students = response.data
          console.log(this.students)
        })
        .catch(error => {
          console.log(error)
        })
    }
  }
}
</script>
<style scoped></style>

二、后端配置

(一)环境搭建:

参考博文:CSDN-kong清空(作者)-十分钟快速入门Springboot(入门级)
基本上按照上述博文的内容进行操作,有些注意事项:

1.

经尝试sdk版本选择jdk17、springboot版本使用3.0.5/3.0.6可以。
jdk17-installer

2.

navicat建表的操作:

3.

修改端口:
在这里插入图片描述
在这里插入图片描述

(二)前后端通信:

在上述提到的参考的教程基础上:参考博文:CSDN-kong清空(作者)-十分钟快速入门Springboot(入门级)
本文的springboot侧的代码如下:(student类的设计与参考教程不一样)

1.com.demo1.demo.entity.Student.java

package com.demo1.demo.entity;

public class Student {
    private  String id;
    private String name;
    private int benValue;
    public String getId(){
        return id;
    }
    public void setId(String id){
        this.id = id;
    }
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name = name;
    }
    public int getBenValue(){
        return benValue;
    }
    public void setBenValue(int benValue){
        this.benValue = benValue;
    }
    public Student(String id, String name, int benValue){
        this.id = id;
        this.name = name;
        this.benValue = benValue;
    }
}

2.com.demo1.demo.contoller.StudentController.java

package com.demo1.demo.controller;

import com.demo1.demo.entity.Student;
import com.demo1.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping(value="/getStudentInfo", method = {RequestMethod.GET, RequestMethod.POST})
    public List<Student> getStudent(){
        return studentService.findAll();
    }

    @PostMapping("/insertStudent")
    public void insertStudent(@RequestBody Student student){
        studentService.insert(student);
    }
}

3.com/demo1/demo/service/StudentService.java

package com.demo1.demo.service;

import com.demo1.demo.entity.Student;
import com.demo1.demo.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {

    @Autowired
    private StudentMapper studentMapper;

    public List<Student>findAll(){
        return studentMapper.findAll();
    }

    public void insert(Student new_student) {studentMapper.insert(new_student);}

}

4.com/demo1/demo/mapper/StudentMapper.java

package com.demo1.demo.mapper;

import com.demo1.demo.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;

@Mapper
public interface StudentMapper {

    List<Student>findAll();
    int insert(Student student);
}

5.com/demo1/demo/config/corsConfig.java(跨域通信)

参考博文:CSDN-深情不及里子(作者)-Springboot+Vue实现简单的前端后分离数据交互
在博文的2.1.5节

package com.demo1.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author young
 * @date 2022/8/23 17:33
 * @description: 跨域配置
 */
@Configuration
public class corsConfig {
    /**
     * 最大有效时长
     */
    private static final  long MAX_TIMEOUT=24*60*60;
    @Bean
    public CorsConfiguration corsConfiguration(){
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.setMaxAge(MAX_TIMEOUT);
        //设置访问源请求头
        corsConfiguration.addAllowedHeader("*");
        //设置访问源地址
        corsConfiguration.addAllowedOrigin("*");
        //设置访问源请求方法
        corsConfiguration.addAllowedMethod("*");
        return corsConfiguration;
    }

    /**
     * 设置跨域配置
     * @return
     */
    @Bean
    public CorsFilter corsFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**",corsConfiguration());
        return new CorsFilter(source);
    }
}

6.StudentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.demo1.demo.mapper.StudentMapper">
    <select id="findAll" resultType="com.demo1.demo.entity.Student">
        SELECT * FROM student
    </select>
    <insert id="insert" parameterType="com.demo1.demo.entity.Student">
        INSERT INTO student(id, name, benValue) VALUES (#{id}, #{name},  #{benValue})
            ON DUPLICATE KEY UPDATE id = id
    </insert>
</mapper>

数据库中内容如下图所示,有个需要特别注意的地方:id、name、benValue的相对位置,与Student类中的声明顺序最好一致(非必要)
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/461689.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Thinkphp获取项目最近更改变动的所有文件

导读&#xff1a; 企业级的网站项目都是要不断优化迭代更新的&#xff0c;做为一名后端程序员&#xff0c;在编写更新模块时&#xff0c;如何能快速获取最近修改的文件&#xff0c;然后打包压缩成更新补丁呢&#xff1f;我们先来看一下最终效果图&#xff1a; 步骤&#xff1a…

使用FFMPEG分离mp4/flv文件中的264视频和aac音频

准备 ffmpeg 4.4 一个MP4或flv格式的视频文件 分离流程 大致分为以下几个简单步骤&#xff1a; 1.使用avformat_open_input 函数打开文件并初始化结构AVFormatContext 2.查找是否存在音频和视频信息 3.构建一个h264_mp4toannexb比特流的过滤器&#xff0c;用来给视频avpa…

Hudi 数据湖技术之集成Flink

目录 1 安装Flink2 快速入门2.1 集成Flink概述2.2 环境准备2.3 创建表2.4 插入数据2.5 查询数据2.6 更新数据 3 Streaming query3.1 创建表3.2 查询数据3.3 插入数据 4 Flink SQL Writer4.1 Flink SQL集成Kafka4.2 Flink SQL写入Hudi4.2.1 创建Maven Module4.2.2 消费Kafka数据…

【C++】了解设计模式、 stackqueue的使用与模拟实现

文章目录 1.设计模式2.stack1.stack的使用1.stack的结构2.stack的接口 2.stack的模拟实现1.stack的结构2.接口实现 3.queue1.queue的使用1.queue的结构3.queue的接口 2.queue的模拟实现1.queue的结构2.接口实现 4.了解deque1.deque的原理介绍2.deque的底层结构3.deque的迭代器设…

Codeforces Round 861 (Div. 2)(A~D)

A. Lucky Numbers 给出边界l和r&#xff0c;在区间[l, r]之间找到幸运值最大的数字。一个数字的幸运值被定义为数位差的最大值&#xff0c;即数字中最大的数位和最小的数位的差。 思路&#xff1a;因为涉及到至少两位&#xff0c;即个位和十位变化最快&#xff0c;最容易得到相…

Android四大组件之广播接收者BroadcastReceiver

一、全局广播 Android中的广播可以分为两种类型&#xff1a;标准广播和有序广播 标准广播&#xff1a;一种完全异步执行的广播&#xff0c;在广播发出之后&#xff0c;所有的广播接收器几乎都会同一时刻接收到这条广播消息&#xff0c;因此它们之间没有任何先后顺序。无法进行…

Vector-常用CAN工具 - 入门到精通 - 专栏链接

一、CANoe篇 1、CANoe入门到精通_软件安装 2、CANoe入门到精通_硬件及环境搭建 3、CANoe入门到精通_软件环境配置 4、CANoe入门到精通_Network Node CAPL开发 5、CANoe入门到精通_Node节点开发基本数据类型 6、CANoe入门到精通_Test Node节点开发设置 7、CANoe入门到精通…

《Cocos Creator游戏实战》AIGC之将草稿内容转为真实内容

目录 前言 训练AI 从识别结果中提取必要数据 发送图片并生成最终代码 总结与提高 资源下载 前言 当创作灵感来的时候&#xff0c;我们可能会先把灵感记录在草稿上&#xff0c;之后再去实现它。比方说有一天&#xff0c;我突然来了游戏创作灵感&#xff0c;想着那可以先把…

gpt 怎么用-免费gpt下载使用方法

gpt 怎么用 GPT&#xff08;Generative Pre-trained Transformer&#xff09;是一种基于Transformer的神经网络模型&#xff0c;用于自然语言处理任务&#xff0c;例如文本生成、摘要生成、翻译、问答等。以下是使用GPT进行文本生成的一般步骤&#xff1a; 首先&#xff0c;您…

编译预处理

编译预处理 1、宏定义1.1、 无参宏定义1.2、使用宏定义的优点1.3、宏定义注意点1.4、带参数的宏(重点)1.5、条件编译1.6、宏定义的一些巧妙用法(有用)1.7、结构体占用字节数的计算原则&#xff08;考题经常考&#xff0c;要会画图&#xff09;1.8、#在宏定义中的作用&#xff0…

转型产业互联网,新氧能否再造辉煌?

近年来&#xff0c;“颜值经济”推动医美行业快速发展&#xff0c;在利润驱动下&#xff0c;除了专注医美赛道的企业之外&#xff0c;也有不少第三方互联网平台正强势进入医美领域&#xff0c;使以新氧为代表的医美企业面对不小发展压力&#xff0c;同时也展现出强大的发展韧性…

六、CANdelaStudio入门-通信参数编辑

本专栏将由浅入深的展开诊断实际开发与测试的数据库编辑,包含大量实际开发过程中的步骤、使用技巧与少量对Autosar标准的解读。希望能对大家有所帮助,与大家共同成长,早日成为一名车载诊断、通信全栈工程师。 本文介绍CANdelaStudio的通信参数编辑,欢迎各位朋友订阅、评论,…

Kubernetes 笔记(16)— 集群管理、使用名字空间分隔系统资源、给名字空间设置资源限额、默认资源配额的使用

1. 为什么要有名字空间 首先要明白&#xff0c;Kubernetes 的名字空间并不是一个实体对象&#xff0c;只是一个逻辑上的概念。它可以把集群切分成一个个彼此独立的区域&#xff0c;然后我们把对象放到这些区域里&#xff0c;就实现了类似容器技术里 namespace 的隔离效果&…

MATLAB符号运算(七) 更新中...

目录 1、实验目的&#xff1a; 2、实验内容&#xff1a; 1、实验目的&#xff1a; 1&#xff09;掌握定义符号对象和创建符号表达式的方法&#xff1b; 2&#xff09;掌握符号运算基本命令和规则&#xff1b; 3&#xff09;掌握符号表达式的运算法则以及符号矩阵运算&#xf…

93、Dehazing-NeRF: Neural Radiance Fields from Hazy Images

简介 论文&#xff1a;https://arxiv.org/pdf/2304.11448.pdf 从模糊图像输入中恢复清晰NeRF 使用大气散射模型模拟有雾图像的物理成像过程&#xff0c;联合学习大气散射模型和干净的NeRF模型&#xff0c;用于图像去雾和新视图合成 通过将NeRF 3D场景的深度估计与大气散射模…

【牛客刷题专栏】23:JZ22 链表中倒数最后k个结点(C语言编程题)

前言 个人推荐在牛客网刷题(点击可以跳转)&#xff0c;它登陆后会保存刷题记录进度&#xff0c;重新登录时写过的题目代码不会丢失。个人刷题练习系列专栏&#xff1a;个人CSDN牛客刷题专栏。 题目来自&#xff1a;牛客/题库 / 在线编程 / 剑指offer&#xff1a; 目录 前言问…

LeetCode-344. 反转字符串

题目链接 LeetCode-344. 反转字符串 题目描述 题解 题解一&#xff08;Java&#xff09; 作者&#xff1a;仲景 直接双指针前后一直交换即可 class Solution {public void reverseString(char[] s) {if (s.length 1)return;// 双指针int lp 0, rp s.length - 1;while (lp…

【百度智能云】基于http3的xcdn 开放直播方案设计与实践

大神:柯老师 现有的融合CDN 0 需要集成sdksdk 是集成在端侧缺点 sdk 对端侧有影响多云模式下,sdk不互通、 XCDN 设计目标 :保持现有cdn的优势 承载各种业务:直播点播让各家的cdn互通cdn 厂家屏蔽了差异性,集成起来比较简单,对接简单开发的互联网生态。使用统一的http3标…

理解缓冲区

文章目录 一.缓冲区1.什么是缓冲区2.缓冲区的意义3.缓冲区的刷新策略4.我们目前谈论的缓冲区在哪里5.仿写FILE5.1myStdio.h5.2myStdio.c 6.操作系统的缓冲区 一.缓冲区 int main() {printf("hello linux");sleep(2);return 0; }对于这样的代码&#xff0c;首先可以肯…

C++11 unique_ptr智能指针

#include<iostream> using namespace std;class test { public:test() {cout << "调用构造函数" << endl;}~test() {cout << "调用析构函数" << endl;} };int main(void) {//1.构造函数unique_ptr<test>t1;unique_ptr…