Bootstrap

AutoGenerator代码自动生成

AutoGenerator代码自动生成

pom依赖:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
    </parent>
    <groupId>com.qf</groupId>
    <artifactId>AutoGenerate-Try</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <name>AutoGenerate-Try</name>
    <url>http://maven.apache.org</url>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>com.qf</groupId>
            <artifactId>jwt-spring-boot-starter</artifactId>
            <version>1.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-crypto</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

    </dependencies>
</project>

启动类:

package com.qf;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CodeGenerator {
    public static void main(String[] args) throws SQLException {
        generate("iot");

    }

    public static void generate(String dbnName) throws SQLException {
        AutoGenerator msg = new AutoGenerator();
        //全局配置
        GlobalConfig gcg = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gcg.setOutputDir(projectPath + "/src/main/java");
        gcg.setAuthor("Gu");
        gcg.setOpen(false);
        gcg.setBaseResultMap(true);
        gcg.setBaseColumnList(true);
        gcg.setSwagger2(true);
        msg.setGlobalConfig(gcg);
        //数据源配置
        DataSourceConfig dcg = new DataSourceConfig();
        dcg.setUrl("jdbc:mysql://localhost:3306/"+dbnName+"?serverTimezone=Asia/Shanghai&tinyInt1isBit=false");
        dcg.setDriverName("com.mysql.cj.jdbc.Driver");
        dcg.setUsername("root");
        dcg.setPassword("123456");
        msg.setDataSource(dcg);
        //包配置
        PackageConfig pcg = new PackageConfig();
        pcg.setParent("com.qf")
            .setController("controller")
            .setService("service")
            .setServiceImpl("service.impl")
            .setMapper("mapper")
            .setEntity("pojo");
        msg.setPackageInfo(pcg);
        //自定义配置
        InjectionConfig icg = new InjectionConfig() {
            @Override
            public void initMap() {

            }
        };
        String templatePath = "/templates/mapper.xml.vm";
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig() {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPath + "src/main/resources/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        icg.setFileOutConfigList(focList);
        msg.setCfg(icg);
        //模板配置
        TemplateConfig tcg = new TemplateConfig();
        tcg.setXml(null);
        msg.setTemplate(tcg);
        //策略配置
        StrategyConfig scg = new StrategyConfig();
        scg.setNaming(NamingStrategy.underline_to_camel);
        scg.setColumnNaming(NamingStrategy.underline_to_camel);
        scg.setEntityTableFieldAnnotationEnable(true);
        scg.setRestControllerStyle(true);
        scg.setEntityLombokModel(true);
        String[] tableNames = getTableNames(dcg,dbnName);
        scg.setInclude(tableNames);
        scg.setTablePrefix("");
        scg.setControllerMappingHyphenStyle(true);
        msg.setStrategy(scg);
        msg.setTemplateEngine(new VelocityTemplateEngine());
        msg.execute();

    }

    private static String[] getTableNames(DataSourceConfig dcg, String dbnName) throws SQLException {
        Connection connection = dcg.getConn();
        String sql = "select GROUP_CONCAT(table_name) from information_schema.tables where table_schema =?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,dbnName);
        ResultSet resultSet = statement.executeQuery();
        String[] tableNames={};
        if (resultSet.next()) {
            tableNames = resultSet.getString(1).split(",");
        }
        resultSet.close();
        statement.close();
        connection.close();
        return tableNames;
    }
}

启动后会自动生成mvc的三层文件以及mapper.xml文件。

;