java代码生成器按照模板_MybatisPlus根据模板生成器代码

packagecon.cyb.build;importcom.baomidou.mybatisplus.annotation.DbType;importcom.baomidou.mybatisplus.annotation.IdType;importcom.baomidou.mybatisplus.core.toolkit.StringPool;importcom.baomidou.mybatisplus.generator.AutoGenerator;importcom.baomidou.mybatisplus.generator.InjectionConfig;import com.baomidou.mybatisplus.generator.config.*;importcom.baomidou.mybatisplus.generator.config.po.TableInfo;importcom.baomidou.mybatisplus.generator.config.rules.NamingStrategy;importcom.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;importjava.util.ArrayList;importjava.util.List;/*** @ClassName:Generator

* @Description:代码自动生成器

* @Author:chenyb

* @Date:2020/9/20 8:10 下午

* @Versiion:1.0*/

public classGenerator {public static voidmain(String[] args) {//生成地址 ://System.getProperty("user.dir") == 得到当前项目的实际地址

String outputDir = System.getProperty("user.dir") + "/src/main/java";//String outputDir = "C://Users/VULCAN/Desktop/new";//表名, 注意大小写

String[] tableNames = new String[]{"chapter"};//数据库地址

String url = "jdbc:mysql://localhost:3306/online_ybclass?useUnicode=true&characterEncoding=utf8";//用户名

String userName = "root";//密码

String password = "root";//父包路径

String parentPackage = "con.cyb";//需要去掉的表名前缀

String prefixTable = "Test_";

generate(outputDir, tableNames, url, userName, password, parentPackage, prefixTable);

}/***@paramoutputDir 生成地址

*@paramtableNames 表名

*@paramurl 数据库地址

*@paramuserName 用户名

*@parampassword 密码

*@paramparentPackage 父包路径

*@paramprefixTable 需要去掉的表名前缀*/

public static voidgenerate(String outputDir, String[] tableNames, String url, String userName,

String password, String parentPackage, String prefixTable) {//=============== 全局配置 ==================

GlobalConfig gc = newGlobalConfig();

gc.setOutputDir(outputDir)

.setActiveRecord(true) //是否支持 AR, 实体类只需继承 Model 类即可进行强大的 CRUD 操作

.setAuthor("GrassPrince") //设置作者名字

.setFileOverride(true) //文件覆盖(全新文件)

.setIdType(IdType.AUTO) //主键策略

.setBaseResultMap(true) //SQL 映射文件

.setBaseColumnList(true) //SQL 片段

.setServiceName("%sService") //service的名字

.setOpen(false);//================= 数据源配置 ===============

DataSourceConfig dsc = newDataSourceConfig();

dsc.setDbType(DbType.MYSQL)

.setDriverName("com.mysql.cj.jdbc.Driver");

dsc.setUrl(url)

.setUsername(userName)

.setPassword(password);//================= 包配置 ===================

PackageConfig pc = newPackageConfig();

pc.setParent(parentPackage)//配置父包路径//.setModuleName("base")//配置业务包路径

.setMapper("mapper")

.setEntity("entity")

.setService("service")//.setServiceImpl("service.impl");//会自动生成 impl,可以不设定

.setController("controller");//================== 自定义配置 =================

InjectionConfig cfg = newInjectionConfig() {

@Overridepublic voidinitMap() {//to do nothing

}

};

List focList = new ArrayList<>();//调整 xml 生成目录演示

focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {

@OverridepublicString outputFile(TableInfo tableInfo) {//自定义输入文件名称

return System.getProperty("user.dir") + "/src/main/resources/mybatis/"

+ tableInfo.getEntityName() + "Mapper" +StringPool.DOT_XML;

}

});

cfg.setFileOutConfigList(focList);//=================== 策略配置 ==================

StrategyConfig strategy = newStrategyConfig();

strategy.setNaming(NamingStrategy.underline_to_camel)//表名命名: underline_to_camel 底线变驼峰

.setColumnNaming(NamingStrategy.underline_to_camel) //字段命名: underline_to_camel 底线变驼峰

.setInclude(tableNames) //需要生成的 表名

.setCapitalMode(true) //全局大写命名 ORACLE 注意

.setTablePrefix(prefixTable) //去掉 表的前缀//.setFieldPrefix(pc.getModuleName() + "_")//去掉字段前缀//.setSuperEntityClass("com.maoxs.pojo")//继承类//.setSuperControllerClass("com.maoxs.controller")//继承类//.setSuperEntityColumns("id")//设置超级超级列//.setEntityLombokModel(true)//是否加入lombok

.setControllerMappingHyphenStyle(true); //设置controller映射联字符//================== 自定义模板配置: 默认配置位置 mybatis-plus/src/main/resources/templates ======================//放置自己项目的 src/main/resources/templates 目录下, 默认名称一下可以不配置,也可以自定义模板名称

TemplateConfig tc = newTemplateConfig();

tc.setXml(null) //设置生成xml的模板

.setEntity("/templates/entity.java") //设置生成entity的模板

.setMapper("/templates/mapper.java") //设置生成mapper的模板

.setController("/templates/controller.java") //设置生成service的模板

.setService("/templates/service.java") //设置生成serviceImpl的模板

.setServiceImpl("/templates/serviceImpl.java"); //设置生成controller的模板//==================== 生成配置 ===================

AutoGenerator mpg = newAutoGenerator();

mpg.setCfg(cfg)

.setTemplate(tc)

.setGlobalConfig(gc)

.setDataSource(dsc)

.setPackageInfo(pc)

.setStrategy(strategy)

.setTemplateEngine(new FreemarkerTemplateEngine()); //选择 freemarker引擎,注意 pom 依赖必须有!

mpg.execute();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值