5分钟——快速搭建后端springboot项目

news/2024/5/5 12:11:42

5分钟——快速搭建后端springboot项目

  • 1. idea新建工程
  • 2. 构建pom.xml文件
  • 3. 构建application.yml配置文件
  • 4. 构建springboot启动类
  • 5. 补充增删改查代码
  • 6. 运行代码
  • 7. 下一章

1. idea新建工程

点击右上角新建一个代码工程
在这里插入图片描述
别的地方不太一样也不用太担心,先创建一个工程就好。
name: small_tieba_new
groupId: org.example
ArtifactId: small_tieba_new
在这里插入图片描述
如果有这个弹窗,点击新窗口就好
在这里插入图片描述

2. 构建pom.xml文件

创建后之后就是这个样子,现在我们先填充一下pom.xml文件。
在这里插入图片描述
把下面的代码粘贴进pom.xml文件后点击一下右上角的小圆圈。(如果有pom.xml文件直接全选文件内容然后粘贴就好。如果没有则在相同位置新建一个名为pom.xml文件然后再粘贴下面提供的代码就好)
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><artifactId>spring-boot-starter-parent</artifactId><groupId>org.springframework.boot</groupId><version>2.5.1</version></parent><groupId>org.example</groupId><artifactId>small_tieba</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>11</maven.compiler.source><maven.compiler.target>11</maven.compiler.target><java.version>11</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.0</version></dependency><!--MySQL数据库驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!--druid数据库连接池依赖--><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.8</version></dependency><!--Lombok依赖(可以配置也可以不用配置具体看自己)--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

3. 构建application.yml配置文件

右键resources,新建一个文件
在这里插入图片描述
名字叫做 application.yml, 然后再把下列代码粘贴进去。

server:#设置端口号port: 8081 #默认端口是8080
spring:datasource:#数据库用户名username: root#数据库用户密码password: 123456#serverTimezone=UTC 解决市区的报错 一般mysql是8.0以上的是必须配置这个#userUnicode=true&characterEncoding=utf-8 指定字符编码、解码格式url: jdbc:mysql://localhost:3307/small_tieba?serverTimezone=UTC&userUnicode=true&characterEncoding=utf-8#设置驱动类driver-class-name: com.mysql.cj.jdbc.Driver#设置数据源type: com.alibaba.druid.pool.DruidDataSource# 配置mybatis
mybatis:#指定pojo扫描包位置让mybatis自动扫描到指定义的pojo包下type-aliases-package: com.me.test.pojo#指定位置扫描Mapper接口对应的XML文件 classpath:xml文件位置mapper-locations: classpath:mapper/*.xml

完成后的样子~
在这里插入图片描述

4. 构建springboot启动类

修改Main文件的内容(如果没有则自己新建一个class文件):

package org.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @author yourname <yourname>* Created on ${YEAR}-${MONTH}-${DAY}*/
@SpringBootApplication
public class Main {public static void main(String[] args) {System.out.println("Hello world!");SpringApplication.run(Main.class, args);}
}

完成后的样子~
在这里插入图片描述

5. 补充增删改查代码

新建文件夹补充代码。
在这里插入图片描述
然后在新建好的文件夹里面新建类(Class)文件
在这里插入图片描述
然后将下面的代码分别粘贴到对应的类(Class)文件中
UserController

package org.example.user.controller;import java.util.List;import org.example.user.entity.User;
import org.example.user.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author yourname <yourname>*/
@RestController
@RequestMapping("/api/user")
public class UserController {@Autowiredprivate UserServiceImpl userService;/*** 查询所有*/@GetMappingList<User> getAll(){List<User> user = userService.getUser();return user;}/*** 新增*/@PostMappingvoid add(@RequestBody User user){userService.insertUser(user);}/*** 修改*/@PutMappingvoid update(@RequestBody User user){userService.updateUser(user);}/*** 通过id删除*/@DeleteMappingvoid deleteById(@RequestBody User user){userService.delUser(user.getId());}
}

entity.User

package org.example.user.entity;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;import lombok.Data;/*** User实体类*/
@Data
public class User {@TableId(type = IdType.AUTO)private Integer id;private String username;}

mapper.UserMapper

package org.example.user.mapper;import org.apache.ibatis.annotations.Mapper;
import org.example.user.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;/*** <p>* UserMapper 接口* </p>*/
@Mapper
public interface UserMapper extends BaseMapper<User> {}

service.UserServiceImpl

package org.example.user.service;import java.util.List;import org.example.user.entity.User;
import org.example.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** @author yourname <yourname>* Created on 2024-04-22*/
@Service
public class UserServiceImpl {@Autowiredprivate UserMapper userMapper;/*** 新增user*/public void insertUser(User user) {userMapper.insert(user);}/*** 通过userId删除user*/public void delUser(Integer userId) {userMapper.deleteById(userId);}/*** 通过userId修改user*/public void updateUser(User user) {userMapper.updateById(user);}/*** 获取所有user*/public List<User> getUser() {List<User> users = userMapper.selectList(null);return users;}}

完成后的样子~
在这里插入图片描述

6. 运行代码

运行main函数
在这里插入图片描述
可以看见Started Main in 12.841 seconds就代表运行成功啦
在这里插入图片描述
打开浏览器输入网址:http://localhost:8081/api/user 就能看见我们数据库中的内容了
在这里插入图片描述

7. 下一章

5分钟——测试搭建的springboot接口(二)


http://www.mrgr.cn/p/70604821

相关文章

NumPy 1.26 中文官方指南(二)

NumPy 1.26 中文官方指南(二) NumPy: 绝对初学者的基础知识原文:numpy.org/doc/1.26/user/absolute_beginners.html欢迎来到 NumPy 的绝对初学者指南!如果你有评论或建议,请不要犹豫联系我们! 欢迎来到 NumPy! NumPy(Numerical Python)是一个开源的 Python 库,几乎在…

spark standalone同时运行pyspark和spark-shell

需要限制资源数量,使用 spark.cores.max 或 --total-executor-cores 来指定最大核数。 假设集群一共4c5.6g pyspark(使用2c2g) from pyspark.sql import SparkSessionspark = SparkSession.builder \.master("spark://worker1:7077") \.appName("pysparkApp&…

解决Vue3项目运行控制台警告

运行Vue3项目,控制台警告:Feature flag VUE_PROD_HYDRATION_MISMATCH_DETAILS is not explicitly defined. You are running the esm-bundler build of Vue, which expects these compile-time feature flags to be globally injected via the bundler config in order to ge…

Redis入门到通关之数据结构解析-SkipList

文章目录 ☃️概述☃️总结 欢迎来到 请回答1024 的博客 &#x1f353;&#x1f353;&#x1f353;欢迎来到 请回答1024的博客 关于博主&#xff1a; 我是 请回答1024&#xff0c;一个追求数学与计算的边界、时间与空间的平衡&#xff0c;0与1的延伸的后端开发者。 博客特色&…

解决警告

运行Vue3项目,控制台警告:Feature flag VUE_PROD_HYDRATION_MISMATCH_DETAILS is not explicitly defined. You are running the esm-bundler build of Vue, which expects these compile-time feature flags to be globally injected via the bundler config in order to ge…

日本岛津电子天平UW UX 系列series 精密电子天平Shimadzu使用说明

日本岛津电子天平UW UX 系列series 精密电子天平Shimadzu使用说明

python-自动化篇-终极工具-用GUI自动控制键盘和鼠标-pyautogui-键盘

文章目录 键盘键盘——记忆宫殿入门——通过键盘发送一个字符串——typewrite()常规——键名——typewrite()常规——按下键盘——keyDown()常规——释放键盘——keyUp()升级——热键组合——hotkey() 键盘 pyautogui也有一些函数向计算机发送虚拟按键&#xff0c;让你能够填充…

微信小程序4~6章总结

目录 第四章 页面组件总结 4.1 组件的定义及属性 4.2 容器视图组件 4.2.1 view 4.2.2 scroll-view 4.2.3 swiper 4.3 基础内容组件 4.3.1 icon ​编辑 4.3.2 text 4.3.3 progress ​编辑 4.4 表单组件 4.4.1 button 4.4.2 radio 4.4.3 checkbox 4.4.4 switch …

手撕sql面试题:根据分数进行排名,不使用窗口函数

分享一道面试题&#xff1a; 有一个分数表id 是该表的主键。该表的每一行都包含了一场考试的分数。Score 是一个有两位小数点的浮点值。 以下是表结构和数据&#xff1a; Create table Scores ( id int(11) NOT NULL AUTO_INCREMENT, score DECIMAL(3,2), PRIMARY KEY…

OpenAI未至,Open-Sora再度升级!已支持生成16秒720p视频

Open-Sora 在开源社区悄悄更新了!现在支持长达 16 秒的视频生成,分辨率最高可达 720p,并且可以处理任何宽高比的文本到图像、文本到视频、图像到视频、视频到视频和无限长视频的生成需求。我们来试试效果。 生成个横屏圣诞雪景,发b站再生成个竖屏,发抖音还能生成16秒的长视…

解决宏定义后面无法加分号

总结&#xff1a;注意是针对单行if语句使用&#xff0c;并且宏定义后面必须带分号&#xff08;格式统一&#xff09; 参考连接 C语言种do_while(0)的妙用_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1vk4y1R7VJ/?spm_id_from333.337.search-card.all.click&vd_…

OpenCV——Bernsen局部阈值二值化方法

目录 一、Bernsen算法1、算法概述2、参考文献二、代码实现三、结果展示Bernsen局部阈值二值化方法由CSDN点云侠原创,爬虫自重。如果你不是在点云侠的博客中看到该文章,那么此处便是不要脸的爬虫。 一、Bernsen算法 1、算法概述 Bernsen 算法是另一种流行的局部阈值二值化方…

Jenkins CI/CD 持续集成专题四 Jenkins服务器IP更换

一、查看brew 的 services brew services list 二、编辑 homebrew.mxcl.jenkins-lts.plist 将下面的httpListenAddress值修改为自己的ip 服务器&#xff0c;这里我是用的本机的ip 三 、重新启动 jenkins-lts brew services restart jenkins-lts 四 、浏览器访问 http://10.…

Microchip 32位MCU CAN驱动图文教程-附源码

文章目录 创建一个新的32位MCU工程Microchip MCC Harmony配置界面说明在MCC下配置系统的时钟在MCC下配置所需要使用的模块配置调试打印模块配置CAN模块配置管脚功能修改系统堆栈大小生成代码 添加用户代码 创建一个新的32位MCU工程 确保电脑上已经安装最新的MPlab X IDE、XC32编…

黑龙江—等保测评三级安全设计思路

需求分析 6.1、 系统现状 6.2、 现有措施 目前&#xff0c;信息系统已经采取了下述的安全措施&#xff1a; 1、在物理层面上&#xff0c; 2、在网络层面上&#xff0c; 3、在系统层面上&#xff0c; 4、在应用层面上&#xff0c; 5、在管理层面上&#xff0c; 6.…

几种中文字体的比较

根据自己的喜好给常见的几个中文字体的打分:字体选项 字体名 得分adobe Adobe 宋体 Std 5fandol FandolSong 0founder 方正书宋_GPK 10hanyi 汉仪宋体 6sinotype 华文宋体 3win 中易宋体 9fandol 缺少偏僻字体,故得零分。

数据治理之数据质量管理

一、数据质量概述什么是数据质量数据质量差的危害数据质量维度(数据六大评价标准)什么是数据质量测量数据质量测量必须要有目的数据质量测量必须可重复数据质量测量必须可解释什么是数据质量管理二、数据问题根因分析什么是根因分析为什么要进行根因分析产生数据问题的阶段规…

第十五届蓝桥杯省赛第二场C/C++B组A题【进制】题解(AC)

解题思路 按照题意进行模拟&#xff0c;计算 x x x 的 b b b 进制过程中&#xff0c;若出现余数大于 9 9 9&#xff0c;则说明 x x x 的 b b b 进制一定要用字母进行表示。 #include <iostream> #include <cstring> #include <algorithm> #include &l…

如何用微信发布考试成绩(如月考、期中、期末等)

自教育部《未成年人学校保护规定》颁布后,教育部明确表示:学校不得公开学生的考试成绩、排名等信息!同时学校应采取措施,便利家长知道学生的成绩等学业信息,对于教师来说,如何用微信发布考试成绩(如:月考、期中、期末等)就成了一道难题... 公开吧,会伤害到学生自尊心,甚至被投诉…

implicit declaration of item ‘write’; did him mean ‘fwrite’?

include <unistd.h> implicit declaration of item ‘write’; did him mean ‘fwrite’?Ask QuestionQuestions 2 years, 5 months ago Modified 2 years, 5 months ago Viewed 5k times 0IODIN bundled an case of a uncomplicated note-taking program that uses sav…