Java:MyBatis-Plus-AutoGenerator代码生成器
【摘要】 文档:https://baomidou.com/guide/generator.html 配置参数:https://baomidou.com/config/generator-config.html
依赖
<!--代码生成器-->
<dependency>
<groupId>com.baomidou</groupId&g...
文档:https://baomidou.com/guide/generator.html
配置参数:https://baomidou.com/config/generator-config.html
依赖
<!--代码生成器-->
<dependency>
<groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.4.0</version>
</dependency>
<!--模板引擎 依赖-->
<dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.2</version>
</dependency>
<!-- 数据库驱动 -->
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version>
</dependency>
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope>
</dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
最简单的代码示例
package com.demo.mybatis;
import com.baomidou.mybatisplus.annotation.FieldFill;
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.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.ArrayList;
/**
* 自动生成类
**/
public class CodeGenerator { /** * 必要的配置 */ // 生成代码的表名 private static final String TABLE_NAME = "table_person"; // 数据库相关配置 private static final String JDBC_URL = "jdbc:mysql://localhost:3306/data?useUnicode=true&useSSL=false&characterEncoding=utf8"; private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver"; private static final String USERNAME = "root"; private static final String PASSWORD = "123456"; // 注释显示的作者 private static final String AUTHOR = "mouday"; // 代码生成指定包名 private static final String PACKAGE_NAME = "com.example"; public static void main(String[] args) { // ======= 全局配置 ======= GlobalConfig globalConfig = new GlobalConfig(); String dir = System.getProperty("user.dir"); globalConfig.setOutputDir(dir + "/src/main/java"); // 输出目录 globalConfig.setOpen(false); // 不打开输出目录 globalConfig.setAuthor(AUTHOR); // 开发人员 // ======= 数据源配置 ======= DataSourceConfig dataSource = new DataSourceConfig(); dataSource.setDriverName(DRIVER_NAME); dataSource.setUsername(USERNAME); dataSource.setPassword(PASSWORD); dataSource.setUrl(JDBC_URL); // ======= 包配置 ======= PackageConfig packageConfig = new PackageConfig(); packageConfig.setParent(PACKAGE_NAME); // ======= 策略配置 ======= StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); // 表名 strategy.setColumnNaming(NamingStrategy.underline_to_camel); // 字段 strategy.setInclude(TABLE_NAME); // 需要生成的表 strategy.setEntityLombokModel(true); // lombok模型 strategy.setRestControllerStyle(true); // Rest控制器 // 自动填充配置 TableFill createTime = new TableFill("create_time", FieldFill.INSERT); TableFill updateTime = new TableFill("update_time", FieldFill.UPDATE); ArrayList<TableFill> tableFills = new ArrayList<>(); tableFills.add(createTime); tableFills.add(updateTime); strategy.setTableFillList(tableFills); // ======= 执行生成 ======= AutoGenerator autoGenerator = new AutoGenerator(); autoGenerator.setGlobalConfig(globalConfig); autoGenerator.setDataSource(dataSource); autoGenerator.setPackageInfo(packageConfig); autoGenerator.setStrategy(strategy); autoGenerator.execute(); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/111644828
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)