备忘录:mybatis-plus代码自动生成
main方法
public static void main(String[] args) {
// 需要构建一个 代码自动生成器 对象
AutoGenerator autoGenerator = new AutoGenerator();
// 配置策略
// 1、全局配置
GlobalConfig globalConfig = new GlobalConfig();
//获取系统目录
String projectPath = System.getProperty("user.dir");
globalConfig.setOutputDir(projectPath + "/src/main/java");
globalConfig.setAuthor("AbrahamCui");//作者
globalConfig.setOpen(false);//生成后是否打开所在文件夹
globalConfig.setFileOverride(false); // 是否覆盖
globalConfig.setServiceName("%Service"); // 去Service的I前缀
globalConfig.setIdType(IdType.ID_WORKER);//主键策略
globalConfig.setDateType(DateType.ONLY_DATE);//日期配置
globalConfig.setSwagger2(true);//自动配置swagger2
autoGenerator.setGlobalConfig(globalConfig);//装入
// 2、设置数据源
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/music?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
dsc.setDbType(DbType.MYSQL);
autoGenerator.setDataSource(dsc);//装入
// 3、包的设置
PackageConfig pc = new PackageConfig();
pc.setModuleName("music");
pc.setParent("com.javaclimb");
pc.setEntity("entity");
pc.setMapper("mapper");
pc.setService("service");
pc.setController("controller");
autoGenerator.setPackageInfo(pc);//装入
// 4、策略配置
StrategyConfig strategyConfig = new StrategyConfig();
// 设置要映射的表名
strategyConfig.setInclude("collect");
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
strategyConfig.setEntityLombokModel(true); // 自动lombok
// 乐观锁
/*strategyConfig.setVersionFieldName("version");*/
strategyConfig.setLogicDeleteFieldName("deleted");
// 自动填充配置
TableFill gmtCreate = new TableFill("create_time", FieldFill.INSERT);
TableFill gmtModified = new TableFill("update_time", FieldFill.INSERT_UPDATE);
ArrayList<TableFill> tableFills = new ArrayList<>();
tableFills.add(gmtCreate);//装入
tableFills.add(gmtModified);//装入
strategyConfig.setTableFillList(tableFills);//装入
strategyConfig.setRestControllerStyle(true);
strategyConfig.setControllerMappingHyphenStyle(true); // localhost:8080/hello_id_2
autoGenerator.setStrategy(strategyConfig);//装入
autoGenerator.execute();//执行
pom.xml和上面代码相关的依赖
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
|