mybais-plus integration springboot, automatic code generation.
The first step: the leader packet: mybatis-plus-generator packages and packages freemarker
<dependencies> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.29</version> </dependency> </dependencies>
Step Two: class code generation:
public class generate { /** * <p> * 读取控制台内容 * </p> */ 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; } } the throw new new MybatisPlusException ( "Please enter the correct" Tip + + "!" ); } public static void main (String [] args) { // code generator AutoGenerator MPG = new new AutoGenerator (); // global globalconfig GC = new new globalconfig (); String ProjectPath = the System.getProperty ( "user.dir" );
// generates code in which a path gc.setOutputDir (ProjectPath + "/ bootstudy / the src / main / Java") ;
// created gc.setAuthor ( "Yang" ); gc.setOpen ( to false ); // gc.setSwagger2 (to true); entity attributes Swagger2 annotation mpg.setGlobalConfig (GC); // data source configuration DataSourceConfig DSC = new new DataSourceConfig ();
/ / set database information dsc.setUrl ( "jdbc: MySQL: // localhost:? 3306 / = to true useUnicode the Boot & useSSL = false & characterEncoding = utf8 & serverTimezone = Asia / on Shanghai"); // dsc.setSchemaName ( "public"); dsc.setDriverName ( "com.mysql.cj.jdbc.Driver" ); dsc.setUsername ( "root" ); dsc.setPassword("123456" ); mpg.setDataSource (DSC); // package configuration PackageConfig PC = new new PackageConfig ();
// Set the module name pc.setModuleName (Scanner ( "module name")); pc.setParent ( "com.springboot .modules "); pc.setEntity (" Model " ); mpg.setPackageInfo (PC); // custom configuration InjectionConfig CFG = new new InjectionConfig () { @Override public void initMap () { // to do Nothing } }; // if the template engine is FreeMarker String the templatePath = "/ templates / mapper.xml.ftl "; // If the template engine is Velocity // String the templatePath = "/templates/mapper.xml.vm"; // custom output configurations List < FileOutConfig> = focList new new the ArrayList <> (); // custom configuration may be preferentially output focList.add ( new new FileOutConfig (the templatePath) { @Override public String the outputFile (the TableInfo TableInfo) { // customize the output file name, if you Entity set up before the suffix name xml attention here will change !! ensuing return ProjectPath + "/ bootstudy / src / main / Resources / Mapper /" +pc.getModuleName () + "/" + tableInfo.getEntityName () + "Mapper" + StringPool.DOT_XML; } }); / * Cfg.setFileCreate (new new IFileCreate () { @Override public boolean isCreate (ConfigBuilder configBuilder, FileType fileType, String filePath) { // determine whether you need a custom folder created checkDir ( "call the default method to create the directory" ); return to false; } }); * / cfg.setFileOutConfigList (focList); mpg.setCfg (CFG); // configuration template templateConfig templateConfig = new new templateConfig (); // configure custom output templates // specify a custom template path, be careful not to bring .ftl / .vm, will be automatically recognized by the template engine used //templateConfig.setEntity ( "Templates / entity2.java"); // templateConfig.setService (); // templateConfig.setController (); templateConfig.setXml ( null ); mpg.setTemplate (templateConfig); // policy configuration StrategyConfig strategy = new new StrategyConfig (); strategy.setNaming (NamingStrategy.underline_to_camel); strategy.setColumnNaming (NamingStrategy.underline_to_camel); // strategy.setSuperEntityClass ( "your own parent entity, no do not set!"); strategy.setEntityLombokModel ( to true ); strategy.setRestControllerStyle ( to true ); // common parent class // strategy.setSuperControllerClass ( "you yourself the parent controller, no do not set! "); // write to the parent class public fields strategy.setSuperEntityColumns (" the above mentioned id " ); strategy.setInclude (Scanner ( " table, multiple comma split ") .split ( "," )); strategy.setControllerMappingHyphenStyle ( to true ); strategy.setTablePrefix (pc.getModuleName () + "_" ); mpg.setStrategy ( Strategy); mpg.setTemplateEngine ( new new FreemarkerTemplateEngine ()); mpg.execute (); } }
The main point is to note that the points marked red: When setting up a database error when I did not add that when & serverTimezone = Asia / Shanghai, set the mysql driver name, not add cj warning will be reported out of date, but does not affect
For more setting properties can refer to the official: https://mp.baomidou.com/config/generator-config.html#%E5%9F%BA%E6%9C%AC%E9%85%8D%E7%BD % AE
The third step is to configure freemarker:
controller.java.ftl
service.java.ftl
serviceImpl.java.ftl
mapper.java.ftl
mapper.xml.ftl
entity.java.ftl
entity.kt.ftl
path:
Effect: up and running:
Corresponding module:
Simple additions change search controller inside layer