自己用的MybatisPlus代碼生成器


// 1. 創建代碼生成器對象
        AutoGenerator mpg = new AutoGenerator();

        // 2. 設置數據源
        DataSourceConfig dataSource = new DataSourceConfig();
        dataSource.setUrl("jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT");
        dataSource.setUsername("root");
        dataSource.setPassword("");
        dataSource.setDriverName("com.mysql.cj.jdbc.Driver");
        mpg.setDataSource(dataSource);

        // 4. 配置包
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.itcast.base"); // 設置父包名
        pc.setModuleName("edu");  // 設置父包模塊名
        pc.setEntity("entity");
        pc.setService("service");
//        pc.setServiceImpl("service.impl");
        pc.setController("controller");
        pc.setMapper("mapper");
//        pc.setXml("mapper.xml");
        mpg.setPackageInfo(pc);

        // 3. 數據表配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setEntityLombokModel(true); // 設置Lombok的使用
        strategy.setEntityBooleanColumnRemoveIsPrefix(true); // 設置表字段是否移除is前綴
        strategy.setRestControllerStyle(true); // 生成RestController控制器
        strategy.setControllerMappingHyphenStyle(true); // url駝峰連接字符
        strategy.setEntityTableFieldAnnotationEnable(true);  // 是否生成實體時,生成字段注解
        strategy.setVersionFieldName("version");  // 樂觀鎖設置
        strategy.setTablePrefix(pc.getModuleName() + "_");
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setLogicDeleteFieldName("is_deleted");  // 邏輯刪除屬性名稱
        // 設置自動填充字段
        strategy.setTableFillList(Arrays.asList(new TableFill("create_time", FieldFill.INSERT),
                new TableFill("update_time", FieldFill.INSERT_UPDATE)));
        mpg.setStrategy(strategy);




        // 5. 配置模板引擎
        String templatePath = "/templates/mapper.xml.ftl";
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 6. 全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(System.getProperty("user.dir") + "/src/main/java"); // 獲取當前項目路徑
        globalConfig.setFileOverride(true); // 覆蓋文件
         globalConfig.setOpen(false); // 打開目錄
        globalConfig.setAuthor("小新");
        //globalConfig.setSwagger2(true); // 開啟Swagger2模式
        globalConfig.setEntityName("%s"); // 提示類,命名方式
        globalConfig.setMapperName("%sMapper");
//        globalConfig.setXmlName("%sMapper.xml");
        globalConfig.setServiceName("%sService");
        globalConfig.setServiceImplName("%sServiceImpl");
        globalConfig.setControllerName("%sControlller");
        globalConfig.setDateType(DateType.ONLY_DATE);

        mpg.setTemplateEngine(new FreemarkerTemplateEngine()); // 指定使用的模板引擎
        mpg.setGlobalConfig(globalConfig);

        mpg.execute();// 開始生成

        System.out.println(pc.getParent());


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM