All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lq.cli.SpringBootCli Maven / Gradle / Ivy

package com.lq.cli;


import com.lq.cli.comment.CliConfig;
import com.lq.cli.comment.TaskArgs;
import com.lq.cli.init.CreateApplicationXmlTask;
import com.lq.cli.init.CreateConfigTask;
import com.lq.cli.init.CreatePomXmlTask;
import com.lq.cli.jdbc.JdbcConfig;
import com.lq.cli.jdbc.JdbcUtil;
import com.lq.cli.jdbc.TableInfo;
import com.lq.cli.mybatis.*;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class SpringBootCli {

    private static SpringBootCli instance = null;
    private final TaskArgs taskArgs;
    private final ExecutorService executorService;

    private SpringBootCli(Builder builder) {
        taskArgs = new TaskArgs();
        taskArgs.jdbcConfig = builder.jdbcConfig;
        taskArgs.cliConfig = builder.cliConfig == null ? new CliConfig.Builder().build() : builder.cliConfig;
        String path = new File(builder.aClass.getResource("").getPath()).getPath();
        taskArgs.projectPath = path.substring(0, path.indexOf(File.separator + "target" + File.separator));
        String filePath = taskArgs.projectPath + File.separator + "src" + File.separator + "main" + File.separator + "java" +
                File.separator + new File(builder.aClass.getName()).getPath().replace(".", File.separator) + ".java";
        taskArgs.rootPackagePath = filePath.replace(builder.aClass.getSimpleName() + ".java", "");
        taskArgs.packageName = builder.aClass.getPackage().getName();
        int threadNumber = Math.min(Runtime.getRuntime().availableProcessors() + 1, 4);
        executorService = Executors.newFixedThreadPool(threadNumber);
    }

    public static SpringBootCli getInstance(Class clazz, String jdbcUserName, String jdbcPwd, String dbName) {
        return getInstance(clazz, jdbcUserName, jdbcPwd, dbName, new CliConfig.Builder().build());
    }

    public static SpringBootCli getInstance(Class clazz, String jdbcUserName, String jdbcPwd, String dbName, CliConfig cliConfig) {
        if (instance == null) {
            synchronized (SpringBootCli.class) {
                if (instance == null) {
                    JdbcConfig jdbcConfig = new JdbcConfig.Builder(jdbcUserName, jdbcPwd, dbName).build();
                    instance = new SpringBootCli.Builder(clazz, jdbcConfig).setCliConfig(cliConfig).build();
                }
            }
        }
        return instance;
    }

    public void initSpringBoot(String... filterMybatisTableNames) throws Exception {
        try {
            JdbcUtil.checkMysqlConnectorJar();
            executorService.submit(new CreatePomXmlTask(taskArgs));
            executorService.submit(new CreateConfigTask(taskArgs));
            executorService.submit(new CreateApplicationXmlTask(taskArgs));
            //整合mybatis
            Future> listFuture = executorService.submit(new CreateJavaBeanTask(taskArgs));
            List tableInfos = listFuture.get();
            if (tableInfos != null && taskArgs.cliConfig.isUseMybatis()) {
                if (filterMybatisTableNames != null && filterMybatisTableNames.length > 0) {
                    List tableNames = Arrays.asList(filterMybatisTableNames);
                    tableInfos = tableInfos.stream()
                            .filter(tableInfo -> !tableNames.contains(tableInfo.getTableName()))
                            .collect(Collectors.toList());
                }
                executorService.submit(new CreateMapperTask(taskArgs, tableInfos));
                executorService.submit(new CreateServiceTask(taskArgs, tableInfos));
                executorService.submit(new CreateMapperXmlTask(taskArgs, tableInfos));
                if (taskArgs.cliConfig.isGenerateController()){
                    executorService.submit(new CreateControllerTask(taskArgs, tableInfos));
                }
            }
        } finally {
            executorService.shutdown();
        }
    }

    public void generateMybatisByTableName(String tableName, String... tableNames) throws Exception {
        generateFilterByTableName(tableInfo -> {
            List infos = Collections.singletonList(tableInfo);
            executorService.submit(new CreateMapperTask(taskArgs, infos));
            executorService.submit(new CreateServiceTask(taskArgs, infos));
            executorService.submit(new CreateMapperXmlTask(taskArgs, infos));
        },tableName,tableNames);
    }

    public void generateControllerByTableName(String tableName, String... tableNames) throws Exception {
        if (taskArgs.cliConfig.isUseWeb()){
            generateFilterByTableName(tableInfo ->
                    executorService.submit(new CreateControllerTask(taskArgs, Collections.singletonList(tableInfo))),tableName,tableNames);
        }
    }

    private void generateFilterByTableName(Consumer consumer,String tableName, String... tableNames) throws ExecutionException, InterruptedException {
        try {
            JdbcUtil.checkMysqlConnectorJar();
            Future> listFuture = executorService.submit(new CreateJavaBeanTask(taskArgs));
            List tableInfos = listFuture.get();
            if (tableInfos != null && taskArgs.cliConfig.isUseMybatis()) {
                List tableNameList = new ArrayList<>(Arrays.asList(tableNames));
                tableNameList.add(tableName);
                tableInfos.stream().filter(tableInfo -> tableNameList.contains(tableInfo.getTableName())).forEach(consumer);
            }
        } finally {
            executorService.shutdown();
        }
    }


    public static class Builder {
        private JdbcConfig jdbcConfig;
        private Class aClass;
        private CliConfig cliConfig;

        public Builder(Class aClass, JdbcConfig jdbcConfig) {
            this.jdbcConfig = jdbcConfig;
            this.aClass = aClass;
        }

        public Builder setCliConfig(CliConfig cliConfig) {
            this.cliConfig = cliConfig;
            return this;
        }

        public SpringBootCli build() {
            return new SpringBootCli(this);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy