环境安装
在开始学习Spring Boot之前,我们需要准备好开发环境。本节将以Windows操作系统为例,介绍如何安装JDK、Intellij IDEA及Apache Maven。如果你的电脑上已经安装了JDK、Intellij IDEA或者Apache Maven,可以跳过本节内容。
- 安装JDK
JDK(Java SE Development Kit)建议使用17及以上的版本,其官方下载路径为:
https://www.oracle.com/java/technologies/downloads/#java17
下载后正常安装,安装完成后,需要配置环境变量JAVA_HOME,具体步骤如下:
1)在电脑桌面上,右击【我的电脑】→【属性】→【高级系统设置】→【环境变量】→【系统变量(S)】→【新建】出现新建环境变量的窗口。
2)在【变量名】和【变量值】中分别输入JAVA_HOME和C:\Program Files\Java\jdk-17,单击【确定】按钮。
3)JAVA_HOME配置好之后,将%JAVA_HOME%\bin加入到【系统变量】的path中。完成后,打开命令行窗口,输入命令java-version,如出现如下所示的提示,即表示安装成功。
1 2 3 4
| c:\XXX\java -version java version "17.0.16" 2025-07-15 LTS Java(TM) SE Runtime Environment (build 17.0.16+12-LTS-247) Java HotSpot(TM) 64-Bit Server VM (build 17.0.16+12-LTS-247, mixed mode, sharing)
|
安装Intellij IDEA
在Intellij IDEA的官方网站
http://www.jetbrains.com/idea/ 上可以免费下载IDEA。下载完IDEA后,运行安装程序,按提示安装即可。
安装Apache Maven
Apache Maven是目前流行的项目管理和构建自动化工具。虽然IDEA已经包含了Maven插件,但是笔者还是希望大家在工作中能够安装自己的Maven插件,方便以后项目配置需要。大家可以通过Maven的官方网站
http://maven.apache.org/download.cgi 下载最新版的Maven,本文的Maven版本为apache-maven-3.6.3。
下载完成后解压缩即可,例如,解压到D:盘上,然后将Maven的安装路径D:\apache-maven-3.6.3\bin加入到Windows的环境变量path中。安装完成后,在命令行窗口执行命令:mvn -v,如果输出"Apache Maven 3.6.3",表示Maven安装成功。
一、创建一个空的Springboot项目工程
在一个你喜欢的地方,创建一个springboot项目文件夹.比如我使用:springboot

启动IDEA–>New Project–>spring boot(spring initializr)
按下面的选

jdk版本不一样,所选的spring boot版本不一样,因为我用的是jdk是17,所以用的是3.0以上的

二、 项目工程配置一下Maven


三、 创建常见的包
在com.example下面把每个层的包创建好,用于后续我们在不同的包里创建java文件,后端我们是分层的。
controller调用service
service调用dao
dao调用entity
- controller:后端接口的入口,主要编写各种xxxController,提供接口给前端调用
- service:后端业务层,主要编写一些后端业务逻辑。controller–:service
- dao(mapper):后端持久层,主要映射数据库,操作数据库表数据。service–
- entity:实体类,对应数据库表,实体类的属性对应表的字段信息
结果图:

四、编写你的第一个hello word
controller是后台接口的入口,这个“接口”与java基础里面的接口不一样,这里的接口是针对前端来说的,前端操作数据会调用后端的接口,是前后台交互的入口
- 在controller下面新建一个UserController类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.example.springboot.controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@GetMapping public String start(){ return "欢迎来到我的第一个Springboot工程:已经启动"; } }
|
运行SpingbootApplication,结果如下,可以看到端口为8080

去搜http://localhost:8080/user

在entity下面创建User类
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| package com.example.springboot.entity;
import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id;
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id; private String name; private String sex; private int age; private String password; private String phone;
public User(String id,String name,String sex,String age,String password,String phone ){ this.id= Integer.parseInt(id); this.name=name; this.sex=sex; this.age= Integer.parseInt(age); this.password=password; this.phone=phone; }
public User() { }
public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex(){ return sex; }
public void setSex(String sex) { this.sex = sex; }
public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; }
}
|
搜索http://localhost:8080/user/start

五、整合MyBatis
可以去搜mvn repository的官网,在里面搜mysql,可以找到相关依赖的代码
- 引入依赖:pox.xml里导入mybatis和数据库mysql的依赖(这里的mybatis依赖版本要看清楚,我这个版本不适用3.0,所以后面我换了,可以看第七点的1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>4.1.5</version> </dependency>
|
例为官方mybatis-spring-boot-starter,建议使用mybatis-plus-spring-boot3-starter替代
- 在application.yml里进行数据库配置(若文件后缀不是yml,可看下面第七的2)
注意:冒号“:”后面一定要加空格,不然会报错
1 2 3 4 5 6 7 8 9 10
| server: port: 8080
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: cjm2003 url: jdbc:mysql://localhost:3306/springboot?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
|
3.配置mybatis实体和xml映射
(1)先如下面结构创建UserMapper.xml用于被mybatis映射

(2)在application.yml里配置
1 2 3 4
| mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.example.springboot.entity
|
六、结合MyBatis将数据库打通
- 创建数据库springboot和user表
创建数据库

1 2 3 4 5 6 7 8 9 10
| DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int NOT NULL AUTO_INCREMENT COMMENT '主键ID', `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '姓名', `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '密码', `sex` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '性别', `age` int NULL DEFAULT NULL COMMENT '年龄', `phone` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '电话', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '用户信息表' ROW_FORMAT = Dynamic;
|
补充数据
1 2
| INSERT INTO `user` VALUES (1, '张三', '123456', '男', 21, '10000000000'); INSERT INTO `user` VALUES (2, '李四', '123456', '女', 22, '18888888888');
|
最终结果:

2. 在User类里加映射
1 2 3
| import javax.persistence.Table;
@Table(name = "user")
|
- 修改UserController
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
| package com.example.springboot.controller;
import com.example.springboot.entity.User; import com.example.springboot.service.UserService; import jakarta.annotation.Resource; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Resource private UserService userService;
@GetMapping public String start(){ return "欢迎来到我的第一个Springboot工程:已经启动"; }
@GetMapping("/start")
public List<User> getUser() { return userService.getUser();
}
}
|
- 创建其他的类和接口,因为
controller调用service
service调用dao
dao调用entity

UserService:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.example.springboot.service;
import com.example.springboot.dao.UserDao; import com.example.springboot.entity.User; import jakarta.annotation.Resource; import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Resource private UserDao userDao;
public List<User> getUser() { return (List<User>) userDao.getUser(); } }
|
UserDao接口:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.example.springboot.dao;
import com.example.springboot.entity.User; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; import tk.mybatis.mapper.common.Mapper;
import java.util.List;
@Repository
public interface UserDao extends Mapper<User> {
@Select("select * from user") List<User> getUser(); }
|
- 搜索http://localhost:8080/user/start (这里若是打不开,可以看看是否是数据库没启动或者是路径错误,可看第七点3)

七、可能会遇到的问题
1.mybatis版本不兼容,去pom.xml里替换依赖
1 2 3 4 5 6
| <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency>
|
- 方法:通过IDE创建(以IntelliJ IDEA为例)
(1)新建Spring Boot项目 使用 Spring Initializr 创建项目(默认生成 .properties)。
(2)右键资源目录 定位到 src/main/resources → 右键选择 New → File。
(3)输入文件名 直接输入 application.yml → 确认创建。
- 路径错误或者是数据库没启动
(1)检查yml里的mybatis配置路径:是否在对应包的下面

检查数据库配置是否是自己的数据库名称出错


(2)去cmd里面查看服务器是否启动
注意mysql为自己的,一般为mysql,但是我的是mysql8.0,所以我的效果图如下

八、打包应用
在项目目录下,打开终端并使用以下命令打包应用:
对于 Maven:
mvn clean package
对于 Gradle:
./gradlew clean bootJar
注释:
mvn clean package 命令会清理项目并构建一个新的 JAR 文件。
./gradlew clean bootJar 对于 Gradle 项目同样会完成清理并生成可执行的 JAR 文件。
打包完成后,您可以在以下路径找到生成的 JAR 文件:
Maven:target 目录下,如 target/springboot-0.0.1-SNAPSHOT.jar
Gradle:build/libs 目录下,如 build/libs/springboot-0.0.1-SNAPSHOT.jar
九、代码
UserController
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
| package com.example.springboot.controller;
import com.example.springboot.entity.User; import com.example.springboot.service.UserService; import jakarta.annotation.Resource; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Resource private UserService userService;
@GetMapping public String start(){ return "欢迎来到我的第一个Springboot工程:已经启动"; }
@GetMapping("/start")
public List<User> getUser() { return userService.getUser();
}
}
|
UserDao
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.example.springboot.dao;
import com.example.springboot.entity.User; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; import tk.mybatis.mapper.common.Mapper;
import java.util.List;
@Repository
public interface UserDao extends Mapper<User> {
@Select("select * from user") List<User> getUser(); }
|
User
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| package com.example.springboot.entity;
import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table;
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id; private String name; private String sex; private int age; private String password; private String phone;
public User(String id,String name,String sex,String age,String password,String phone ){ this.id= Integer.parseInt(id); this.name=name; this.sex=sex; this.age= Integer.parseInt(age); this.password=password; this.phone=phone; }
public User() { }
public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex(){ return sex; }
public void setSex(String sex) { this.sex = sex; }
public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; }
}
|
UserService
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.example.springboot.service;
import com.example.springboot.dao.UserDao; import com.example.springboot.entity.User; import jakarta.annotation.Resource; import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Resource private UserDao userDao;
public List<User> getUser() { return (List<User>) userDao.getUser(); } }
|
SpringbootApplication
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.example.springboot;
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.springboot.dao")
public class SpringbootApplication {
public static void main(String[] args) { SpringApplication.run(SpringbootApplication.class, args); }
}
|
UserMapper.xml
1 2 3 4 5 6
| <?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.example.springboot.dao.UserDao">
</mapper>
|
application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| server: port: 8080
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: cjm2003 url: jdbc:mysql://localhost:3306/springboot?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.example.springboot.entity
|
pom.xml
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 63 64 65 66 67 68 69 70 71 72
| <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.5.3</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot</name> <description>springboot</description> <url/> <licenses> <license/> </licenses> <developers> <developer/> </developers> <scm> <connection/> <developerConnection/> <tag/> <url/> </scm> <properties> <java.version>17</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> <scope>test</scope> </dependency>
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>4.1.5</version> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>
|