├── model 实体类
├── dto 数据传输对象
├── repository 数据层
├── service 业务层
├── controller API层
├── config JPA审计,全局拦截(advice)
├── util 工具类
└── exception 异常处理
BaseEntity
NamedEntity
Dictionary
Ingredient
IngredientBodyDTO AuthRequestDTO
- pom.xml :
mysql-connector-j
spring-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
createRecipeWithIngredients
editRecipeWithIngredients
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
login
validateAndRefresh
refresh