package com.heketai.website;


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

import java.util.ArrayList;

public class MyCode {
        public static void main(String[] args) {
            //需要构建一个 代码自动生成器 对象
            // 代码生成器
            AutoGenerator mpg = new AutoGenerator();
            //配置策略

            //1、全局配置
            GlobalConfig gc = new GlobalConfig();
            String projectPath = System.getProperty("user.dir");
            gc.setOutputDir(projectPath + "/src/main/java");
            gc.setAuthor("ChanV");
            gc.setOpen(false);
            //是否覆盖
            gc.setFileOverride(false);
            //去Service的I前缀
            gc.setServiceName("%sService");
            gc.setIdType(IdType.ID_WORKER);
            mpg.setGlobalConfig(gc);

            //2、设置数据源
            DataSourceConfig dsc = new DataSourceConfig();
            dsc.setUrl("jdbc:mysql://47.56.160.5:3306/hotjt?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=PRC&useSSL=false");
            dsc.setDriverName("com.mysql.jdbc.Driver");
            dsc.setUsername("root");
            dsc.setPassword("hkt2019abc_123");
            dsc.setDbType(DbType.MYSQL);
            mpg.setDataSource(dsc);

            //3、包的配置
            PackageConfig pc = new PackageConfig();
            pc.setModuleName("blog");
            pc.setParent("com.chanv");
            pc.setEntity("pojo");
            pc.setMapper("mapper");
            pc.setService("service");
            pc.setController("controller");
            mpg.setPackageInfo(pc);

            //4、策略配置
            StrategyConfig strategy = new StrategyConfig();
            strategy.setInclude("job_search");    //设置要映射的表名
            strategy.setNaming(NamingStrategy.underline_to_camel);
            strategy.setColumnNaming(NamingStrategy.underline_to_camel);
            strategy.setEntityLombokModel(true);    //自动lombok
            strategy.setLogicDeleteFieldName("deleted");
            //自动填充配置
            TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
            TableFill updateTime = new TableFill("update_time", FieldFill.UPDATE);
            ArrayList<TableFill> tableFills = new ArrayList<>();
            tableFills.add(createTime);
            tableFills.add(updateTime);
            strategy.setTableFillList(tableFills);
            //乐观锁
            strategy.setVersionFieldName("version");
            strategy.setRestControllerStyle(true);
            strategy.setControllerMappingHyphenStyle(true);     //localhost:8080/hello_id_2
            mpg.setStrategy(strategy);

            mpg.execute();  //执行代码构造器
        }
    }
    
