MybatisPlus代码生成器

1 :引入相关依赖jar包
MybatisPlus代码生成器_第1张图片
2:编写类

package com.yunfan.hr.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class MybatisPlusUtils {

    /**
     * 获取全部的表
     * @return
     */
    public static String[] getAllTables(){
        List list = new ArrayList();
        try{
            Connection connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/vhr?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8",
                    "root",
                    "123");
            DatabaseMetaData metaData = connection.getMetaData();
            ResultSet rs = metaData.getTables(null,null,null,new String[]{"TABLE"});
            while (rs.next()){
                list.add(rs.getString(3));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return (String[]) list.toArray(new String[]{});
    }

    /**
     * 连接数据库
     * @param autoGenerator
     */
    private static void buildDb(AutoGenerator autoGenerator){
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/vhr?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("123");
        autoGenerator.setDataSource(dataSourceConfig);

    }

    public static void generateMysql(String[] includeTables,String[] excludeTables,Boolean db1){
        AutoGenerator autoGenerator = new AutoGenerator();
        //数据库配置
        buildDb(autoGenerator);
        //数据表配置
        StrategyConfig strategyConfig = new StrategyConfig();
        //骆驼命名
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setInclude(includeTables);
        strategyConfig.setExclude(excludeTables);
        strategyConfig.setTablePrefix("");
        autoGenerator.setStrategy(strategyConfig);

        //全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setAuthor("Lixuan");
        globalConfig.setIdType(IdType.INPUT);
        globalConfig.setDateType(DateType.ONLY_DATE);
        globalConfig.setActiveRecord(true);
        globalConfig.setOutputDir("D:/Project/Hr/hr1/src/main/java");
        globalConfig.setMapperName("%sMapper");
        globalConfig.setServiceName("%sService");
        globalConfig.setServiceImplName("%sServiceImpl");
        globalConfig.setXmlName("%sMapper");
        globalConfig.setSwagger2(false);
        globalConfig.setFileOverride(true);
        globalConfig.setBaseColumnList(false);
        globalConfig.setBaseResultMap(false);
        globalConfig.setEnableCache(false);
        autoGenerator.setGlobalConfig(globalConfig);

        //包名配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("");
        packageConfig.setEntity("com.yunfan.hr.entity");
        packageConfig.setMapper("com.yunfan.hr.dao");
        packageConfig.setXml("mapper");
        packageConfig.setService("com.yunfan.hr.service");
        packageConfig.setServiceImpl("com.yunfan.hr.service.impl");
        packageConfig.setController("com.yunfan.hr.controller");
        autoGenerator.setPackageInfo(packageConfig);
        //设置模板
        autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());
        //执行
        autoGenerator.execute();
    }

    public static void main(String[] args) {
        generateMysql(getAllTables(),null,false);
    }





}


你可能感兴趣的:(Java,Mybatisplus,SpringBoot)