MyBatisPlus——代码生成器

风陵南·2023-04-10 15:47·219 次阅读

MyBatisPlus——代码生成器

代码生成器

  • 快速生成各项代码

步骤#

  • 创建Generator类,并创建main方法
  • 创建代码生成器
AutoGenerator autoGenerator = new AutoGenerator();
  • 连接要生成实体类的数据库
DataSourceConfig dataSource = new DataSourceConfig();
dataSource.setDriverName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/mybatisplus_db?serverTimezone=UTC");
dataSource.setUsername("root");
dataSource.setPassword("1234");
autoGenerator.setDataSource(dataSource);
  • 配置全局设置
// 设置全局配置
GlobalConfig globalConfig = new GlobalConfig();
// 设置生成的代码的输出目录
globalConfig.setOutputDir(System.getProperty("user.dir") + "/mybatisplus_04_generator/src/main/java");
// 设置生成完毕后是否打开生成代码所在目录(默认为true)
globalConfig.setOpen(false);
// 设置作者
globalConfig.setAuthor("程渝");
// 设置是否覆盖原始代码(默认为false)
globalConfig.setFileOverride(true);
// 设置数据层接口名,%s为占位符,指代模块名称
globalConfig.setMapperName("%sDao");
// 设置id生成策略
globalConfig.setIdType(IdType.ASSIGN_ID);
autoGenerator.setGlobalConfig(globalConfig);
  • 配置包名相关设置
// 设置包名相关配置
PackageConfig packageConfig = new PackageConfig();
// 设置生成的包名
packageConfig.setParent("com.aaa");
// 设置实体类包名
packageConfig.setEntity("domain");
// 设置数据层包名
packageConfig.setMapper("dao");
autoGenerator.setPackageInfo(packageConfig);
  • 配置策略设置
// 策略设置
StrategyConfig strategyConfig = new StrategyConfig();
// 设置当前参与生成的表名,参数为String...,可传入多个表名
strategyConfig.setInclude("tbl_user");
// 设置表名前缀,模块名=数据库表名-表名前缀
strategyConfig.setTablePrefix("tbl_");
// 设置是否启用Rest风格
strategyConfig.setRestControllerStyle(true);
// 设置乐观锁字段名
strategyConfig.setVersionFieldName("version");
// 设置逻辑删除字段名
strategyConfig.setLogicDeleteFieldName("deleted");
// 设置是否启用lombok
strategyConfig.setEntityLombokModel(true);
autoGenerator.setStrategy(strategyConfig);
  •  执行代码生成
// 执行代码生成
autoGenerator.execute();
  • 生成结果

  

posted @   风陵南  阅读(219)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示
目录