spring-boot快速开始

什么是spring-boot

  1. Spring Boot是一个脚手架
  2. 用于快速搭建一个基于Spring的web应用,开箱即用!创建即可开发业务代码。
  3. 其设计目的是用来简化Spring应用的初始搭建以及开发过程

使用idea 创建一个sping-boot 项目

image-20250513235300230

image-20250513235323358

别选太新的,也不要选shapshot 版本

勾选上

image-20250513235641671

image-20250513235706194

需要那个版本勾选哪个数据库版本

image-20250513235751777

jpa 是操作数据库的

然后点击创建

接下来配置数据库的连接地址

image-20250514000228317

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
spring.application.name=SpringBootDemo

server.port=18080

spring.datasource.url=jdbc:mysql://localhost:3306/db_springboot_study?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# 打印 spring
spring.jpa.show-sql=true
# 格式化 sql
spring.jpa.properties.hibernate.format_sql=true
# 自动建表
spring.jpa.hibernate.ddl-auto=update

image-20250514001204728

尝试启动

image-20250514001230507

项目结构

SpringBoot : 一个较完整的SpringBoot项目的目录结构_springboot项目目录结构-CSDN博客

image-20250514000250705

启动类

image-20250514000111831

简单的项目结构

image-20250514001515123

controller(访问控制层)

处理 HTTP 请求,接收客户端参数,调用 Service 层处理业务逻辑,并返回响应结果给客户端。

pojo(Plain Old Java Object)

定义数据模型,包括实体类(Entity)和数据传输对象(DTO)。

repository(数据访问层)

与数据库交互,提供数据持久化操作(增删改查)。

service(业务逻辑层)

实现核心业务逻辑,协调 Repository 层完成数据操作,并处理事务、权限等。

编写数据库表

在pojo 软件包中

添加User.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package cn.august.springbootdemo.pojo;

import jakarta.persistence.*;

@Table(name = "user")
@Entity
public class User {

@Id // 主键
@GeneratedValue(strategy = GenerationType.IDENTITY) // 通过自增的方式生成主键
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "age")
private Integer age;
@Column(name = "password")
private String password;

public void setId(Integer id) {
this.id = id;
}

public void setName(String name) {
this.name = name;
}

public void setAge(Integer age) {
this.age = age;
}

public void setPassword(String password) {
this.password = password;
}

public Integer getId() {
return id;
}

public String getName() {
return name;
}

public Integer getAge() {
return age;
}

public String getPassword() {
return password;
}

@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", password='" + password + '\'' +
'}';
}
}

由于我们使用了配置

1
spring.jpa.hibernate.ddl-auto=update

那么在启动的时候自动更新这个表。没有就创建

image-20250514002646286

编写逻辑

AddUser

image-20250514004033894

这个地方add 函数先忽略

使用post 作为请求方法

将UserDto 作为接受参数

UserDto 在这里的作用可以理解成go zero 中go api 层的传入,但是这个不能由表结构接受。因为可能字段不一样

image-20250514004214260

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package cn.august.springbootdemo.pojo.dto;


public class UserDto {
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

private String name;
private Integer age;
private String password;
}

然后创建service

image-20250514004318654

UserService.class

1
2
3
4
5
package cn.august.springbootdemo.service;

public interface UserService {
}

UserServiceImpl.class

1
2
3
4
5
6
7
8
9
10
11
package cn.august.springbootdemo.service.impl;


import cn.august.springbootdemo.pojo.dto.UserDto;
import cn.august.springbootdemo.service.UserService;
import org.springframework.stereotype.Service;

@Service // 绑定spring 中的bean
public class UserServiceImpl implements UserService {
}

添加了Service 注解之后在controller 添加

1
2
@Autowired
UserService userService;

迄今为止全部代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package cn.august.springbootdemo.controller;


import cn.august.springbootdemo.pojo.dto.UserDto;
import cn.august.springbootdemo.service.UserService;
import cn.august.springbootdemo.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController // 允许返回对象,并且默认返回json格式
@RequestMapping("/user")
public class UserController {

@Autowired
UserService userService;

// 增加用户
@PostMapping
public String addUser(@RequestBody UserDto user)
{
userService.add(user);
return "addUser";
}


// 删除用户
// 修改用户
// 查询用户
}

由于这个地方add 爆红,直接组织爆红!

image-20250514004705629

UserService.java

1
2
3
4
5
6
7
8
9
10
11
12
package cn.august.springbootdemo.service;

import cn.august.springbootdemo.pojo.dto.UserDto;

public interface UserService {
/**
* 添加用户
* @param user
*/
void add(UserDto user);
}

image-20250514004803933

UserServiceImpl.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package cn.august.springbootdemo.service.impl;


import cn.august.springbootdemo.pojo.dto.UserDto;
import cn.august.springbootdemo.service.UserService;
import org.springframework.stereotype.Service;

@Service // 绑定spring 中的bean
public class UserServiceImpl implements UserService {
@Override
public void add(UserDto user) {

}
}

到这一步需要在数据库中操作了,比如添加一个用户

image-20250514011003399

需要添加一个repository

UserRepository.class

1
2
3
4
5
6
7
8
9
10
11
package cn.august.springbootdemo.repository;

import cn.august.springbootdemo.pojo.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends CrudRepository<User, Integer> { // 继承这个自动实现curd 操作,User 是实体类,Integer 是主键类型

}

在回到UserServiceImpl 进行装在

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package cn.august.springbootdemo.service.impl;


import cn.august.springbootdemo.pojo.User;
import cn.august.springbootdemo.pojo.dto.UserDto;
import cn.august.springbootdemo.repository.UserRepository;
import cn.august.springbootdemo.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service // 绑定spring 中的bean
public class UserServiceImpl implements UserService {

@Autowired
private UserRepository userRepository;
@Override
public User add(UserDto user) {
User userPojo = new User();
BeanUtils.copyProperties(user,userPojo);

return userRepository.save(userPojo);// insert 和update都可以用
}
}

创建一个响应的结构

image-20250514012729339

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package cn.august.springbootdemo.pojo;

import org.springframework.http.HttpStatus;

public class ResponseMessage<T> {
private Integer code;
private String message;
private T data;
public ResponseMessage(Integer code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
}
public static <T> ResponseMessage<T> success(T data){
return new ResponseMessage<T>(HttpStatus.OK.value(),"success",data);
}
}

修改controller

1
2
3
4
5
6
7
@PostMapping
public ResponseMessage<User> addUser(@RequestBody UserDto user)
{
User newUser=userService.add(user);
return ResponseMessage.success(newUser);
}

发送请求

image-20250514013746739

引入参数验证库

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

然后在dto 添加 注解

1
2
3
4
5
6
7
8
9
@NotBlank(message = "用户名不能为空")
private String name;
@NotBlank(message = "年龄不能为空")
private Integer age;
@NotBlank(message = "密码不能为空")
private String password;
@Email(message = "邮箱格式错误")
private String email;

然后在controller

1
2
3
4
5
6
@PostMapping
public ResponseMessage<User> addUser(@Validated @RequestBody UserDto user)
{
User newUser=userService.add(user);
return ResponseMessage.success(newUser);
}

添加统一异常处理器通知


spring-boot快速开始
https://tsy244.github.io/2025/05/13/spring-boot/spring-boot快速开始/
Author
August Rosenberg
Posted on
May 13, 2025
Licensed under