Mybatis-Plus代码生成器
程序员文章站
2022-05-07 21:36:22
代码生成器 代码生成器基于mybatis-plus持久化框架,可快速生成数据表对应的类: Controller.java 控制器 Service.java service接口 ServiceImpl.java service实现类 Mapper.java DAO Entity.java 实体 map ......
AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。
文档:https://mybatis.plus/guide/generator.html
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
代码
/**
* 演示例子,执行 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");
gc.setAuthor("javakf");
gc.setOpen(false);
// gc.setSwagger2(true); 实体属性 Swagger2 注解
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(
"jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(scanner("模块名"));
pc.setParent("cn.com.javakf.generator");
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
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/java/cn/com/javakf/generator/" + pc.getModuleName() + "/mapper/"
+ tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
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);
// 你自己的父类实体,没有就不用设置!
// strategy.setSuperEntityClass("com.baomidou.mybatisplus.samples.generator.common.BaseEntity");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
// 公共父类
// strategy.setSuperControllerClass("com.baomidou.mybatisplus.samples.generator.common.BaseController");
// 写于父类中的公共字段
strategy.setSuperEntityColumns("id");
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);
// 选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有!
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
测试
请输入模块名:
user
请输入表名,多个英文逗号分割:
tb_user
[main] [com.baomidou.mybatisplus.generator.AutoGenerator]-[DEBUG] ==========================准备生成文件...==========================
[main] [com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine]-[DEBUG] 创建目录: [E:\workspace\springboot\springboot_mybatis_plus_generator/src/main/java\cn\com\javakf\generator\user\entity]
[main] [com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine]-[DEBUG] 创建目录: [E:\workspace\springboot\springboot_mybatis_plus_generator/src/main/java\cn\com\javakf\generator\user\controller]
[main] [com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine]-[DEBUG] 创建目录: [E:\workspace\springboot\springboot_mybatis_plus_generator/src/main/java\cn\com\javakf\generator\user\service]
[main] [com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine]-[DEBUG] 创建目录: [E:\workspace\springboot\springboot_mybatis_plus_generator/src/main/java\cn\com\javakf\generator\user\mapper]
[main] [com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine]-[DEBUG] 创建目录: [E:\workspace\springboot\springboot_mybatis_plus_generator/src/main/java\cn\com\javakf\generator\user\service\impl]
[main] [com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine]-[DEBUG] 模板:/templates/mapper.xml.ftl; 文件:E:\workspace\springboot\springboot_mybatis_plus_generator/src/main/java/cn/com/javakf/generator/user/mapper/TbUserMapper.xml
[main] [com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine]-[DEBUG] 模板:/templates/entity.java.ftl; 文件:E:\workspace\springboot\springboot_mybatis_plus_generator/src/main/java\cn\com\javakf\generator\user\entity\TbUser.java
[main] [com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine]-[DEBUG] 模板:/templates/mapper.java.ftl; 文件:E:\workspace\springboot\springboot_mybatis_plus_generator/src/main/java\cn\com\javakf\generator\user\mapper\TbUserMapper.java
[main] [com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine]-[DEBUG] 模板:/templates/service.java.ftl; 文件:E:\workspace\springboot\springboot_mybatis_plus_generator/src/main/java\cn\com\javakf\generator\user\service\ITbUserService.java
[main] [com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine]-[DEBUG] 模板:/templates/serviceImpl.java.ftl; 文件:E:\workspace\springboot\springboot_mybatis_plus_generator/src/main/java\cn\com\javakf\generator\user\service\impl\TbUserServiceImpl.java
[main] [com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine]-[DEBUG] 模板:/templates/controller.java.ftl; 文件:E:\workspace\springboot\springboot_mybatis_plus_generator/src/main/java\cn\com\javakf\generator\user\controller\TbUserController.java
[main] [com.baomidou.mybatisplus.generator.AutoGenerator]-[DEBUG] ==========================文件生成完成!!!==========================
代码托管:springboot_mybatis_plus_generator
本文地址:https://blog.csdn.net/weixin_45730091/article/details/107362842
上一篇: 买的成品火锅料怎样炒