编码

99 阅读2分钟

Code auto generator

使用mybatis-plus-generator-ui 实现代码自动生成

  1. 构建Springboot 项目
  2. 在项目中添加mybatis-plus-generator-ui 插件
<dependency>
    <groupId>com.github.davidfantasy</groupId>
    <artifactId>mybatis-plus-generator-ui</artifactId>
    <version>2.0.5</version>
    <scope>test</scope>
</dependency>

3.在test目录下创建初始化启动文件

public class GneratorUI {

    public static void main(String[] args) {
        GeneratorConfig config = GeneratorConfig.builder().jdbcUrl("jdbc:postgresql://10.238.126.73:5432/auto_generator")
                .userName("postgres")
                .password("postgres")
                .driverClassName("org.postgresql.Driver")
                //数据库schema,MSSQL,PGSQL,ORACLE,DB2类型的数据库需要指定
                //.schemaName("auto_generator")
                //数据库表前缀,生成entity名称时会去掉(v2.0.3新增)
                .tablePrefix("t_")
                //如果需要修改entity及其属性的命名规则,以及自定义各类生成文件的命名规则,可自定义一个NameConverter实例,覆盖相应的名称转换方法,详细可查看该接口的说明:
                .nameConverter(new NameConverter() {
                    /**
                     * 自定义Service类文件的名称规则,entityName是NameConverter.entityNameConvert处理表名后的返回结果,如有特别的需求可以自定义实现
                     */
                    @Override
                    public String serviceNameConvert(String entityName) {
                        return entityName + "Service";
                    }

                    /**
                     * 自定义Controller类文件的名称规则
                     */
                    @Override
                    public String controllerNameConvert(String entityName) {
                        return entityName + "Action";
                    }
                })
                //所有生成的java文件的父包名,后续也可单独在界面上设置
                .basePackage("com.pcitc.mybatis")
                .port(8068)
                .build();
        MybatisPlusToolsApplication.run(config);
    }
}
  1. 启动该项目,而后访问url http://localhost:8080
  2. 启动后可在页面查看所有的table示例 image.png 6.代码生成配置示例 image.png

基于若依框架的代码自动化生成工具

  1. 文件列表
    1. application.yml
    2. application-druid-postgressql.yml
    3. auto-code-1.0.0.jar
    4. auto-code-psql.sql
    5. 启动脚本
  2. 使用说明
    1. 构建PostgreSql数据库
    2. 使用sql脚本创建数据表
    3. 修改文件application-druid-postgressql.yml中的数据库连接信息
    4. 使用脚本启动服务
  3. 相关文件地址

Code Style

编码规范

  1. 使用 googleJavaFormat 插件
<plugin>
    <groupId>com.diffplug.spotless</groupId>
    <artifactId>spotless-maven-plugin</artifactId>
    <version>2.25.0</version>
    <configuration>
        <java>
            <includes>
                <include>src/main/java/**/*.java</include> <!-- Check application code -->
                <include>src/test/java/**/*.java</include> <!-- Check application tests code -->
            </includes>
            <googleJavaFormat>
                <version>1.15.0</version>
                <style>GOOGLE</style>
            </googleJavaFormat>
        </java>
    </configuration>
    <executions>
        <execution>
            <phase>verify</phase>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
</plugin>

  1. 集成git hooks 插件,定义脚本commit-msg以及pre-commit处理
<plugin>
    <groupId>com.rudikershaw.gitbuildhook</groupId>
    <artifactId>git-build-hook-maven-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <installHooks>
            <commit-msg>hooks/commit-msg</commit-msg>
            <pre-commit>hooks/pre-commit</pre-commit>
        </installHooks>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>install</goal>
            </goals>
        </execution>
    </executions>
</plugin>
  1. 定义脚本
    1. pre-commit
    #!/bin/sh
    echo "Running Spotless formatting check"
    # Run Spotless formatting check
    mvn spotless:check
    
    # Check if there are any formatting errors
    SPOTLESS_RESULT=$?
    if [ $SPOTLESS_RESULT -ne 0 ]; then
        echo "Spotless formatting check failed. Please format the code before committing."
        exit 1
    fi
    
    echo "Running Maven clean and compile"
    # Run Maven clean and compile
    mvn clean compile
    
    # Check if the compilation was successful
    COMPILE_RESULT=$?
    if [ $COMPILE_RESULT -ne 0 ]; then
        echo "Maven compilation failed. Please fix the compilation errors before committing."
        exit 1
    fi
    
    # All checks passed. Code can be committed.
    exit 0
    
    1. commit-msg
    #!/bin/sh
    # Create a regex for a conventional commit.
    convetional_commit_regex="^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(([a-z -]+))?!?: .+$"
    
    # Get the commit message (the parameter we're given is just the path to the
    # temporary file which holds the message).
    commit_message=$(cat "$1")
    
    # Check the message, if we match, all good baby.
    if [[ "$commit_message" =~ $convetional_commit_regex ]]; then
       echo -e "message meets Conventional Commit standards..."
       exit 0
    fi
    
    # Uh-oh, this is not a conventional commit, show an example and link to the spec.
    echo -e "\e[31mThe commit message does not meet the Conventional Commit standard\e[0m"
    echo "An example of a valid message is: "
    echo "  feat(login): add the 'remember me' button"
    echo "More details at: https://www.conventionalcommits.org/en/v1.0.0/#summary"
    exit 1