To use the mybatis plus code generator, you must first add dependencies (also known as import packages), and import packages according to the official website recommendations and project requirements
Official website link: https://mp.baomidou.com/guide/
1. Add code generator dependency
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.4.1</version> </dependency>
2. Add template engine dependencies as needed. Specific engine instances are available on the official website. This article takes Freemarker template engine as an example
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>latest-freemarker-version</version> </dependency>
3. Start writing the code generator class
Create a class to place the automatic generator code, which can generally be created in the test folder (test)
Put the code generator code in a main() method and run it directly
① Create a code generator object
AutoGenerator mpg = new AutoGenerator();
② Global configuration
// Global configuration GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); //Gets the directory of the current project gc.setOutputDir(projectPath + "/src/main/java"); //Set code generation path gc.setAuthor("jobob"); //Set code author mpg.setGlobalConfig(gc); //Put the global configuration into the object of the code generator
③ Data source configuration
One of the powerful functions of mybatis plus code generator is that it does not need you to create the object corresponding to the table in the database in advance. The code generator can directly generate the corresponding object by reading the database table and column name
// Data source configuration DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/ant?useUnicode=true&useSSL=false&characterEncoding=utf8"); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("password"); mpg.setDataSource(dsc);
This configuration is used to directly generate objects corresponding to database tables in pojo files.
④ Configure the package (take my personal project as an example)
// Package configuration PackageConfig pc = new PackageConfig(); pc.setParent("com.jp"); pc.setEntity("pojo"); pc.setController("controller"); pc.setMapper("mapper"); pc.setService("service"); pc.setServiceImpl("service.impl");
⑤ After the package configuration, the code on the official website directly puts the package configuration into the code generator object, and then customize the output configuration. Personally, the process is cumbersome. Here is a simple method. For your reference only
The entry of code generator execution is to execute generation, that is mpg.excupt()
When you look at the source code, you will find (because it is open source, you can check it in the java code writing tool)
public void execute() { logger.debug("==========================Prepare to generate files...=========================="); if (null == this.config) { this.config = new ConfigBuilder(this.packageInfo, this.dataSource, this.strategy, this.template, this.globalConfig); if (null != this.injectionConfig) { this.injectionConfig.setConfig(this.config); } }
A packageInfo attribute is passed in the ConfigBuilder object, so if you look deeper, you will find such a piece of code in the handlerPackage class
this.setPathInfo(this.pathInfo, template.getEntity(this.getGlobalConfig().isKotlin()), outputDir, "entity_path", "Entity"); this.setPathInfo(this.pathInfo, template.getMapper(), outputDir, "mapper_path", "Mapper"); this.setPathInfo(this.pathInfo, template.getXml(), outputDir, "xml_path", "Xml"); this.setPathInfo(this.pathInfo, template.getService(), outputDir, "service_path", "Service"); this.setPathInfo(this.pathInfo, template.getServiceImpl(), outputDir, "service_impl_path", "ServiceImpl"); this.setPathInfo(this.pathInfo, template.getController(), outputDir, "controller_path", "Controller");
It is used to set the code generation path, so we can set the code generation path in this way (take my personal code as an example)
I think this is a good place to use. You can consider whether to use it according to your actual situation
//Custom build path Map<String,String> map = new HashMap<>(); map.put("entity_path",config + "/src/main/java/com/jp/pojo"); map.put("mapper_path",config + "/src/main/java/com/jp/mapper"); map.put("xml_path",config + "/src/main/resources/com/jp/mapper"); map.put("service_path",config + "/src/main/java/com/jp/service"); map.put("service_impl_path",config + "/src/main/java/com/jp/service/impl"); map.put("controller_path",config + "/src/main/java/com/jp/controller"); pc.setPathInfo(map); //Put the generated path into the map mpg.setPackageInfo(pc);
⑥ Policy configuration
// Policy configuration StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setSuperEntityClass("Your own parent entity,No, you don't have to set it!"); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); // Public parent class strategy.setSuperControllerClass("Your own parent controller,No, you don't have to set it!"); // Public fields written in the parent class strategy.setSuperEntityColumns("id"); strategy.setInclude(scanner("Table name, separated by multiple English commas").split(",")); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix(pc.getModuleName() + "_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine());
⑦ Execute build
mpg.execute();
Problems encountered in my personal code writing:
Caused by package guide error
The sudden error here is due to the error of the upper guide package
Caused by GlobalConfig package inversion error
What should be guided is
GlobalConfig under generator
Statement: This article is written for your own reference to the official website. If there is infringement, please contact and delete the post