【Mybatis-Plus】06 代码生成器 CodeGenerator
导入生成器需要的依赖坐标:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.2.0</version>
</dependency> <dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.1</version>
</dependency> <dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.29</version>
</dependency> <dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
新建了一个普通Maven空项目:

创建我们的代码生成器程序:
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.apache.commons.lang3.StringUtils; import java.util.Scanner; /**
* @author DaiZhiZhou
* @file MP-Spring
* @create 2020-08-06 9:11
*/
public class CodeGenerator { /**
* <p>
* 读取控制台内容
* </p>
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入").append(tip).append(":");
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 mybatisPlusCodeGenerator = new AutoGenerator(); // 全局配置
GlobalConfig gc = new GlobalConfig(); // 先得到当前工程目录
String projectPath = System.getProperty("user.dir");
// 是maven项目的结构,就是工程目录 + /src/main/java
gc.setOutputDir(projectPath + "/src/main/java"); //gc.setOutputDir("D:\\workspace-sts\\0520adv\\02_mp_springboot/src/main/java"); // 设置生成文件的作者信息
gc.setAuthor("Echo42"); //当代码生成完成之后是否打开代码所在的文件夹
gc.setOpen(true); // gc.setSwagger2(true); 实体属性 Swagger2 注解
//gc.setServiceName("%sService"); // 将上述的全局配置注入
mybatisPlusCodeGenerator.setGlobalConfig(gc); // 数据源配置
DataSourceConfig dataSourceConfiguration = new DataSourceConfig(); dataSourceConfiguration.setUrl("jdbc:mysql://localhost:3306/oa?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC"); // dataSourceConfiguration.setSchemaName("public");
dataSourceConfiguration.setDriverName("com.mysql.cj.jdbc.Driver");
dataSourceConfiguration.setUsername("root");
dataSourceConfiguration.setPassword("123456"); mybatisPlusCodeGenerator.setDataSource(dataSourceConfiguration); // 包配置
PackageConfig pc = new PackageConfig(); // 设置父级包名
pc.setParent("cn.echo42");//controller entity service service.impl pc.setModuleName(scanner("模块名"));
//pc.setModuleName("sys"); // 实体类名称
pc.setEntity("domain"); // mapper包名称
pc.setMapper("mapper"); // mapper对应的映射器xml
pc.setXml("mapper.xml"); // 业务包层名称
pc.setService("service"); // 业务接口的实现类包
pc.setServiceImpl("service.impl"); // 控制器包名称
pc.setController("controller"); // 装填包信息对象
mybatisPlusCodeGenerator.setPackageInfo(pc); // 策略配置
StrategyConfig strategy = new StrategyConfig(); //设置字段和表名的是否把下划线完成驼峰命名规则
strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); //设置生成的实体类继承的父类
//strategy.setSuperEntityClass("com.sxt.BaseEntity"); //是否启动lombok
strategy.setEntityLombokModel(true); //是否生成resetController
strategy.setRestControllerStyle(true); // 公共父类
//strategy.setSuperControllerClass("com.sxt.BaseController"); // 写于父类中的公共字段
//strategy.setSuperEntityColumns("person_id","person_name"); //要设置生成哪些表 如果不设置就是生成所有的表
//strategy.setInclude(scanner("表名,多个英文逗号分割").split(",")); strategy.setControllerMappingHyphenStyle(true); //strategy.setTablePrefix(pc.getModuleName() + "_");
strategy.setTablePrefix("sys_"); mybatisPlusCodeGenerator.setStrategy(strategy); mybatisPlusCodeGenerator.execute();
}
}
点击运行:

然后生成的这个目录结构:

基础四层就帮我们自动创建完成了:

controller报错是因为没有加入SpringMVC:

如果是SSMP的开发模式,编写结构文件使用这个能省力很多,
但是对结构不清楚的情况还是多人肉手写熟悉熟悉
【Mybatis-Plus】06 代码生成器 CodeGenerator的更多相关文章
- 【MyBatis学习06】_parameter:解决There is no getter for property named in class java.lang.String
我们知道在mybatis的映射中传参数,只能传入一个.通过#{参数名} 即可获取传入的值. Mapper接口文件: public int delete(int id) throws Exception ...
- 【MyBatis学习06】输入映射和输出映射
在前面几篇博文的例子中也可以看到mybatis中输入映射和输出映射的身影,但是没有系统的总结一下,这篇博客主要对这两个东东做一个总结.我们知道mybatis中输入映射和输出映射可以是基本数据类型.ha ...
- MyBatis学习06(动态SQL和缓存)
10.动态SQL 10.1 什么是动态SQL 动态SQL指的是根据不同的查询条件 , 生成不同的Sql语句. 官网描述: MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或 ...
- MyBatis Generator 详解 专题
idea中有plugin可提高效率: http://www.henryxi.com/use-idea-mybatis-plugin-generate-mapper-files eg: <?xml ...
- MyBatis Generator 详解
MyBatis Generator中文文档 MyBatis Generator中文文档地址:http://mbg.cndocs.tk/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中 ...
- MyBatis Generator 详解 【转来纯为备忘】
版权声明:版权归博主所有,转载请带上本文链接!联系方式:abel533@gmail.com 目录(?)[+] MyBatis Generator中文文档 运行MyBatis Generator X ...
- mybatis Generator配置文件详解
这里按照配置的顺序对配置逐个讲解,更细的内容可以配合中文文档参照. 1. 配置文件头 <?xml version="1.0" encoding="UTF-8&quo ...
- 转载:mybatis自动生成
MyBatis Generator中文文档 MyBatis Generator中文文档地址: http://generator.sturgeon.mopaas.com/ 该中文文档由于尽可能和原文内容 ...
- MyBatis Generator中文文档
MyBatis Generator中文文档 MyBatis Generator中文文档地址: http://mbg.cndocs.tk/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看 ...
- MyBatis Generator 详解(转)
MyBatis Generator中文文档 MyBatis Generator中文文档地址:http://mbg.cndocs.tk/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中 ...
随机推荐
- codemirror-editor-vue3 编辑器只要换行保存就会有小红点
先看示例 找了篇博客写的贼详细 https://blog.csdn.net/xujingyiss/article/details/118992763 只要设置这二个 let conten ...
- Yapi部署到kubernetes(k8s)--Azure AKS
背景 找来找去都没找到简单方便的部署yapi到k8s的教程,就自己写了个: 本教程部署的版本是: 1.10.2 部署步骤 克隆仓库: git clone https://github.com/gebi ...
- Vite-Wechat网页聊天室|vite5.x+vue3+pinia+element-plus仿微信客户端
基于Vue3+Pinia+ElementPlus仿微信网页聊天模板Vite5-Vue3-Wechat. vite-wechat使用最新前端技术vite5+vue3+vue-router@4+pinia ...
- 基于 Linux 2.6的 硬中断 / 软中断的原理以及实现
Author:zhangskd @ csdn blog 概述 从本质上来讲,中断是一种电信号,当设备有某种事件发生时,它就会产生中断,通过总线把电信号发送给中断控制器. 如果中断的线是激活的,中断控制 ...
- TI AM64x工业核心板硬件说明书(双核ARM Cortex-A53 + 单/四核Cortex-R5F + 单核Cortex-M4F,主频1GHz)
1 硬件资源 创龙科技SOM-TL64x是一款基于TI Sitara系列AM64x双核ARM Cortex-A53 + 单/四核Cortex-R5F + 单核Cortex-M4F设计 ...
- AT_abc317_f 题解
调了一小时结果发现爆 long long 了. 考虑数位 dp,具体来说,设计状态 \(dp_{i,r_1,r_2,r_3,mx_1,mx_2,mx3_,c_1,c_2,c_3}\) 表示当前考虑到第 ...
- 傻瓜式Java操作MySQL数据库备份(使用mysqldump命令)
傻瓜式Java操作MySQL数据库备份(使用mysqldump命令) 注释都是由chatGPT生成,有什么问题可以评论交流 @Value("${backup.sql.database}&qu ...
- HTTP 和 HTTPS,为什么HTTPS安全?
HTTP协议通常承载与 TCP协议之上,在HTTP和TCP之间添加一个安全协议层(SSL或TSL),这个时候,就成了我们常说的HTTPS 默认HTTP的端口号为80,HTTPS的端口号为443 因为网 ...
- v-if 和 v-show 有什么区别?
v-if 是真正的条件渲染,会控制这个 DOM 节点的存在与否.因为它会确保在切换过程中条件块内的事件监听器和子组件适当地被销毁和重建:也是惰性的:如果在初始渲染时条件为假,则什么也不做--直到条件第 ...
- MySQL 获取所有表名、所有表结构
获取所有表名 SELECT A.TABLE_SCHEMA '数据库', A.TABLE_NAME '表名', A.TABLE_ROWS '表记录行数', A.CREATE_TIME '创表时间', A ...