package com.chen; import java.util.ArrayList; 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 java.util.ArrayList; //代码自动生成器 public class ChenCode { public static void main(String[] args) { //需要构建一个 代码自动生成器 对象 AutoGenerator mpg = new AutoGenerator(); //配置策略 //1、全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); //获取当前系统目录 gc.setOutputDir(projectPath+"/src/main/java"); //自动生成代码的位置 gc.setAuthor("小陈"); //作者 gc.setOpen(false); //是否打开文件夹 gc.setFileOverride(false); //是否覆盖原来生成的 gc.setServiceName("%sService"); // 去Service的I前缀 gc.setIdType(IdType.ID_WORKER); gc.setDateType(DateType.ONLY_DATE); gc.setSwagger2(true); mpg.setGlobalConfig(gc); //2、设置数据源 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8&useSSL=false&useUnicode=true&characterEncoding=utf-8"); dsc.setUsername("root"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setPassword("123456"); dsc.setDbType(DbType.MYSQL); mpg.setDataSource(dsc); //3、配置包的位置 PackageConfig pc = new PackageConfig(); pc.setModuleName("blog"); //设置模块名称 pc.setParent("com.chen"); pc.setEntity("entity"); pc.setMapper("mapper"); pc.setService("service"); pc.setController("controller"); mpg.setPackageInfo(pc); //3、策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setInclude("user"); //设置要映射的表名 strategy.setNaming(NamingStrategy.underline_to_camel); //包命名时下划线能转驼峰命名 strategy.setColumnNaming(NamingStrategy.underline_to_camel); //列命名时下划线也能转驼峰命名 strategy.setEntityLombokModel(true); //自动生成Lombok strategy.setLogicDeleteFieldName("deleted"); //设置逻辑删除 //自动填充配置 TableFill createTime = new TableFill("create_time", FieldFill.INSERT); TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE); ArrayList<TableFill> tableFills = new ArrayList<>(); tableFills.add(createTime); tableFills.add(updateTime); strategy.setTableFillList(tableFills); //乐观锁配置 strategy.setVersionFieldName("version"); strategy.setRestControllerStyle(true); //开启驼峰命名规则 strategy.setControllerMappingHyphenStyle(true); //可以使用下划线 Localhost:8080/hello_id_2 mpg.setStrategy(strategy); mpg.execute();//执行代码生成器 } }
pom文件的依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--数据库驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency> <!--Lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.10</version> </dependency> <!--mybatis_plus--> <!--mybaits_plus 是自己开发的,并非官方的--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.5</version> </dependency> <!--代码生成器依赖--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.4.0</version> </dependency> <!-- Swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <!--模板引擎 依赖--> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies>