Pseudo Code Generator

Code Generation Configuration and Execution Overview

This pseudocode outlines the process of configuring and executing a code generation tool, specifying settings for global, data source, strategy, package, and template configurations before executing the generation.


Empty image or helper icon

Prompt

public static void main(String[] args) {
        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(System.getProperty("user.dir") + "/sso-server/src/main/java") // 设置输出目录
                .setAuthor("Admin") // 设置作者
                .setOpen(false) // 设置生成后是否自动打开目录
                .setFileOverride(true) // 设置文件存在时是否覆盖
                .setServiceName("%sService") // 设置Service接口名后缀
                .setIdType(IdType.AUTO); // 设置主键生成策略

        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL) // 设置数据库类型
                .setUrl("jdbc:mysql://10.0.10.5:3306/xu-sso?allowMultiQueries=true&useUnicode=true&characterEncoding=utf8&useSSL=false") // 数据库连接URL
                .setUsername("app") // 数据库用户名
                .setPassword("Qidi@2352") // 数据库密码
                .setDriverName("com.mysql.jdbc.Driver"); // 数据库驱动类名

        // 策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setInclude("user") // 指定需要生成代码的表名
                .setNaming(NamingStrategy.underline_to_camel) // 设置表名转类名策略
                .setColumnNaming(NamingStrategy.underline_to_camel) // 设置列名转属性名策略
                .setEntityLombokModel(true) // 设置实体类使用Lombok模型
                .setRestControllerStyle(true); // 设置Controller使用REST风格
//                .setTablePrefix(new String[]{"tbl_"}); // 设置表名前缀

        // 包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.tusvn.sso") // 设置父包名
                .setMapper("mapper") // 设置Mapper接口所在的子包名
                .setEntity("entity") // 设置实体类所在的子包名
//                .setController("controller") // 设置Controller所在的子包名
                .setService("service") // 设置Service所在的子包名
                .setXml("mapper"); // 设置Mapper XML文件所在的子包名

        // 模板配置
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setXml(null) // 不生成XML文件
//                .setController("templates/controller.java.vm") // 设置Controller模板路径
                .setEntity("templates/entity.java.vm") // 设置实体类模板路径
                .setMapper("templates/mapper.java.vm")
                .setXml("templates/mapper.xml.vm"); // 设置Mapper接口模板路径

        // 整合配置
        AutoGenerator autoGenerator = new AutoGenerator();
        autoGenerator.setGlobalConfig(globalConfig)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(packageConfig)
                .setTemplate(templateConfig);

        // 执行生成
        autoGenerator.execute();
    }

Answer

Pseudocode: Code Generation Configuration and Execution

Main Function

// Main function to configure and execute code generation
Main Function() {
    // Global Configuration
    Set globalConfig = Create GlobalConfig()
    globalConfig.setOutputDirectory("current_directory/sso-server/src/main/java")
    globalConfig.setAuthor("Admin")
    globalConfig.setOpenDirectoryAutomatically(false)
    globalConfig.setFileOverrideEnabled(true)
    globalConfig.setServiceNamePattern("%sService")
    globalConfig.setIdGenerationStrategy(AUTO)

    // Data Source Configuration
    Set dataSourceConfig = Create DataSourceConfig()
    dataSourceConfig.setDatabaseType(MYSQL)
    dataSourceConfig.setConnectionURL("jdbc:mysql://10.0.10.5:3306/xu-sso?allowMultiQueries=true&useUnicode=true&characterEncoding=utf8&useSSL=false")
    dataSourceConfig.setUsername("app")
    dataSourceConfig.setPassword("Qidi@2352")
    dataSourceConfig.setDriver("com.mysql.jdbc.Driver")

    // Strategy Configuration
    Set strategyConfig = Create StrategyConfig()
    strategyConfig.setTablesToInclude("user")
    strategyConfig.setTableNamingStrategy(UNDERLINE_TO_CAMEL)
    strategyConfig.setColumnNamingStrategy(UNDERLINE_TO_CAMEL)
    strategyConfig.setEntityUseLombokModel(true)
    strategyConfig.setUseRESTControllerStyle(true)

    // Package Configuration
    Set packageConfig = Create PackageConfig()
    packageConfig.setParentPackage("com.tusvn.sso")
    packageConfig.setMapperSubPackage("mapper")
    packageConfig.setEntitySubPackage("entity")
    packageConfig.setServiceSubPackage("service")
    packageConfig.setMapperXMLSubPackage("mapper")

    // Template Configuration
    Set templateConfig = Create TemplateConfig()
    templateConfig.setGenerateXML(false)
    templateConfig.setEntityTemplatePath("templates/entity.java.vm")
    templateConfig.setMapperTemplatePath("templates/mapper.java.vm")
    templateConfig.setMapperXMLTemplatePath("templates/mapper.xml.vm")

    // Integrate All Configurations
    Set autoGenerator = Create AutoGenerator()
    autoGenerator.setGlobalConfig(globalConfig)
    autoGenerator.setDataSource(dataSourceConfig)
    autoGenerator.setStrategy(strategyConfig)
    autoGenerator.setPackageConfig(packageConfig)
    autoGenerator.setTemplateConfig(templateConfig)

    // Execute Code Generation
    autoGenerator.execute()
}

Key Components

Global Configuration

  • Output Directory: Set the directory where generated code will be placed.
  • Author: Specify the author name.
  • Open Directory: Flag to open the directory automatically after generation.
  • File Override: Determine whether to overwrite existing files.
  • Service Name: Specify the pattern for Service interface names.
  • ID Generation Strategy: Set the primary key generation strategy.

Data Source Configuration

  • Database Type: Set the type of database (e.g., MySQL).
  • Connection URL: URL for the database connection.
  • Username: Database username.
  • Password: Database password.
  • Driver: Fully qualified name of the database driver class.

Strategy Configuration

  • Tables to Include: Specify the tables for which code will be generated.
  • Naming Strategies: Set the strategies for naming tables and columns.
  • Lombok Model: Enable Lombok annotations in the entity classes.
  • REST Controller Style: Use REST style for controllers.

Package Configuration

  • Parent Package: Specify the parent package name.
  • Sub Packages: Define specific sub-packages for different components like Mapper, Entity, Controller, Service, and Mapper XML.

Template Configuration

  • Generate XML: Flag to control the generation of XML files.
  • Template Paths: Paths to template files for different components.

Integration and Execution

  • Auto Generator: Aggregate all configurations and execute the code generation using the AutoGenerator.

This pseudocode outlines how to configure various settings for code generation and execute the process, aiming at clarity and simplicity.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This pseudocode outlines the process of configuring and executing a code generation tool, specifying settings for global, data source, strategy, package, and template configurations before executing the generation.