首页 > MyBatis-plus 自动生成代码
头像
社交牛逼一号
发布于 2021-08-25 22:30
+ 关注

MyBatis-plus 自动生成代码

<!-- mybatis plus 代码生成器 --> <dependency>  <groupId>com.baomidou</groupId>  <artifactId>mybatis-plus-generator</artifactId>  <version>3.2.0</version> </dependency>  <dependency>  <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId>  <version>2.3.28</version> </dependency>  <dependency>  <groupId>org.slf4j</groupId>  <artifactId>slf4j-log4j12</artifactId>  <version>1.6.4</version> </dependency> 
一定要使用这几个版本的jar包,不然生成不会成功
再按照以下配置
package test; import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;  import java.util.Scanner;  /**  * 自动生成mybatisplus的相关代码  */ public class GeneratorCodeConfig { public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);    
      StringBuilder help = new StringBuilder();    
      help.append("请输入" + tip + "");    
      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 mpg = new AutoGenerator();   // 全局配置 
       GlobalConfig gc = new GlobalConfig();    String projectPath = System.getProperty("user.dir"); // E:\Server\HIMS    System.out.println(projectPath); //E:\Server\HIMS  gc.setOutputDir(projectPath + "/src/main/java");    gc.setAuthor("brothdayluo"); //作者信息  gc.setOpen(false);  //实体属性 Swagger2 注解  gc.setSwagger2(false);    mpg.setGlobalConfig(gc);   // 数据源配置    DataSourceConfig dsc = new DataSourceConfig();    dsc.setUrl("jdbc:mysql://localhost:3306/hims?useUnicode=true&characterEncoding=utf-8");    dsc.setDriverName("com.mysql.jdbc.Driver");    dsc.setUsername("root");    dsc.setPassword("123456");    mpg.setDataSource(dsc);   // 包配置    PackageConfig pc = new PackageConfig();    pc.setParent("com.hims");    pc.setEntity("pojo");    pc.setMapper("mapper");    pc.setService("service");    pc.setServiceImpl("service.impl");    mpg.setPackageInfo(pc);    // 配置模板    TemplateConfig templateConfig = new TemplateConfig();     templateConfig.setXml(null);  mpg.setTemplate(templateConfig);   // 策略配置    StrategyConfig strategy = new StrategyConfig();    strategy.setNaming(NamingStrategy.underline_to_camel);    strategy.setColumnNaming(NamingStrategy.underline_to_camel);     strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));    strategy.setControllerMappingHyphenStyle(true);    strategy.setTablePrefix(pc.getModuleName() + "_");    mpg.setStrategy(strategy);    mpg.setTemplateEngine(new FreemarkerTemplateEngine());    mpg.execute();  }
}

全部评论

(1) 回帖
加载中...
话题 回帖

相关热帖

近期精华帖

热门推荐