@Test
    public void test1(){
        GlobalConfig globalConfig = new GlobalConfig();

        globalConfig.setOutputDir(System.getProperty("user.dir") + "/src/main/java");
        globalConfig.setAuthor("ZhengQinfeng");
        globalConfig.setOpen(false);
        globalConfig.setActiveRecord(true);
        globalConfig.setFileOverride(true);
//        globalConfig.setServiceName("%sService");
        globalConfig.setIdType(IdType.AUTO).setBaseResultMap(true).setBaseColumnList(true);



        //2. 数据源配置
        DataSourceConfig dsConfig = new DataSourceConfig();
        dsConfig.setDbType(DbType.MYSQL)  // 设置数据库类型
                .setDriverName("com.mysql.jdbc.Driver")
                .setUrl("jdbc:mysql://192.168.79.199:3306/data")
                .setUsername("root")
                .setPassword("1328174");

        //3. 策略配置
        StrategyConfig stConfig = new StrategyConfig();
        stConfig.setCapitalMode(true) //全局大写命名
                .setEntityLombokModel(true)
                .setTablePrefix("tb_")
                .setNaming(NamingStrategy.underline_to_camel) // 数据库表映射到实体的命名策略
                .setInclude("tb_goods");  // 生成的表

        //4. 包名策略配置
        PackageConfig pkConfig = new PackageConfig();
        pkConfig.setParent("qinfeng.zheng.mobb.goods")
                .setMapper("mapper")
                .setService("service")
                .setController("controller")
                .setEntity("beans")
                .setXml("mapper");

        //5. 整合配置
        AutoGenerator ag = new AutoGenerator();

        ag.setGlobalConfig(globalConfig)
                .setDataSource(dsConfig)
                .setStrategy(stConfig)
                .setPackageInfo(pkConfig);

        //6. 执行
        ag.execute();
    }

 

 ==========================下面来个更完一点的================================================

1. 添加依赖

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.2</version>
        </dependency>

2.  代码 

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
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.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

/**
 * @Author ZhengQinfeng
 * @Date 2020/7/18 15:58
 * @dec
 */
public class AutoGeneratorCode {
    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 1.全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/app-kaile/src/main/java");
        gc.setAuthor("郑钦锋");
        gc.setOpen(false);
        gc.setFileOverride(false);
        gc.setDateType(DateType.ONLY_DATE);
        mpg.setGlobalConfig(gc);

        // 2.数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://192.168.79.199:3306/appBB?useUnicode=true&useSSL=false&characterEncoding=utf8");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("1328174");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("qinfeng.zheng.goods");
        pc.setController("controller");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        mpg.setPackageInfo(pc);


        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("tb_goods_type","tb_goods","tb_comments","tb_image","tb_coupon");  // 设置要映射的表名
        strategy.setTablePrefix("tb_");
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setLogicDeleteFieldName("deleted"); // 逻辑删除字段
        // 自动填充配置
//        TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
//
//        TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);
//
//        ArrayList<TableFill> tableFillList = new ArrayList<>();
//
//        tableFillList.add(createTime);
//        tableFillList.add(updateTime);
//
//        strategy.setTableFillList(tableFillList);
        strategy.setControllerMappingHyphenStyle(true);  // http://localhost:8080/hello_id_2


        mpg.setStrategy(strategy);
        mpg.execute();

    }
}

 

posted on 2020-02-21 15:26  显示账号  阅读(1890)  评论(0编辑  收藏  举报