SpringBoot 项目中 使用Mybatis-Plus自动生成代码

添加依赖

 
     com.baomidou
     mybatis-plus-generator
     3.2.0
 

 
 
      org.apache.velocity
      velocity-engine-core
      2.0
 

 代码

package com.example.mybatisplus02.generator;

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;


public class CodeGenerator {

    public static void main(String[] args) {
        String packageName = "com.example.mybatisplus02";
        boolean serviceNameStartWithI = false;//auth -> UserService, 设置成true: auth -> IUserService
        generateByTables(serviceNameStartWithI, packageName, "author"
                , "address_info");

        System.out.println("completed...");
    }

    /**
     * @param serviceNameStartWithI
     * @param packageName   包名
     * @param author  作者
     * @param tableNames 表名
     */
    private static void generateByTables(boolean serviceNameStartWithI, String packageName
            , String author, String... tableNames) {

        //数据源配置
        String dbUrl = "jdbc:mysql://127.0.0.1:3306/mpdemo?serverTimezone=UTC";
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                .setUsername("root")
                .setPassword("123456")
                .setDriverName("com.mysql.cj.jdbc.Driver");

        //策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true)
                .setEntityLombokModel(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setInclude(tableNames);//修改替换成你需要的表名,多个表名传数组

        //全局配置
        GlobalConfig config = new GlobalConfig();
        config.setActiveRecord(false)
                .setAuthor(author)
                .setOutputDir("D:\\Project\\Demos\\JAVA\\mybatisplus02\\src\\main\\java\\")
                .setFileOverride(true)
                .setEnableCache(false);
        TemplateConfig tc = new TemplateConfig();
        tc.setService(null);
        tc.setServiceImpl(null);
        tc.setController(null);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(packageName);
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setXml("xml");

        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setTemplate(tc)
                .setPackageInfo(pc)

                .execute();
    }
}

 

你可能感兴趣的:(【007】JAVA)