Generate Source Code Default Rules
Introduction
When creating a project and generating source code, you can customize AI rule files and choose to customize the content of generated source code files. If none are selected, the default rules will be used to generate the relevant source code.
The default rules are as follows:
During development, please strictly follow these principles:
- SOLID, DRY, KISS, YAGNI: Ensure code maintainability, simplicity, and efficiency.
- OWASP security best practices: Including input validation, SQL injection protection, etc.
I. Technology Stack Specifications
Technology stack requirements
- Framework: Spring Boot 3.x + Java 17
- Dependencies: Spring Web, Spring Data JPA, Lombok, Swagger (SpringDoc), Spring Security (if permission control is required)
II. Application Logic Design Guidelines
Layered Architecture Principles
Layer Responsibilities Constraints Controller Handle HTTP requests/responses, define API interfaces 1. Must not directly operate the database; 2. Must call through Service layer Service Implement business logic, manage transactions, validate data 1. Must access database through Repository; 2. Return DTO instead of entity classes (unless necessary) Repository Define database query logic, persist data 1. Must inherit JpaRepository; 2. Use@EntityGraphto avoid N+1 query problemsEntity Database table structure mapping objects 1. Only for database interaction; 2. Must not be returned directly to frontend (requires DTO conversion), package name unified as entityImplementation classes of interfaces should be placed in the
implpackage under the interface package path
III. Security and Performance Guidelines
Input Validation
- Use
@Validannotation with JSR-303 validation annotations (such as@NotBlank,@Size). Note: Validation annotations for Spring Boot 3.x and above versions are located in thejakartapackage, notjavax. - Prohibit direct concatenation of SQL to prevent injection attacks.
- Use
Transaction Management
@Transactionalannotation only used for Service methods.- Avoid frequent transaction commits within loops.
Performance Optimization
- Use
@EntityGraphfor pre-loading associations. - Avoid executing database queries within loops, prioritize bulk operations.
- Use
IV. Code Style Guidelines
Naming Conventions
- Class names:
UpperCamelCase(e.g.,UserServiceImpl) - Method/variable names:
lowerCamelCase(e.g.,saveUser) - Constants:
UPPER_SNAKE_CASE(e.g.,MAX_LOGIN_ATTEMPTS)
- Class names:
Commenting Standards
- Classes, methods, and properties need Javadoc-style comments.
- Planned tasks need to be marked as
// TODO. - Logic with potential defects need to be marked as
// FIXME.
Code Formatting
- Use IntelliJ IDEA's default Spring Boot style.
- Prohibit manual adjustment of code indentation, rely on IDE auto-formatting.
Class Name Standards Comply with Alibaba naming conventions, such as entity classes ending with specific characters based on functionality (DTO, DO, BO, VO, Query).
- DTO (Data Transfer Object): Used for transferring data between different systems or services.
- DO (Data Object): Represents entity data in the database.
- BO (Business Object): Encapsulates business logic.
- VO (View Object): Displays data to users.
- Query: Represents query conditions or parameters.
V. Extensibility Design Guidelines
Interface-First Approach
- Separate service layer interfaces (such as
UserService) from implementations (such asUserServiceImpl).
- Separate service layer interfaces (such as
Extension Point Reservations
- Key business logic needs to support
StrategyorTemplatepattern extensions.
- Key business logic needs to support
Logging Standards
- Use
SLF4Jfor logging (prohibited to useSystem.out.println). - Core operations need to record
INFOlevel logs, exceptions recordERRORlevel.
- Use
