- @Test
- void generatorCode() {
- FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/admin", "root", "root")
- .globalConfig(builder -> {
- builder.author("liangdada") // 设置作者
- .enableSwagger() // 开启 swagger 模式
- // .fileOverride()
- .disableOpenDir() //禁止打开输出目录
- .outputDir("D:\\my\\sharding-jdbc\\src\\main\\java"); // 指定输出目录
- })
- .packageConfig(builder -> {
- builder.parent("com.liang") // 设置父包名
- .moduleName("sharding") // 设置父包模块名
- .pathInfo(
- new HashMap
(){{ - put(OutputFile.xml,"D:\\my\\sharding-jdbc\\src\\main\\resources\\mapper");
- // put(OutputFile.parent,"D:\\my\\sharding-jdbc\\src\\main\\java\\client");
- }}
- ); // 设置mapperXml生成路径
- })
- .strategyConfig(builder -> {
- builder.addInclude("user")// 设置需要生成的表名 // .addTablePrefix("t_", "c_"); // 设置过滤表前缀
- .entityBuilder() //entity配置
- // .enableFileOverride() //覆盖文件
- .enableLombok() // 开启lombok
- // .serviceBuilder()
- // .enableFileOverride()
- .controllerBuilder()
- .enableRestStyle() // 开启生成@RestController 控制器
- // .enableFileOverride()
- .mapperBuilder()
- .enableBaseResultMap(); // 启用 BaseResultMap 生成
- // .enableFileOverride(); // 是否覆盖
- })
- // 模板配置
- .templateConfig(builder -> {
- builder.controller("/templates/controller.java")
- .xml("/templates/mapper.xml");
- })
- .templateEngine(new FreemarkerTemplateEngine(){
- // 重写自定义文件输出
- @Override
- protected void outputCustomFile(List
customFiles, TableInfo tableInfo, Map objectMap) { - String entityName = tableInfo.getEntityName();
- String parentPath = this.getPathInfo(OutputFile.parent);
- customFiles.forEach((file) -> {
- String filePath = StringUtils.isNotBlank(file.getFilePath()) ? file.getFilePath() : parentPath;
- String fileName = file.getFileName();
- if(fileName.contains("Req")){
- filePath = filePath + "\\pojo\\req";
- }else if (fileName.contains("Dto")){
- filePath = filePath + "\\pojo\\dto";
- }else if (fileName.contains("Resp")){
- filePath = filePath + "\\pojo\\resp";
- }else if (fileName.contains("Pojo")){
- filePath = filePath + "\\pojo\\mapper";
- }else if (fileName.contains("Query")){
- filePath = filePath + "\\pojo\\query";
- }
- //Query
-
- if (StringUtils.isNotBlank(file.getPackageName())) {
- filePath = filePath + File.separator + file.getPackageName();
- }
- fileName = filePath + File.separator + entityName + file.getFileName();
- log.info("fileName===>{}",fileName);
- this.outputFile(new File(fileName), objectMap, file.getTemplatePath(), file.isFileOverride());
- });
- }
- }) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
- .injectionConfig(builder -> {
- builder.beforeOutputFile((tableInfo,objectMap)->{
- // 预处理信息
- System.out.println(tableInfo.getEntityName());
- System.out.println(objectMap);
- })
- .customMap(// 设置模板参数
- Collections.singletonMap("Dto","Dto")
- )
- .customFile(new HashMap
(){{ - // key 生成的文件名,val 模板路径
- put("Dto.java","/templates/entityDTO.java.ftl");
- put("Req.java","/templates/entityReq.java.ftl");
- put("Resp.java","/templates/entityResp.java.ftl");
- put("PojoMapper.java","/templates/pojoMapper.java.ftl");
- put("Query.java","/templates/entityQuery.java.ftl");
- }})
- .build();
- })
- .execute();
- }