Let’s take a look at the basics of code generators

This is the complete generator code, directly build a class to put it.

Just change the path in the code, you can automatically generate code and directory

Rely on:

< the dependency > < groupId > org. Apache. Velocity < / groupId > < artifactId > velocity - engine - core < / artifactId > < version > 2.3 < / version >  </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> < version > 3.1.2 < / version > < / dependency > < the dependency > < groupId > org. Freemarker < / groupId > <artifactId>freemarker</artifactId> <version>2.3.31</version> </dependency> <dependency> <groupId>com.baomidou</groupId> < artifactId > mybatis - plus - the boot - starter < / artifactId > < version > 3.1.2 < / version > < / dependency >Copy the code

Code:

/** * <p> */ public static String scanner(String tip) {scanner scanner = new scanner(system.in); StringBuilder help = new StringBuilder(); Help.append (" Please enter "+ tip +" : "); System.out.println(help.toString()); if (scanner.hasNext()) { String ipt = scanner.next(); if (StringUtils.isNotBlank(ipt)) { return ipt; }} throw new MybatisPlusException(" please enter the correct "+ tip +"! ); } public static void main(String[] args) {// AutoGenerator MPG = new AutoGenerator(); // GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/wdq-app-es/src/main/java/com/swj/company"); gc.setAuthor("jobob"); gc.setOpen(false); // gc.setSwagger2(true); Swagger2 entity attribute mpg.setGlobalConfig(GC); // DataSourceConfig DSC = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://old.shuiwujia.cn:3306/wqd_company?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2b 8&allowMultiQueries=true"); // dsc.setSchemaName("public"); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("swj2016"); mpg.setDataSource(dsc); PackageConfig PC = new PackageConfig(); Pc.setmodulename (scanner(" module name ")); pc.setParent("com.swj.company"); mpg.setPackageInfo(pc); InjectionConfig CFG = new ectionConfig() {@override public void initMap() {// to do nothing}}; / / if the template engine is a freemarker String templatePath = "/ templates/mapper. XML. FTL"; // String templatePath = "/templates/mapper.xml.vm"; List<FileOutConfig> focList = new ArrayList<>(); Add (new FileOutConfig(templatePath) {@override public String outputFile(TableInfo TableInfo) { // Customize the output file name. If your Entity has a prefix or suffix, note that the XML name will change accordingly!! return projectPath + "/wdq-app-es/src/main/java/com/swj/company/" + pc.getModuleName() + "/" + tableInfo.getEntityName()  + "Mapper" + StringPool.DOT_XML; }}); /* cfg.setFileCreate(new IFileCreate() { @Override public boolean isCreate(ConfigBuilder configBuilder, FileType FileType, String filePath) {// Check whether the custom folder needs to create checkDir(" call the default directory created, custom directory used "); If (fileType == filetype.mapper) {if (fileType == filetype.mapper) {return false! new File(filePath).exists(); } // allow template files to be generated. }}); */ cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); // TemplateConfig TemplateConfig = new TemplateConfig(); // Configure a custom output template. // Specify a custom template path without. FTL /.vm, which is automatically identified as "templates/entity2.java"; // templateConfig.setService(); // templateConfig.setController(); templateConfig.setXml(null); mpg.setTemplate(templateConfig); // StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); Strategy. SetSuperEntityClass (" your parent entity, there is no need not set!" ); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); / / public parent strategy. SetSuperControllerClass (" your parent controller, no need not set up!" ); / / write in public field in the parent class strategy. SetSuperEntityColumns (" id "); Strategy.setinclude (scanner(" table name, multiple English comma split ").split(",")); strategy.setControllerMappingHyphenStyle(true); System.out.println("pc.getModuleName():"+pc.getModuleName()); strategy.setTablePrefix(pc.getModuleName() + "_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); }Copy the code

Here is the application of the generator’s custom configuration.

1. Customize the entity class file name generated by the table name: Set the prefix of the table here and filter out the prefix.

2,