Mybatis-Plus自动生成代码的CodeGenerator代码
官方地址:
Mybatis-Plus:https://mp.baomidou.com/guide/generator.html
pom中导入mybatis plus的jar包,因为后面会涉及到代码生成,所以我们还需要导入页面模板引擎,这里我们用的是freemarker。
pom.xml导入以下依赖

<!-- 加载mybatis-plus jar包 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!--mybatis-plus反向生成-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<!-- 模板引擎 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency> <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
依赖
CodeGenarator代码

1 import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
2 import com.baomidou.mybatisplus.core.toolkit.StringPool;
3 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
4 import com.baomidou.mybatisplus.generator.AutoGenerator;
5 import com.baomidou.mybatisplus.generator.InjectionConfig;
6 import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
7 import com.baomidou.mybatisplus.generator.config.FileOutConfig;
8 import com.baomidou.mybatisplus.generator.config.GlobalConfig;
9 import com.baomidou.mybatisplus.generator.config.PackageConfig;
10 import com.baomidou.mybatisplus.generator.config.StrategyConfig;
11 import com.baomidou.mybatisplus.generator.config.TemplateConfig;
12 import com.baomidou.mybatisplus.generator.config.po.TableInfo;
13 import com.baomidou.mybatisplus.generator.config.rules.DateType;
14 import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
15 import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
16
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Scanner;
20
21
22 public class CodeGenerator {
23 /**
24 * <p>
25 * 读取控制台内容
26 * </p>
27 */
28 public static String scanner(String tip) {
29 Scanner scanner = new Scanner(System.in);
30 StringBuilder help = new StringBuilder();
31 help.append("请输入" + tip + ":");
32 System.out.println(help.toString());
33 if (scanner.hasNext()) {
34 String ipt = scanner.next();
35 if (StringUtils.isNotBlank(ipt)) {
36 return ipt;
37 }
38 }
39 throw new MybatisPlusException("请输入正确的" + tip + "!");
40 }
41
42 public static void main(String[] args) {
43 // 代码生成器
44 AutoGenerator mpg = new AutoGenerator();
45 // 全局配置
46 String module = "workticket";
47 GlobalConfig gc = new GlobalConfig();
48 String projectPath = System.getProperty("user.dir") + "/" + module;
49 gc.setOutputDir(projectPath + "/src/main/java");
50 //作者
51 gc.setAuthor(scanner("作者名"));
52 //打开输出目录
53 gc.setOpen(false);
54 // //xml开启 BaseResultMap
55 // gc.setBaseResultMap(true);
56 // //xml 开启BaseColumnList
57 // gc.setBaseColumnList(true);
58 // 实体属性 Swagger2 注解
59 gc.setSwagger2(true);
60 gc.setDateType(DateType.ONLY_DATE);
61 mpg.setGlobalConfig(gc);
62
63
64 // 数据源配置
65 DataSourceConfig dsc = new DataSourceConfig();
66 // dsc.setUrl("jdbc:mysql://localhost:3307/vueblog?" +
67 // "useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia" +
68 // "/Shanghai");
69 // dsc.setDriverName("com.mysql.cj.jdbc.Driver");
70 // dsc.setUsername("root");
71 // dsc.setPassword("001314");
72 // mpg.setDataSource(dsc);
73 dsc.setUrl("jdbc:postgresql://localhost:5432/test");
74 dsc.setSchemaName("public");
75 dsc.setDriverName("org.postgresql.Driver");
76 dsc.setUsername("postgres");
77 dsc.setPassword("123456");
78 mpg.setDataSource(dsc);
79
80 // 包配置
81 PackageConfig pc = new PackageConfig();
82 pc.setModuleName(module);
83 pc.setParent("com.demo.micro");
84 mpg.setPackageInfo(pc);
85
86 // 自定义配置
87 InjectionConfig cfg = new InjectionConfig() {
88 @Override
89 public void initMap() {
90 // to do nothing
91 }
92 };
93 // 如果模板引擎是 freemarker
94 String templatePath = "/templates/mapper.xml.ftl";
95 // 如果模板引擎是 velocity
96 // String templatePath = "/templates/mapper.xml.vm";
97 // 自定义输出配置
98 List<FileOutConfig> focList = new ArrayList<>();
99 // 自定义配置会被优先输出
100 focList.add(new FileOutConfig(templatePath) {
101 @Override
102 public String outputFile(TableInfo tableInfo) {
103 // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
104 return projectPath + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
105 }
106 });
107 cfg.setFileOutConfigList(focList);
108 mpg.setCfg(cfg);
109 // 配置模板
110 TemplateConfig templateConfig = new TemplateConfig();
111 //执行main方法,在控制台直接输出表名,多个表名用,隔开结果
112 templateConfig.setXml(null);
113 mpg.setTemplate(templateConfig);
114 // 策略配置
115 StrategyConfig strategy = new StrategyConfig();
116 //数据库表映射到实体的命名策略
117 strategy.setNaming(NamingStrategy.underline_to_camel);
118 //数据库表字段映射到实体的命名策略
119 strategy.setColumnNaming(NamingStrategy.underline_to_camel);
120 //lombok模型
121 strategy.setEntityLombokModel(true);
122 //生成 @RestController 控制器
123 strategy.setRestControllerStyle(true);
124 strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
125 strategy.setControllerMappingHyphenStyle(true);
126 //表前缀
127 strategy.setTablePrefix("");
128 mpg.setStrategy(strategy);
129 mpg.setTemplateEngine(new FreemarkerTemplateEngine());
130 mpg.execute();
131 }
132 }
CodeGenerator
参考:https://blog.csdn.net/qq_44723773/article/details/118862972
Mybatis-Plus自动生成代码的CodeGenerator代码的更多相关文章
- mybatis generator 自动生成dao层映射代码
资源: doc url :http://www.mybatis.org/generator/ download:https://github.com/mybatis/generator/release ...
- 使用Mybatis Generator自动生成Mybatis相关代码
本文将简要介绍怎样利用Mybatis Generator自动生成Mybatis的相关代码: 一.构建一个环境: 1. 首先创建一个表: CREATE TABLE pet (name VARCHAR(2 ...
- SpringBoot 添加mybatis generator 自动生成代码插件
自动生成数据层代码,提高开发效率 1.pom添加插件,并指定配置文件路径 <!-- mybatis generator 自动生成代码插件 --> <plugin> <gr ...
- idea中mybatis generator自动生成代码配置 数据库是sqlserver
好长时间没有写博客了,最近公司要用java语言,开始学习java,属于初学者,今天主要记录一下mybatis generator自动生成代码,首先在如下图的目录中新建两个文件,如下图 generato ...
- SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件
原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...
- IDEA Maven Mybatis generator 自动生成代码
IDEA Maven Mybatis generator 自动生成代码 一.安装配置maven以及在Idea中配置maven 安装过程步骤可以看上面的博文,里面介绍得很详细. 二.建数据表 DROP ...
- IDEA Maven Mybatis generator 自动生成代码(实例讲解)(转)
IDEA Maven Mybatis generator 自动生成代码(实例讲解) MyBatis Generator • 简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的 ...
- (转)MyBatis框架的学习(七)——MyBatis逆向工程自动生成代码
http://blog.csdn.net/yerenyuan_pku/article/details/71909325 什么是逆向工程 MyBatis的一个主要的特点就是需要程序员自己编写sql,那么 ...
- 使用MyBatis Generator自动生成MyBatis的代码
这两天需要用到MyBatis的代码自动生成的功能,由于MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所以可利用MyBatis生 ...
- 使用mybatis插件自动生成代码以及问题处理
1.pom.xml中加入依赖插件 <!-- mybatis generator 自动生成代码插件 --> <plugin> <groupId>org.mybatis ...
随机推荐
- [转帖]Kafka 核心技术与实战学习笔记(八)kafka集群参数配置(下)
一.Topic级别参数 Topic的优先级: 如果同时设置Topic级别参数和全局Broker参数,那么Topic级别优先 消息保存方面: retention.ms:规定Topic消息保存时长.默认是 ...
- [转帖]xsos - Summarize system info from sosreports
https://github.com/ryran/rsar I'M LOOKING FOR RSAR SCREEN SHOTS INTRO INSTALLATION EXAMPLES IN ACTIO ...
- vue组件上绑定原生事件
将原生事件绑定在组件上 .native 修饰符: 子组件 <template> <div class="demo"> <h2>我是子组件< ...
- Ant Design Vue 单文件上传Upload
单文件上传 <a-upload name="file" :beforeUpload="beforeUpload" :multiple="fals ...
- 手写一个Promise完成resolve 和 reject状态的改变和修改属性
1.手写 Promise 1 创建一个文件 Promise.js:内容 function Promise(){ } 2 引入 Promise.js 这个文件 <script src=" ...
- 【发现一个问题】VictoriaMetrics中,所有vmstorage在中午12:00切换索引,导致所有vm-insert发生oom
请看我提的issue: https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2464 bug: vm v1.70.0, all vmst ...
- .NET 6 使用 System.Drawing.Common 出现 The type initializer for ‘Gdip’ threw an exception 异常的解决办法
出现问题的原因 在Linux环境部署.NET Core程序时,如果要到System.Drawing.Common引用会出现该问题,目前大量的第三方组件使用该Windows专用库,尤其是涉及图片处理.W ...
- C/C++ Qt 监控文件状态变化
实现对特定文件的监控,Qt中提供了QFileSystemWatcher调用这个接口可以快速实现监控功能,当有文件发生变化是自动触发并输出文件具体信息. filesystem.h #ifndef FIL ...
- 外部文件使用django的models
#外部文件使用django的models,需要配置django环境 import os if __name__ == '__main__': os.environ.setdefault("D ...
- JAVA入门学习之GUI编程思想——day01
GUI编程 什么是GUI???图形化编程 组件 窗口 弹窗 按钮 文本框 图片 事件 ..... GUI的核心:AWT Swing GUI编程的缺陷: 1.界面不美观 2.需要jre环境 虽然 ...