├── model 实体类
├── dto 数据传输对象
├── repository 数据层
├── service 业务层
├── controller API层
├── config JPA审计,全局拦截(advice)
├── util 工具类
└── exception 异常处理
BaseEntity
NamedEntity
Dictionary
Ingredient
IngredientBodyDTO AuthRequestDTO
- pom.xml :
mysql-connector-jspring-boot-docker-compose - application-mysql
- compose.yml
- 引入 Spring Data JPA 的 @EnableJpaAuditing 以启用自动时间管理,例:JpaConfig.java
@CreatedDate@LastModifiedDate,例:BaseEntity.java
多个当前实体对象Ingredient可以对应一个关联的目标实体对象Dictionary
Recipe(菜谱)和Ingredient(食材)是多对多,创建中间实体RecipeIngredient
BaseEntity RecipeProjection RecipeSummaryDTO
- 在 Spring Data JPA 的 JpaRepository 中使用 Page 对象实现分页查询,例如 RecipeRepository.java
- 在 Service 层实现分页查询,例如:RecipeService.java
- 在 Controller 层处理分页请求,例如:RecipeController.java
- 创建ResourceNotFoundException
- 在
Service或Controller中 throwResourceNotFoundException,例如DictionaryServiceImpl - 创建 the Global Exception Handler:GlobalExceptionHandler
- 新建IngredientSpecification.java,用于动态构建查询条件
- 修改IngredientRepository.java,继承
JpaSpecificationExecutor - 在 IngredientService.java 中调用动态查询
- 在 IngredientController.java 中添加接口
GET /ingredients/search?name=鸡蛋&unit=1
- 新建RecipeSpecification.java,用于动态构建查询条件
- 修改RecipeRepository.java,继承
JpaSpecificationExecutor - 在 RecipeService.java 中调用动态查询
- 在 RecipeController.java 中添加接口
GET /recipes/list/search?name=面条&description=辣&ingredients=肉丝 青椒
- pom.xml中添加
spring-boot-starter-data-redis和spring-boot-starter-cache - application-redis.properties
- 在主类或配置类上添加
@EnableCaching注解 : RedisConfig.java @Cacheable@CachePut@CacheEvict: DictionaryService.java- 默认情况下,Redis 使用二进制序列化,但你可以配置 JSON 序列化以提高可读性 : RedisConfig.java : 添加
RedisTemplate和CacheManager的配置,并且使用相同的序列化方式
- application-mysql.properties
${DB_HOST:localhost} - application-redis.properties
${DB_HOST:localhost} - Dockerfile
- docker-compose.yml
- build.sh
- RecipeService.java
createRecipeWithIngredientseditRecipeWithIngredients
User.java RecipeIngredientId.java
- pom.xml 添加 spring-boot-starter-security
- 实现
UserDetailsService: CustomUserDetailsService - 自定义 JWT 过滤器 : JwtAuthenticationFilter
- 通过
SecurityFilterChain配置Spring Security: SecurityConfig - JWT 工具类 : JwtUtils
- AuthService
- AuthController
- JwtAuthenticationFilter
- SecurityConfig
- 启用方法级别的安全配置:
@EnableMethodSecurity(prePostEnabled = true) - 添加异常处理配置
- 启用方法级别的安全配置:
- DictionaryController
@PreAuthorize("hasRole('ADMIN')")
-
passwordEncoder
-
register
- AuthController
loginvalidateAndRefreshrefresh
此脚本用于备份 mydatabase 数据库。它会连接到正在运行的 MySQL Docker 容器,使用 mysqldump 导出数据,并通过 gzip 进行实时压缩,最终生成一个带有时间戳的 .sql.gz 文件。
-
添加执行权限 (只需操作一次):
chmod +x backup.sh
-
运行备份脚本:
./backup.sh
脚本执行后,会在项目根目录下的
./mysql_backups/文件夹中创建一个名为backup_YYYY-MM-DD_HH-MM-SS.sql.gz的压缩备份文件。
此脚本用于从一个 .sql.gz 备份文件中恢复 mydatabase 数据库。它会自动查找所有可用的备份文件,并提供一个交互式菜单供您选择。请注意:这是一个危险操作,它会覆盖当前数据库的所有数据。
-
添加执行权限 (只需操作一次):
chmod +x restore.sh
-
运行恢复脚本:
./restore.sh
脚本会列出所有在
./mysql_backups/目录中找到的备份文件。您只需输入对应的数字并按回车,然后根据提示输入y确认,即可开始恢复过程。
- AuthServiceImpl.java
- loginByWechat
- GitHub 仓库配置
Actions secrets - docker-compose.yml 配置
environment,这样,Spring Boot 就能通过application.properties读取环境变量。 - 在main.yml脚本中写入 .env 文件(docker-compose 会自动读取 .env 里的变量)。