How to Use the Code Generator in the laokou-tool Module
The laokou-tool module provides utilities that power the KCloud-Platform-IoT code generator, which you invoke through the GeneratorApp entry point in the laokou-generator service to scaffold Java modules from database tables.
The laokou-tool module contains the core utilities supporting automated code generation in the koushenhai/kcloud-platform-iot repository. While the generator logic resides in laokou-service/laokou-generator, the tool module supplies the ModifyProjectBoot utility that enforces project-wide naming conventions. This guide explains how to configure and execute the generator to produce fully-structured Java modules containing entities, repositories, DTOs, services, controllers, and MyBatis-Plus mappers.
Prerequisites and Configuration
Before generating code, ensure your environment meets the following requirements and configure the database connection.
- JDK 1.8 or higher
- Maven 3.8 or higher (to build the multi-module repository)
- MySQL instance containing the target tables for code generation
- Module name prefix (e.g.,
laokou-order) for the generated output
Update the generator's data source configuration in laokou-service/laokou-generator/laokou-generator-start/src/main/resources/application.yml:
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/kcloud_iot?useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: yourPassword
Building and Running the Generator
Step 1: Build the Repository
Compile all modules including the generator from the repository root:
mvn clean install -DskipTests
This packages the required dependencies for the generator startup JAR.
Step 2: Start the Generator Service
The generator is a standard Spring Boot application. Start it via the executable JAR:
java -jar laokou-service/laokou-generator/laokou-generator-start/target/laokou-generator-start-*.jar
The main method in GeneratorApp.java initializes the Spring context and prints startup metrics via a stopwatch.
Step 3: Trigger Code Generation
Invoke the generator programmatically using the provided JUnit test class TableTest.java. The test_generatorGenerateCode() method demonstrates the required configuration:
@Test
void test_generatorGenerateCode() {
// Configure generator parameters
String sourceName = "generator"; // Template source module
String tablePrefix = "boot_generator_"; // Prefix to strip from table names
String moduleName = "laokou-generator"; // Target module prefix
String packageName = "org.laokou.generator"; // Base package
Set<String> tables = Set.of("generator_template", "generator_info"); // Tables to process
// Build configuration object
GeneratorA generatorA = new GeneratorA(author, packageName, moduleName,
sourceName, tablePrefix, tables);
// Execute generation
generatorDomainService.generateCode(generatorA);
}
The GeneratorDomainService.generateCode() method orchestrates the process by reading table metadata via TableMapper and TableColumnMapper, constructing TableE and ColumnE domain objects, and rendering templates.
Understanding the Generated Output
After execution, the generator creates a new module (e.g., newlaokou-generator) under the project root. ModifyProjectBoot.java automatically applies platform conventions to ensure consistency:
- pom.xml: Configured with
groupIdset tocn.org.laokouand proper module naming - src/main/java/: Contains packages under
cn.org.laokou.generatorwith entities, DTOs, services, and controllers - src/main/resources/: Houses MyBatis-Plus XML mapper files
Compare generated implementations like TemplateGatewayImpl.java against the original templates in laokou-service/laokou-generator/laokou-generator-infrastructure/template to verify output quality.
Integrating the Generated Module
To incorporate the new module into the platform:
- Add the generated module to the root
pom.xml(a<module>placeholder typically exists) - Execute
mvn clean installto compile the new code alongside existing services - Verify that package names and imports align with the
ModifyProjectBoottransformations
Summary
- The laokou-tool module supplies
ModifyProjectBootfor enforcing project naming conventions during generation. - The generator entry point is
GeneratorApp.javain thelaokou-generator-startmodule. - Configure database credentials in
application.ymlbefore running the generator. - Use
TableTest.javato programmatically trigger code generation with specific table sets. - Generated modules follow the platform's standard structure with
cn.org.laokougroupId and package hierarchy.
Frequently Asked Questions
Where is the main entry point for the code generator?
The main entry point is GeneratorApp.java located at laokou-service/laokou-generator/laokou-generator-start/src/main/java/org/laokou/generator/GeneratorApp.java. This Spring Boot application class bootstraps the generator service and manages the application lifecycle.
How does the laokou-tool module influence code generation?
The laokou-tool module provides ModifyProjectBoot.java, which automatically rewrites package names, groupIds, and module prefixes in generated files to match KCloud-Platform-IoT standards. This ensures all generated code adheres to the project-wide architectural conventions without manual intervention.
Can I generate code for multiple tables simultaneously?
Yes. Pass a Set<String> containing multiple table names to the GeneratorA configuration object, as demonstrated in TableTest.java. The generator iterates through the set, building TableE and ColumnE domain objects for each table and producing corresponding Java source files in a single execution.
What templates does the generator use for creating source files?
The generator uses templates stored in laokou-service/laokou-generator/laokou-generator-infrastructure/template. These templates define the structure for entities, gateways, implementations, and mappers. You can examine existing implementations like TemplateGatewayImpl.java and InfoGatewayImpl.java to understand the output format.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →