web123456

Mybatis-plus code generator

import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.config.DataSourceConfig; import com.baomidou.mybatisplus.generator.config.GlobalConfig; import com.baomidou.mybatisplus.generator.config.PackageConfig; import com.baomidou.mybatisplus.generator.config.StrategyConfig; import com.baomidou.mybatisplus.generator.config.po.TableFill; import com.baomidou.mybatisplus.generator.config.rules.DateType; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import org.junit.jupiter.api.Test; import java.util.ArrayList; public class CodeGenerator { @Test public void genCode() { // Prefix (table name in the database) String prefix = "xxx_"; String moduleName = "xxx"; // 1. Create a code generator AutoGenerator mpg = new AutoGenerator(); // 2. Global configuration GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty(""); gc.setOutputDir(projectPath + "/src/main/java"); gc.setAuthor("cxl"); gc.setOpen(false); // Whether to open Explorer after generation gc.setFileOverride(false); // Whether the file is overwritten when re-created gc.setServiceName("%sService"); // Remove the initial letter I of the Service interface gc.setIdType(IdType.ASSIGN_ID); // Primary key policy gc.setDateType(DateType.ONLY_DATE);// Define the date type in the generated entity class gc.setSwagger2(true);// Turn on Swagger2 mode mpg.setGlobalConfig(gc); // 3. Data source configuration DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://127.0.0.1:3306/" + prefix + moduleName + "?serverTimezone=UTC"); dsc.setDriverName(""); dsc.setUsername("root"); dsc.setPassword("root"); dsc.setDbType(DbType.MYSQL); mpg.setDataSource(dsc); // 4. Package configuration PackageConfig pc = new PackageConfig(); pc.setParent(""); pc.setModuleName(moduleName); //Module name pc.setController("controller"); pc.setEntity("entity"); pc.setService("service"); pc.setMapper("mapper"); mpg.setPackageInfo(pc); // 5. Policy configuration StrategyConfig strategy = new StrategyConfig(); // What tables are generated strategy.setInclude("xxx","xxxx"); // Naming policy for database table mapping to entities strategy.setNaming(NamingStrategy.underline_to_camel); // Set table prefix not to be generated strategy.setTablePrefix(moduleName + "_"); // Naming policy for database table fields to map to entities strategy.setColumnNaming(NamingStrategy.underline_to_camel); // lombok model @Accessors(chain = true) setter chain operation strategy.setEntityLombokModel(true); // Logical delete field names strategy.setLogicDeleteFieldName("is_deleted"); // Remove the is_prefix of the boolean value strategy.setEntityBooleanColumnRemoveIsPrefix(true); // Automatic filling 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); // restful api style controller strategy.setRestControllerStyle(true); // Camel to hyphen in url strategy.setControllerMappingHyphenStyle(true); mpg.setStrategy(strategy); // 6. Execute mpg.execute(); } }