打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
MyBatis-Plus学习笔记(八) 代码自动生成器
  • AutoGenerator 是 MyBatis-Plus 的代码生成器
  • AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等模块的代码

一、新建一个类-编写代码自动生成配置

public class zhouDode {    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("heelo");   //是否打开输出目录                      gc.setOpen(false);   //是否覆盖已有文件                          gc.setFileOverride(false);                         //service的命名方式,%可去Service的I前缀,如:%sBusiness 生成 UserBusinessgc.setServiceName("%sService");                  //指定生成的主键的ID类型gc.setIdType(IdType.ID_WORKER);//时间类型对应策略gc.setDateType(DateType.ONLY_DATE);//开启 swagger2 模式gc.setSwagger2(true);mpg.setGlobalConfig(gc);

三、添加数据源配置

通过此配置指定需要生成代码的具体数据库

DataSourceConfig dsc = new DataSourceConfig();//驱动连接的urldsc.setUrl("jdbc:mysql://localhost:3306/kuang_community&useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT+8");//驱动名dsc.setDriverName("com.mysql.cj.jdbc.Driver");//数据库连接用户名dsc.setUsername("root");//数据库连接密码dsc.setPassword("123456");//数据库类型,该类内置了常用的数据库类型(必填)dsc.setDbType(DbType.MYSQL); mpg.setDataSource(dsc);

四、添加包的配置

包名配置,通过该配置,指定生成代码的包路径

PackageConfig pc = new PackageConfig();    //父包模块名pc.setModuleName("blog"); //父包名 如果为空,下面的子包名必须写全;如果非空,下面仅写子包名即可pc.setParent("com.zhou");//Entity包名pc.setEntity("entity"); //Mapper包名pc.setMapper("mapper");//Service包名pc.setService("service"); //Controller包名pc.setController("controller");mpg.setPackageInfo(pc);

五、添加数据库表配置

StrategyConfig strategy = new StrategyConfig();//需要包含的表名(与exclude二选一配置)strategy.setInclude("blog_tags","course","links","sys_settings","user_record");    //数据库表映射到实体的命名策略strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略, 未指定按照 naming 执行strategy.setColumnNaming(NamingStrategy.underline_to_camel);//实体是否为lombok模型(默认 false)//3.3.2以下版本默认生成了链式模型,3.3.2以后,默认不生成,如有需要需开启 chainModelstrategy.setEntityLombokModel(true);    //逻辑删除属性名称strategy.setLogicDeleteFieldName("deleted");     //自动填充配置TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT);TableFill gmtModified = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);ArrayList<TableFill> tableFills = new ArrayList<>();tableFills.add(gmtCreate); tableFills.add(gmtModified);strategy.setTableFillList(tableFills);    // 乐观锁strategy.setVersionFieldName("version");//生成 @RestController 控制器strategy.setRestControllerStyle(true);//驼峰转连字符strategy.setControllerMappingHyphenStyle(true);    mpg.setStrategy(strategy);

六、执行

mpg.execute(); 

 

来源:https://www.icode9.com/content-4-875751.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
mybatis_plus_autogenerator生成代码
Mybatis Plus代码生成
手把手教你SpringBoot整合MybatisPlus 代码生成器
smartsofthelp 最好,最干净的NetFramework 代码生成器
SpringBoot持久化层操作支持(一)SQL篇
MyBatis与MyBatis
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服