mybatisplus 自动生成实体方法

package com.xxl.job;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class MybatisGenerate {
    /**
     * 生成文件所在项目路径
     */
    private static final String baseProjectPath = "c:\\v5.0\\java";

    /**
     * 基本包名
     */
    private static final String basePackage = "com.s";
    /**
     * author
     */
    private final String authorName = "c";


    /**
     * 数据库配置四要素
     */
    private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
    private static final String URL = "jdbc:mysql://";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "";


    private static final List<String> TABLES_EXCLUDE = Arrays.asList("shu");
    static String  s= "t_e";
    private static final List<String> TABLES_ONLY = Arrays.stream(s.split(";\n")).collect(Collectors.toList());

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        if (TABLES_ONLY.size() > 0) {
            new MybatisGenerate().generateByTables(basePackage, TABLES_ONLY);
        } else {
            Class.forName(DRIVER_NAME);
            Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            ResultSet tables = connection.getMetaData().getTables(null, null, "%", new String[]{"TABLE"});
            List<String> t = new ArrayList<>();
            while (tables.next()) {
                String tableName = tables.getString("table_name");
                if (TABLES_EXCLUDE.contains(tableName)) {
                    continue;
                }
                t.add(tableName);
            }

            System.out.println(Arrays.toString(t.toArray()));
            new MybatisGenerate().generateByTables(basePackage, t);
        }
    }


    private void generateByTables(String packageName, List<String> tables) {
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setEnableCache(false)
                .setBaseColumnList(false)
                .setBaseResultMap(false)
                .setAuthor(authorName)
                .setOutputDir(baseProjectPath)
                .setServiceName("%sService")
                .setControllerName("%sController")
                .setActiveRecord(false)
                .setOpen(true)
                .setEnableCache(false)
                .setFileOverride(false);

        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(URL)
                .setUsername(USERNAME)
                .setPassword(PASSWORD)
                .setDriverName(DRIVER_NAME);

        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setChainModel(true)
                .setCapitalMode(true)
                .setEntityLombokModel(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setTablePrefix("t_")
                //修改替换成你需要的表名,多个表名传数组
                .setInclude(tables.toArray(new String[0]));

        PackageConfig packageConfig = new PackageConfig()
                .setParent(packageName)
                .setController("controller")
                .setService("service")
                .setEntity("model.entity")
                .setMapper("dao")
                .setXml("mapperxml");

        TemplateConfig templateConfig = new TemplateConfig();
//        templateConfig
//                .setServiceImpl(null)
//                .setService(null)
//                .setController(null)
//                .setMapper(null)
//                .setXml(null)
//        ;


        new AutoGenerator().setGlobalConfig(globalConfig)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(packageConfig)
                .setTemplate(templateConfig)
                .setTemplateEngine(new FreemarkerTemplateEngine())
                .execute();
    }

}

你可能感兴趣的:(java,开发语言,后端)