完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| package com.itheima.reggie;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.generator.FastAutoGenerator; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.ArrayList; import java.util.Collections; import java.util.List;
public class MysqlGenerator {
public static void main(String[] args) { List<String> tables = new ArrayList<>(); tables.add("dish"); tables.add("setmeal");
FastAutoGenerator.create("jdbc:mysql://localhost:3306/reggie?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true","root","Xuwei19941214~") .globalConfig(builder -> { builder.author("威少") .outputDir("/Users/xuwei/Desktop/项目/reggie_take_out/src/main/java") .commentDate("yyyy-MM-dd");
}) .packageConfig(builder -> { builder.parent("com.itheima") .moduleName("reggie") .entity("entity") .service("service") .serviceImpl("service.impl") .controller("controller") .mapper("mapper") .xml("mapper.xml") .pathInfo(Collections.singletonMap(OutputFile.mapperXml,"/Users/xuwei/Desktop/项目/reggie_take_out/src/main/resources/mapper")); }) .strategyConfig(builder -> { builder.addInclude(tables)
.serviceBuilder() .formatServiceFileName("%sService") .formatServiceImplFileName("%sServiceImpl") .entityBuilder() .enableLombok() .logicDeleteColumnName("is_deleted") .enableTableFieldAnnotation() .controllerBuilder() .enableHyphenStyle() .formatFileName("%sController") .enableRestStyle() .mapperBuilder() .enableBaseResultMap() .superClass(BaseMapper.class) .formatMapperFileName("%sMapper") .enableMapperAnnotation() .formatXmlFileName("%sMapper"); }) .templateEngine(new FreemarkerTemplateEngine()) .execute(); }
}
|
导入依赖
1 2 3 4 5 6 7 8 9 10
| <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.5.1</version> </dependency>
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> </dependency>
|
添加生成数据库表
全局配置
包配置
策略配置
官网参考
https://baomidou.com/pages/981406/#%E6%95%B0%E6%8D%AE%E5%BA%93%E9%85%8D%E7%BD%AE-datasourceconfig