Spring Boot 3 + AI 辅助开发实战:从入门到精通🚀
一、引言
1.1 Spring Boot 3的主要特性⚡
- 基于Java 17+的运行时环境
- 支持GraalVM原生镜像
- 改进的依赖管理
- 增强的监控和可观测性
- 更好的性能优化
1.2 当前AI辅助开发的发展现状🤖
-
随着科技不断发展,AI辅助开发也成为了程序员必不可少的辅助工具,但是你真的会用AI吗?🤔
-
你以为的AI编程助手😅
- 就是个代码补全工具
- 只会写简单的代码
- 经常给出错误的建议
- 就是个"高级复制粘贴"
-
实际上的AI编程助手🧠
- 能理解你的开发意图
- 可以生成完整的项目结构
- 能帮你优化代码性能
- 甚至可以帮你写测试用例
- 就像有个24小时在线的技术大牛
1.3 为什么选择Spring Boot 3 + AI辅助开发
-
**Spring Boot 3的"硬实力"**💪
- 基于Java 17+,性能提升30%
- 支持GraalVM原生镜像,启动速度提升50%
- 改进的依赖管理,就像从手动挡升级到了自动挡
- 增强的监控和可观测性,就像给车装上了360°全景摄像头
-
**AI辅助开发的"软实力"**🧠
- 代码生成准确率高达90%
- 开发效率提升40-50%
- 就像有个24小时在线的技术大牛
- 减少重复性工作,让开发更轻松
1.4 本文将要分享的内容 📚
- 用户管理 👥
- 学生注册和登录
- 教师注册和登录
- 管理员后台管理
二、环境准备🛠️
2.1 Spring Boot 3开发环境搭建💻
- JDK 17+安装配置
- Maven环境配置
2.2 常用AI编程助手介绍🤖
- Trae CN
- Cursor
- 相对于其他AI辅助工具,这两个我认为是开发java中我用过最好用的,比较与其他ai,他们的代码编辑和生成速度就快了很多,并且具有记忆功能,上下文能够随时切换问答。
2.4 项目初始化与基础结构📁
作为初学者,我推荐使用Spring Initializr来创建项目,这是最简单的方式。
-
使用IDEA创建(推荐)
- 打开IDEA,选择
File -> New -> Project
- 选择
Spring Initializr
- 填写项目信息: Group: com.example Artifact: blog-system Name: blog-system Description: Spring Boot 3 Blog System Package name: com.example.blog Language: Java Java Version: 17 Packaging: Jar
- 选择依赖: - Spring Web - Spring Data JPA - MySQL Driver - Lombok - Thymeleaf - Spring Security - Spring Boot DevTools
- 打开IDEA,选择
-
使用Spring Initializr网站
- 访问 start.spring.io/
- 填写相同的项目信息
- 选择相同的依赖
- 点击
Generate
下载项目
2.4.2 基础依赖配置
在
pom.xml
中添加必要的依赖和配置
xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>blog-system</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
</parent>
<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<!-- Spring Boot Starters -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- DevTools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
</project>
三、AI辅助开发实战
3.1 项目结构设计
- 考试模块
- 试卷模块
- 题库模块
- 成绩模块
3.1.1 使用AI生成标准的项目目录结构
-
考试管理模块 📚
- 考试信息管理
- 考试时间设置
- 考试规则配置
- 考试状态管理
-
试卷管理模块 📄
- 试卷创建
- 试题管理
- 试卷模板
- 试卷分析
-
题库管理模块 📋
- 题目分类
- 题目难度
- 题目导入导出
- 题目审核
-
成绩管理模块 📊
- 成绩录入
- 成绩统计
- 成绩分析
- 成绩导出
3.1.2 配置文件的自动生成
- 我们可以直接将需求给ai,让ai自动帮我们生成对应的模块以及所需要的依赖。
xml
# 应用配置
server:
port: 8080 # 服务器端口
servlet:
context-path: /api
# Spring 配置
spring:
application:
name: xiaoagen
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver #数据源驱动
url: jdbc:mysql://localhost:3306/table?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: root
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
# MyBatis-Plus 配置
mybatis-plus:
mapper-locations: classpath*:mapper/**/*Mapper.xml
type-aliases-package: com.zzh.xiaoagen.entity
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
id-type: auto
logic-delete-field: delFlag
logic-delete-value: 1
logic-not-delete-value: 0
# 日志配置
logging:
level:
com.zzh.xiaoagen: debug
org.springframework: warn
3.2 数据库设计
3.2.1 使用AI辅助设计数据库表结构
sql
create table exam
(
exam_id bigint auto_increment comment '考试ID'
primary key,
exam_name varchar(50) not null comment '考试名称',
exam_type char not null comment '考试类型',
start_time datetime not null comment '开始时间',
end_time datetime not null comment '结束时间',
duration int not null comment '考试时长',
total_score int not null comment '总分',
pass_score int not null comment '及格分数',
status char default '0' null comment '状态',
create_by varchar(64) default '' null comment '创建者',
create_time datetime null comment '创建时间',
update_by varchar(64) default '' null comment '更新者',
update_time datetime null comment '更新时间',
remark varchar(500) null comment '备注'
)
comment '考试表' engine = InnoDB;
create table paper
(
paper_id bigint auto_increment comment '试卷ID'
primary key,
paper_name varchar(50) not null comment '试卷名称',
exam_id bigint not null comment '考试ID',
total_score int not null comment '总分',
question_count int not null comment '题目数量',
create_by varchar(64) default '' null comment '创建者',
create_time datetime null comment '创建时间',
update_by varchar(64) default '' null comment '更新者',
update_time datetime null comment '更新时间',
remark varchar(500) null comment '备注'
)
comment '试卷表' engine = InnoDB;
create table question
(
question_id bigint auto_increment comment '题目ID'
primary key,
question_type char not null comment '题目类型',
question_content text not null comment '题目内容',
answer text not null comment '答案',
analysis text null comment '解析',
difficulty char not null comment '难度',
score int not null comment '分值',
create_by varchar(64) default '' null comment '创建者',
create_time datetime null comment '创建时间',
update_by varchar(64) default '' null comment '更新者',
update_time datetime null comment '更新时间',
remark varchar(500) null comment '备注'
)
comment '题目表' engine = InnoDB;
3.2.2 实体类自动生成
- 可以使用idea自带的数据库工具MybatisXGenerator生成 具体可以参考MyBatis Generator 超详细配置
- 可以直接帮我们生成实体类,mapper,server,impl等
3.2.3 启动程序
- 在web模块增加启动类,然后启动程序,很顺利,没有报错,一次成功。

四、结语
AI在本次框架搭建中,帮助我们构建了sql表,项目依赖和配置。非常快速的完成了项目的搭建, 在下一章节,我们继续进行模块的开发。