mybatisplus generator 代码生成器

本文介绍MyBatis Plus代码生成器的使用方法,包括配置数据源、作者信息、输出路径等,并展示了如何通过修改generator.properties文件及templates下的模板来生成所需的Java文件。此外,还提供了部分源代码示例,帮助读者更好地理解其工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

##mybatis-plus 代码生成器
基于 mybatis-plus generator

git: https://github.com/lvzhyt/mybatisplus-generator


模板工具 velocity
修改配置文件 generator.properties

修改templates下的模板 生成想要的文件 只保留了vm

详细 参见官方 

https://mp.baomidou.com/guide/generator.html#%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B


# 数据源
datasource.url=jdbc:mysql://localhost:3306/paw-patrol?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC
datasource.driver-class-name=com.mysql.cj.jdbc.Driver
datasource.username=root
datasource.password=123456


# 作者
author=Rubble

# 输出路径 可调整输出到其他module中
output-dir=src/main/java

# 模块
module=module

# 包名
package=com.tg.code

# 数据库表名
table-names=dms_user

#swagger2
swagger2=true

# 父类实体,没有就不用设置
super-entity-class=

# 父类控制器,没有就不用设置
# com.patrol.db.mapper.SuperMapper
super-mapper-class=

# 父类控制器,没有就不用设置
super-service-class=

# 父类控制器,没有就不用设置
super-controller-class=
可配置的参数
参见 git 
```html
https://gitee.com/baomidou/mybatis-plus/blob/3.0/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/engine/AbstractTemplateEngine.java
```
部分源代码
```java
class AbstractTemplateEngine {
 /**
      * 渲染对象 MAP 信息
      *
      * @param tableInfo 表信息对象
      * @return ignore
      */
     public Map<String, Object> getObjectMap(TableInfo tableInfo) {
         Map<String, Object> objectMap = new HashMap<>(30);
         ConfigBuilder config = getConfigBuilder();
         if (config.getStrategyConfig().isControllerMappingHyphenStyle()) {
             objectMap.put("controllerMappingHyphenStyle", config.getStrategyConfig().isControllerMappingHyphenStyle());
             objectMap.put("controllerMappingHyphen", StringUtils.camelToHyphen(tableInfo.getEntityPath()));
         }
         objectMap.put("restControllerStyle", config.getStrategyConfig().isRestControllerStyle());
         objectMap.put("config", config);
         objectMap.put("package", config.getPackageInfo());
         GlobalConfig globalConfig = config.getGlobalConfig();
         objectMap.put("author", globalConfig.getAuthor());
         objectMap.put("idType", globalConfig.getIdType() == null ? null : globalConfig.getIdType().toString());
         objectMap.put("logicDeleteFieldName", config.getStrategyConfig().getLogicDeleteFieldName());
         objectMap.put("versionFieldName", config.getStrategyConfig().getVersionFieldName());
         objectMap.put("activeRecord", globalConfig.isActiveRecord());
         objectMap.put("kotlin", globalConfig.isKotlin());
         objectMap.put("swagger2", globalConfig.isSwagger2());
         objectMap.put("date", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
         objectMap.put("table", tableInfo);
         objectMap.put("enableCache", globalConfig.isEnableCache());
         objectMap.put("baseResultMap", globalConfig.isBaseResultMap());
         objectMap.put("baseColumnList", globalConfig.isBaseColumnList());
         objectMap.put("entity", tableInfo.getEntityName());
         objectMap.put("entitySerialVersionUID", config.getStrategyConfig().isEntitySerialVersionUID());
         objectMap.put("entityColumnConstant", config.getStrategyConfig().isEntityColumnConstant());
         objectMap.put("entityBuilderModel", config.getStrategyConfig().isEntityBuilderModel());
         objectMap.put("entityLombokModel", config.getStrategyConfig().isEntityLombokModel());
         objectMap.put("entityBooleanColumnRemoveIsPrefix", config.getStrategyConfig().isEntityBooleanColumnRemoveIsPrefix());
         objectMap.put("superEntityClass", getSuperClassName(config.getSuperEntityClass()));
         objectMap.put("superMapperClassPackage", config.getSuperMapperClass());
         objectMap.put("superMapperClass", getSuperClassName(config.getSuperMapperClass()));
         objectMap.put("superServiceClassPackage", config.getSuperServiceClass());
         objectMap.put("superServiceClass", getSuperClassName(config.getSuperServiceClass()));
         objectMap.put("superServiceImplClassPackage", config.getSuperServiceImplClass());
         objectMap.put("superServiceImplClass", getSuperClassName(config.getSuperServiceImplClass()));
         objectMap.put("superControllerClassPackage", verifyClassPacket(config.getSuperControllerClass()));
         objectMap.put("superControllerClass", getSuperClassName(config.getSuperControllerClass()));
         return Objects.isNull(config.getInjectionConfig()) ? objectMap : config.getInjectionConfig().prepareObjectMap(objectMap);
     }
 }
```
### MyBatisPlus 代码生成器使用教程 #### Maven依赖引入 要在项目中集成MyBatisPlus代码生成器,需先在`pom.xml`文件中加入相应的Maven依赖[^1]。 ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.5.2</version> </dependency> <!-- 模板引擎 freemarker --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>${freemarker.version}</version> </dependency> ``` #### 配置代码生成器 接下来定义一个Java类来配置并启动代码生成过程。此部分展示了如何创建基本的代码生成器实例以及设置其属性。 ```java 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; public class CodeGenerator { 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("Your Name"); //作者名 gc.setOpen(false); mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/your_database?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("password"); mpg.setDataSource(dsc); // 包信息配置 PackageConfig pc = new PackageConfig(); pc.setParent("com.example.demo"); mpg.setPackageInfo(pc); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setInclude("table_name"); // 表名 strategy.setNaming(NamingStrategy.underline_to_camel); // 下划线转驼峰命名 mpg.setStrategy(strategy); mpg.execute(); // 执行生成操作 } } ``` 上述代码片段实现了数据库表结构向实体类、Mapper接口以及其他必要组件转换的过程。通过调整参数可以满足不同场景下的需求。 对于更复杂的定制化需求,比如添加Swagger注解支持,则可能需要额外探索其他工具或插件的支持方式[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值