java-templates.mdc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. ---
  2. description:
  3. globs:
  4. alwaysApply: false
  5. ---
  6. # Java Backend Template Patterns
  7. ## Entity Template Pattern
  8. Entities follow the MyBatis-Plus active record pattern:
  9. ```java
  10. @Data
  11. @TableName("${tableName}")
  12. @EqualsAndHashCode(callSuper = true)
  13. public class ${ClassName}Entity extends Model<${ClassName}Entity> {
  14. // Fields with annotations
  15. }
  16. ```
  17. Key annotations used:
  18. - `@TableId(type = IdType.ASSIGN_ID)` - For primary keys
  19. - `@TableField(fill = FieldFill.INSERT)` - Auto-fill on insert
  20. - `@TableLogic` - Logical deletion flag
  21. - `@Schema` - OpenAPI documentation
  22. ## Controller Template Pattern
  23. Controllers follow RESTful conventions with Spring Boot:
  24. ```java
  25. @RestController
  26. @RequiredArgsConstructor
  27. @RequestMapping("/${functionName}")
  28. @Tag(description = "${tableComment}", name = "${tableComment}管理")
  29. public class ${ClassName}Controller {
  30. // CRUD endpoints
  31. }
  32. ```
  33. Standard endpoints:
  34. - `GET /{id}` - Get by ID
  35. - `GET /page` - Paginated list
  36. - `POST` - Create new
  37. - `PUT` - Update existing
  38. - `DELETE` - Delete (logical or physical)
  39. ## Service Layer Pattern
  40. Service interface:
  41. ```java
  42. public interface ${ClassName}Service extends IService<${ClassName}Entity> {
  43. // Custom business methods
  44. }
  45. ```
  46. Implementation extends ServiceImpl:
  47. ```java
  48. @Service
  49. @RequiredArgsConstructor
  50. public class ${ClassName}ServiceImpl extends ServiceImpl<${ClassName}Mapper, ${ClassName}Entity>
  51. implements ${ClassName}Service {
  52. // Business logic implementation
  53. }
  54. ```
  55. ## Mapper Pattern
  56. MyBatis mapper interface:
  57. ```java
  58. @Mapper
  59. public interface ${ClassName}Mapper extends BaseMapper<${ClassName}Entity> {
  60. // Custom SQL methods
  61. }
  62. ```
  63. ## Multi-Tenant Support
  64. When `isTenant` is true:
  65. - Entity includes `@TenantTable` annotation
  66. - Automatic tenant filtering in queries
  67. - Tenant ID auto-fill on insert
  68. ## Transaction Handling
  69. Master-detail operations use:
  70. ```java
  71. @Transactional(rollbackFor = Exception.class)
  72. public Boolean save${ClassName}(${ClassName}Entity entity) {
  73. // Parent save
  74. // Children save/update/delete
  75. }
  76. ```
  77. ## Security Annotations
  78. Common security patterns:
  79. - `@PreAuthorize("@pms.hasPermission('${functionName}_add')")` - Permission check
  80. - `@SysLog("新增${tableComment}")` - Audit logging
  81. - `@Inner(false)` - Internal service access control