# How to Use the Code Generator in the laokou-tool Module

> Master the koushenhai laokou-tool module code generator to quickly scaffold Java modules from your database tables. Streamline your development with GeneratorApp.

- Repository: [laokou/kcloud-platform-iot](https://github.com/koushenhai/kcloud-platform-iot)
- Tags: how-to-guide
- Published: 2026-03-05

---

**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`](https://github.com/koushenhai/kcloud-platform-iot/blob/main/laokou-service/laokou-generator/laokou-generator-start/src/main/resources/application.yml):

```yaml
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:

```bash
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:

```bash
java -jar laokou-service/laokou-generator/laokou-generator-start/target/laokou-generator-start-*.jar

```

The `main` method in **[GeneratorApp.java](https://github.com/koushenhai/kcloud-platform-iot/blob/master/laokou-service/laokou-generator/laokou-generator-start/src/main/java/org/laokou/generator/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](https://github.com/koushenhai/kcloud-platform-iot/blob/master/laokou-service/laokou-generator/laokou-generator-start/src/test/java/org/laokou/generator/TableTest.java)**. The `test_generatorGenerateCode()` method demonstrates the required configuration:

```java
@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](https://github.com/koushenhai/kcloud-platform-iot/blob/master/laokou-tool/src/main/java/org/laokou/tool/ModifyProjectBoot.java)** automatically applies platform conventions to ensure consistency:

- **pom.xml**: Configured with `groupId` set to `cn.org.laokou` and proper module naming
- **src/main/java/**: Contains packages under `cn.org.laokou.generator` with entities, DTOs, services, and controllers
- **src/main/resources/**: Houses MyBatis-Plus XML mapper files

Compare generated implementations like **[TemplateGatewayImpl.java](https://github.com/koushenhai/kcloud-platform-iot/blob/master/laokou-service/laokou-generator/laokou-generator-infrastructure/src/main/java/org/laokou/generator/template/gatewayimpl/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:

1. Add the generated module to the root [`pom.xml`](https://github.com/koushenhai/kcloud-platform-iot/blob/main/pom.xml) (a `<module>` placeholder typically exists)
2. Execute `mvn clean install` to compile the new code alongside existing services
3. Verify that package names and imports align with the `ModifyProjectBoot` transformations

## Summary

- The **laokou-tool** module supplies `ModifyProjectBoot` for enforcing project naming conventions during generation.
- The generator entry point is **[`GeneratorApp.java`](https://github.com/koushenhai/kcloud-platform-iot/blob/main/GeneratorApp.java)** in the `laokou-generator-start` module.
- Configure database credentials in **[`application.yml`](https://github.com/koushenhai/kcloud-platform-iot/blob/main/application.yml)** before running the generator.
- Use **[`TableTest.java`](https://github.com/koushenhai/kcloud-platform-iot/blob/main/TableTest.java)** to programmatically trigger code generation with specific table sets.
- Generated modules follow the platform's standard structure with `cn.org.laokou` groupId and package hierarchy.

## Frequently Asked Questions

### Where is the main entry point for the code generator?

The main entry point is **[`GeneratorApp.java`](https://github.com/koushenhai/kcloud-platform-iot/blob/main/GeneratorApp.java)** located at [`laokou-service/laokou-generator/laokou-generator-start/src/main/java/org/laokou/generator/GeneratorApp.java`](https://github.com/koushenhai/kcloud-platform-iot/blob/main/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`](https://github.com/koushenhai/kcloud-platform-iot/blob/main/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`](https://github.com/koushenhai/kcloud-platform-iot/blob/main/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`](https://github.com/koushenhai/kcloud-platform-iot/blob/main/TemplateGatewayImpl.java) and [`InfoGatewayImpl.java`](https://github.com/koushenhai/kcloud-platform-iot/blob/main/InfoGatewayImpl.java) to understand the output format.