代码生成器步骤如下
废话不多说直接上代码
pom依赖
先导入mybatis plus的依赖,在导入模板引擎 依赖
<!--mybatis plus的依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.0</version>
</dependency>
<!---Velocity 模板引擎依赖->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.1</version>
</dependency>
接下来创建CodeGenerator代码生成器类
// 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class CodeGenerator {
/**
* <p>
* 读取控制台内容
* </p>
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");//生成文件的输出目录,默认值:D 盘根目录
gc.setAuthor("老刘");//开发人员,默认值:null
gc.setOpen(false);//是否打开输出目录,默认值:true
// gc.setSwagger2(true); 实体属性 Swagger2 注解
//去掉service接口开头的i
gc.setServiceName("%sService");
mpg.setGlobalConfig(gc);
// 数据源配置,通过该配置,指定需要生成代码的具体数据库
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://127.0.0.1:3306/你的数据库?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC");//驱动连接的URL
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");//驱动名称
dsc.setUsername("root");//数据库连接用户名
dsc.setPassword("数据库密码");//数据库连接密码
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(scanner("模块名"));//父包模块名
pc.setParent("com.bdqn.lkl");//设置父包 代码生成在这个包下
//根据你的需要设置以下包名,如果不需要可以使用默认的
/*pc.setController("controller");//Controller包名
pc.setEntity("entity");//Entity包名
pc.setService("service");//Service包名
pc.setServiceImpl("service.impl");//Service Impl包名
pc.setMapper("mapper");//Mapper包名
pc.setXml("mapper.xml");//Mapper XML包名*/
mpg.setPackageInfo(pc);//包名配置,通过该配置,指定生成代码的包路径
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker MyBatis-Plus 支持 Velocity(默认) 不设置用默认的
//String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
/*List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});*/
/*
cfg.setFileCreate(new IFileCreate() {
@Override
public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
// 判断自定义文件夹是否需要创建
checkDir("调用默认方法创建的目录");
return false;
}
});
*/
/*cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);*/
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
// 配置自定义输出模板
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// templateConfig.setEntity("templates/entity2.java");
// templateConfig.setService();
// templateConfig.setController();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略 驼峰命名
strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略, 未指定按照 naming 执行
//strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");//自定义继承的Entity类全称,带包名
strategy.setEntityLombokModel(true);//自定义基础的Entity类,公共字段 是否启用Lombok
strategy.setRestControllerStyle(true);//生成 @RestController 控制器 是否生成RestController
// 公共父类
//strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");//自定义继承的Controller类全称,带包名
// 写于父类中的公共字段
//strategy.setSuperEntityColumns("id");
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));//设置生成那些表 如果不设置就是生成全部
strategy.setControllerMappingHyphenStyle(true);//驼峰转连字符
strategy.setTablePrefix(pc.getModuleName() + "_");//表前缀
mpg.setStrategy(strategy);
//mpg.setTemplateEngine(new FreemarkerTemplateEngine());
// 执行生成
mpg.execute();
}
}