Browse Source

mybatisplus 代码生成器初步集成完成

dev-minio
dashan 1 year ago
parent
commit
828d7c7408
  1. 3
      .idea/compiler.xml
  2. 2
      ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/DsWebInterceptor.java
  3. 4
      ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/MiniAppsApplication.java
  4. 6
      ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/api/dummyinfo/DummyInfoApi.java
  5. 54
      ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/api/test/TestController.java
  6. 16
      ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/mapper/test/TestMapper.java
  7. 4
      ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/service/dummyinfo/DummyInfoService.java
  8. 8
      ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/service/impl/dummyinfo/DummyInfoServiceImpl.java
  9. 20
      ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/service/impl/test/TestServiceImpl.java
  10. 16
      ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/service/test/ITestService.java
  11. 5
      ds-biz/ds-miniapps/ds-miniapps-biz/src/main/resources/com/ds/miniapps/mapper/test/TestMapper.xml
  12. 4
      ds-biz/ds-miniapps/ds-miniapps-biz/src/test/java/com/ds/miniapps/biz/dummyinfo/DummyInfoServiceTest.java
  13. 2
      ds-biz/ds-miniapps/ds-miniapps-biz/src/test/java/com/ds/miniapps/biz/dummyinfo/DummyInfoTest.java
  14. 4
      ds-biz/ds-miniapps/ds-miniapps-model/src/main/java/com/ds/miniapps/entity/dummyinfo/DummyInfo.java
  15. 2
      ds-biz/ds-miniapps/ds-miniapps-model/src/main/java/com/ds/miniapps/entity/dummyinfo/TelecomOperatorsConvert.java
  16. 2
      ds-biz/ds-miniapps/ds-miniapps-model/src/main/java/com/ds/miniapps/entity/dummyinfo/TelecomOperatorsEnum.java
  17. 37
      ds-biz/ds-miniapps/ds-miniapps-model/src/main/java/com/ds/miniapps/entity/test/Test.java
  18. 3
      ds-commons/ds-basemodel/src/main/java/com/ds/commons/basemodel/response/R.java
  19. 2
      ds-commons/ds-config/ds-mybatisplus-config/pom.xml
  20. 2
      ds-commons/ds-config/ds-mybatisplus-support/pom.xml
  21. 51
      ds-commons/ds-config/ds-mybatisplus-support/src/main/java/com/ds/commons/support/generator/MybatisPlusGeneratorConfig.java
  22. 12
      ds-commons/ds-config/ds-mybatisplus-support/src/main/java/com/ds/commons/support/generator/bean/MybatisPlusGeneratorYml.java
  23. 27
      ds-commons/ds-config/ds-mybatisplus-support/src/main/java/com/ds/commons/support/generator/service/impl/MybatisPlusGeneratorServiceImpl.java
  24. 1
      ds-commons/ds-config/ds-mybatisplus-support/src/main/java/com/ds/commons/support/generator/vo/GeneratorReq.java
  25. 166
      ds-commons/ds-config/ds-mybatisplus-support/src/main/resources/templates/generator/entity.java.ftl
  26. 167
      ds-commons/ds-config/ds-mybatisplus-support/src/main/resources/templates/generator/entity.java.vm
  27. 23
      pom.xml

3
.idea/compiler.xml

@ -19,6 +19,9 @@
<module name="ds-timer-task" /> <module name="ds-timer-task" />
</profile> </profile>
</annotationProcessing> </annotationProcessing>
<bytecodeTargetLevel>
<module name="ds-mybatisplus-generator-config" target="17" />
</bytecodeTargetLevel>
</component> </component>
<component name="JavacSettings"> <component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE"> <option name="ADDITIONAL_OPTIONS_OVERRIDE">

2
ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/DsWebInterceptor.java → ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/DsWebInterceptor.java

@ -1,4 +1,4 @@
package com.ds.miniapps.biz; package com.ds.miniapps;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;

4
ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/MiniAppsApplication.java → ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/MiniAppsApplication.java

@ -1,8 +1,9 @@
package com.ds.miniapps.biz; package com.ds.miniapps;
import com.ds.commons.support.generator.config.MybatisPlusGeneratorConfig; import com.ds.commons.support.generator.config.MybatisPlusGeneratorConfig;
import com.ds.commons.web.config.exection.WebGlobalExceptionHandler; import com.ds.commons.web.config.exection.WebGlobalExceptionHandler;
import com.ds.commons.web.config.swagger.SwaggerConfig; import com.ds.commons.web.config.swagger.SwaggerConfig;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
@ -13,6 +14,7 @@ import org.springframework.context.annotation.Import;
*/ */
@SpringBootApplication @SpringBootApplication
@Import({SwaggerConfig.class, WebGlobalExceptionHandler.class, MybatisPlusGeneratorConfig.class}) @Import({SwaggerConfig.class, WebGlobalExceptionHandler.class, MybatisPlusGeneratorConfig.class})
@MapperScan("com.ds.miniapps.mapper")
public class MiniAppsApplication { public class MiniAppsApplication {
public static void main(String[] args) { public static void main(String[] args) {

6
ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/api/dummyinfo/DummyInfoApi.java → ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/api/dummyinfo/DummyInfoApi.java

@ -1,8 +1,8 @@
package com.ds.miniapps.biz.api.dummyinfo; package com.ds.miniapps.api.dummyinfo;
import com.ds.commons.basemodel.response.R; import com.ds.commons.basemodel.response.R;
import com.ds.miniapps.biz.service.dummyinfo.DummyInfoService; import com.ds.miniapps.service.dummyinfo.DummyInfoService;
import com.ds.miniapps.model.entity.dummyinfo.DummyInfo; import com.ds.miniapps.entity.dummyinfo.DummyInfo;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Min; import jakarta.validation.constraints.Min;

54
ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/api/test/TestController.java

@ -0,0 +1,54 @@
package com.ds.miniapps.api.test;
import com.ds.commons.basemodel.response.R;
import com.ds.miniapps.entity.test.Test;
import com.ds.miniapps.service.test.ITestService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* <p>
* 测试表 前端控制器
* </p>
*
* @author ds
* @since 2023-09-05
*/
@RestController
@Tag(name = "测试表",description = "用于测试")
@AllArgsConstructor
public class TestController {
private final ITestService testService;
@GetMapping("/test/list")
@Operation(summary = "查询所有测试数据",description = "查询所有测试数据")
public R<List<Test>> testList(){
return R.success(testService.list());
}
@PutMapping("/test")
@Operation(summary = "新增测试数据",description = "新增测试数据")
public R<Void> addTest(@RequestBody Test test){
return R.build(testService.save(test));
}
@GetMapping("/test/{id}")
@Operation(summary = "根据ID查询测试数据",description = "根据ID查询测试数据")
public R<Test> getById(@PathVariable("id")Integer id){
return R.success(testService.getById(id));
}
@DeleteMapping("/test/{id}")
@Operation(summary = "删除测试数据",description = "删除测试数据")
public R<Void> deleteById(@PathVariable("id")Integer id){
return R.build(testService.removeById(id));
}
}

16
ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/mapper/test/TestMapper.java

@ -0,0 +1,16 @@
package com.ds.miniapps.mapper.test;
import com.ds.miniapps.entity.test.Test;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* <p>
* 测试表 Mapper 接口
* </p>
*
* @author ds
* @since 2023-09-05
*/
public interface TestMapper extends BaseMapper<Test> {
}

4
ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/service/dummyinfo/DummyInfoService.java → ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/service/dummyinfo/DummyInfoService.java

@ -1,6 +1,6 @@
package com.ds.miniapps.biz.service.dummyinfo; package com.ds.miniapps.service.dummyinfo;
import com.ds.miniapps.model.entity.dummyinfo.DummyInfo; import com.ds.miniapps.entity.dummyinfo.DummyInfo;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import java.util.List; import java.util.List;

8
ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/service/impl/dummyinfo/DummyInfoServiceImpl.java → ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/service/impl/dummyinfo/DummyInfoServiceImpl.java

@ -1,10 +1,10 @@
package com.ds.miniapps.biz.service.impl.dummyinfo; package com.ds.miniapps.service.impl.dummyinfo;
import com.ds.commons.utils.stringutil.RandInitNameUtils; import com.ds.commons.utils.stringutil.RandInitNameUtils;
import com.ds.commons.utils.stringutil.RandInitPhoneNumbersUtils; import com.ds.commons.utils.stringutil.RandInitPhoneNumbersUtils;
import com.ds.miniapps.biz.service.dummyinfo.DummyInfoService; import com.ds.miniapps.service.dummyinfo.DummyInfoService;
import com.ds.miniapps.model.entity.dummyinfo.DummyInfo; import com.ds.miniapps.entity.dummyinfo.DummyInfo;
import com.ds.miniapps.model.enums.dummyinfo.TelecomOperatorsEnum; import com.ds.miniapps.entity.dummyinfo.TelecomOperatorsEnum;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;

20
ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/service/impl/test/TestServiceImpl.java

@ -0,0 +1,20 @@
package com.ds.miniapps.service.impl.test;
import com.ds.miniapps.entity.test.Test;
import com.ds.miniapps.mapper.test.TestMapper;
import com.ds.miniapps.service.test.ITestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
/**
* <p>
* 测试表 服务实现类
* </p>
*
* @author ds
* @since 2023-09-05
*/
@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements ITestService {
}

16
ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/service/test/ITestService.java

@ -0,0 +1,16 @@
package com.ds.miniapps.service.test;
import com.ds.miniapps.entity.test.Test;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* <p>
* 测试表 服务类
* </p>
*
* @author ds
* @since 2023-09-05
*/
public interface ITestService extends IService<Test> {
}

5
ds-biz/ds-miniapps/ds-miniapps-biz/src/main/resources/com/ds/miniapps/mapper/test/TestMapper.xml

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ds.miniapps.mapper.test.TestMapper">
</mapper>

4
ds-biz/ds-miniapps/ds-miniapps-biz/src/test/java/com/ds/miniapps/biz/dummyinfo/DummyInfoServiceTest.java

@ -6,8 +6,8 @@ package com.ds.miniapps.biz.dummyinfo;
*/ */
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.ds.miniapps.biz.service.dummyinfo.DummyInfoService; import com.ds.miniapps.service.dummyinfo.DummyInfoService;
import com.ds.miniapps.model.entity.dummyinfo.DummyInfo; import com.ds.miniapps.entity.dummyinfo.DummyInfo;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;

2
ds-biz/ds-miniapps/ds-miniapps-biz/src/test/java/com/ds/miniapps/biz/dummyinfo/DummyInfoTest.java

@ -1,7 +1,7 @@
package com.ds.miniapps.biz.dummyinfo; package com.ds.miniapps.biz.dummyinfo;
import com.ds.commons.utils.stringutil.RandInitPhoneNumbersUtils; import com.ds.commons.utils.stringutil.RandInitPhoneNumbersUtils;
import com.ds.miniapps.model.enums.dummyinfo.TelecomOperatorsEnum; import com.ds.miniapps.entity.dummyinfo.TelecomOperatorsEnum;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.Random; import java.util.Random;

4
ds-biz/ds-miniapps/ds-miniapps-model/src/main/java/com/ds/miniapps/model/entity/dummyinfo/DummyInfo.java → ds-biz/ds-miniapps/ds-miniapps-model/src/main/java/com/ds/miniapps/entity/dummyinfo/DummyInfo.java

@ -1,8 +1,6 @@
package com.ds.miniapps.model.entity.dummyinfo; package com.ds.miniapps.entity.dummyinfo;
import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.ExcelProperty;
import com.ds.miniapps.model.enums.dummyinfo.TelecomOperatorsConvert;
import com.ds.miniapps.model.enums.dummyinfo.TelecomOperatorsEnum;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;

2
ds-biz/ds-miniapps/ds-miniapps-model/src/main/java/com/ds/miniapps/model/enums/dummyinfo/TelecomOperatorsConvert.java → ds-biz/ds-miniapps/ds-miniapps-model/src/main/java/com/ds/miniapps/entity/dummyinfo/TelecomOperatorsConvert.java

@ -1,4 +1,4 @@
package com.ds.miniapps.model.enums.dummyinfo; package com.ds.miniapps.entity.dummyinfo;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.WriteConverterContext; import com.alibaba.excel.converters.WriteConverterContext;

2
ds-biz/ds-miniapps/ds-miniapps-model/src/main/java/com/ds/miniapps/model/enums/dummyinfo/TelecomOperatorsEnum.java → ds-biz/ds-miniapps/ds-miniapps-model/src/main/java/com/ds/miniapps/entity/dummyinfo/TelecomOperatorsEnum.java

@ -1,4 +1,4 @@
package com.ds.miniapps.model.enums.dummyinfo; package com.ds.miniapps.entity.dummyinfo;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;

37
ds-biz/ds-miniapps/ds-miniapps-model/src/main/java/com/ds/miniapps/entity/test/Test.java

@ -0,0 +1,37 @@
package com.ds.miniapps.entity.test;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import java.io.Serial;
/**
* <p>
* 测试表
* </p>
*
* @author ds
* @since 2023-09-05
*/
@Getter
@Setter
@TableName("tbl_test")
@Schema(name = "Test", description = "测试表")
public class Test implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Schema(description = "自增")
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@Schema(description = "名称")
private String name;
@Schema(description = "备注")
private String remark;
}

3
ds-commons/ds-basemodel/src/main/java/com/ds/commons/basemodel/response/R.java

@ -88,6 +88,9 @@ public class R<T> implements Serializable {
this.data = obj; this.data = obj;
} }
public static R<Void> build(boolean bool) {
return bool?success():failure();
}
public static R<Void> success() { public static R<Void> success() {
return new R<>(MSG.SUCCESS); return new R<>(MSG.SUCCESS);
} }

2
ds-commons/ds-config/ds-mybatisplus-config/pom.xml

@ -22,12 +22,10 @@
<dependency> <dependency>
<groupId>mysql</groupId> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>
<version>8.0.24</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.baomidou</groupId> <groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId> <artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.2</version>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

2
ds-commons/ds-config/ds-mybatisplus-support/pom.xml

@ -22,12 +22,10 @@
<dependency> <dependency>
<groupId>org.apache.velocity</groupId> <groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId> <artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.baomidou</groupId> <groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId> <artifactId>mybatis-plus-generator</artifactId>
<version>3.5.3.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.ds.common.web.config</groupId> <groupId>com.ds.common.web.config</groupId>

51
ds-commons/ds-config/ds-mybatisplus-support/src/main/java/com/ds/commons/support/generator/MybatisPlusGeneratorConfig.java

@ -1,51 +0,0 @@
package com.ds.commons.support.generator;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
/**
* @author ds
* @since 2023/9/3
*/
public class MybatisPlusGeneratorConfig {
public static void main(String[] args) {
Map<OutputFile,String> map = new HashMap<>();
map.put(OutputFile.entity,"C:\\Users\\21113\\Desktop\\dev\\dashan\\dashan-cloud\\ds-commons\\ds-config\\ds-mybatisplus-support\\src\\main\\java\\com\\ds\\commons\\entity");
map.put(OutputFile.xml,"C:\\Users\\21113\\Desktop\\dev\\dashan\\dashan-cloud\\ds-commons\\ds-config\\ds-mybatisplus-support\\src\\main\\java\\com\\ds\\commons\\mpgenerator");
FastAutoGenerator.create("jdbc:mysql://server:3306/mini_apps?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false", "mini_apps", "ds4810")
.globalConfig(builder -> {
builder.author("ds") // 设置作者
.enableSpringdoc()
.fileOverride() // 覆盖已生成文件
.outputDir(System.getProperty("user.dir")+ "/" + "\\ds-commons\\ds-config\\ds-mybatisplus-support" + "/src/main/java"); // 指定输出目录
})
.dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
int typeCode = metaInfo.getJdbcType().TYPE_CODE;
if (typeCode == Types.SMALLINT) {
// 自定义类型转换
return DbColumnType.INTEGER;
}
return typeRegistry.getColumnType(metaInfo);
}))
.packageConfig(builder -> {
builder.parent("com.ds.commons") // 设置父包名
.moduleName("mpgenerator") // 设置父包模块名
.controller("api.test")
.pathInfo(map); // 设置mapperXml生成路径
})
.strategyConfig(builder -> {
builder
.addInclude("tbl_test")
.addTablePrefix("tbl_"); // 设置需要生成的表名
})
// .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
}
}

12
ds-commons/ds-config/ds-mybatisplus-support/src/main/java/com/ds/commons/support/generator/bean/MybatisPlusGeneratorYml.java

@ -13,12 +13,18 @@ import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "mp.generator") @ConfigurationProperties(prefix = "mp.generator")
public class MybatisPlusGeneratorYml { public class MybatisPlusGeneratorYml {
/**
* 输出路径
*/
private String outputDir; private String outputDir;
/**
* 实体类输出路径
*/
private String entityOutputDir; private String entityOutputDir;
/**
* 包名
*/
private String packagePath; private String packagePath;
private String entityPackagePath;
} }

27
ds-commons/ds-config/ds-mybatisplus-support/src/main/java/com/ds/commons/support/generator/service/impl/MybatisPlusGeneratorServiceImpl.java

@ -26,10 +26,6 @@ public class MybatisPlusGeneratorServiceImpl implements IMybatisPlusGeneratorSer
private final JDBCInfoYml jdbcInfoYml; private final JDBCInfoYml jdbcInfoYml;
public static void main(String[] args) {
System.out.println("com.ds.miniapps.model");
System.out.println("com.ds.miniapps.model".replace(".","/"));
}
private static final String USER_DIR = System.getProperty("user.dir"); private static final String USER_DIR = System.getProperty("user.dir");
private static final String JAVA_PATH ="/src/main/java/"; private static final String JAVA_PATH ="/src/main/java/";
private static final String XML_PATH = "/src/main/resources/"; private static final String XML_PATH = "/src/main/resources/";
@ -37,15 +33,20 @@ public class MybatisPlusGeneratorServiceImpl implements IMybatisPlusGeneratorSer
@Override @Override
public boolean generator(GeneratorReq req) { public boolean generator(GeneratorReq req) {
//TODO 策略通过接受 req 不同值使用不同的生成策略
Map<OutputFile,String> map = new HashMap<>(); Map<OutputFile,String> map = new HashMap<>();
map.put(OutputFile.entity, USER_DIR + mybatisPlusGeneratorYml.getEntityOutputDir() + JAVA_PATH + mybatisPlusGeneratorYml.getEntityPackagePath().replace(".","/")+ req.buildPath("/entity")); //设置entity 和 mapper的路径 因为entity文件在单独的一个模块,mapper在reasources中 所以需要单独进行配置
map.put(OutputFile.entity, USER_DIR + mybatisPlusGeneratorYml.getEntityOutputDir() + JAVA_PATH + mybatisPlusGeneratorYml.getPackagePath().replace(".","/")+ req.buildPath("/entity"));
map.put(OutputFile.xml, USER_DIR + mybatisPlusGeneratorYml.getOutputDir() + XML_PATH + mybatisPlusGeneratorYml.getPackagePath().replace(".","/") + req.buildPath("/mapper")); map.put(OutputFile.xml, USER_DIR + mybatisPlusGeneratorYml.getOutputDir() + XML_PATH + mybatisPlusGeneratorYml.getPackagePath().replace(".","/") + req.buildPath("/mapper"));
//创建生成器
FastAutoGenerator.create(jdbcInfoYml.getUrl(), jdbcInfoYml.getUsername(), jdbcInfoYml.getPassword()) FastAutoGenerator.create(jdbcInfoYml.getUrl(), jdbcInfoYml.getUsername(), jdbcInfoYml.getPassword())
.globalConfig(builder -> { .globalConfig(builder -> {
builder.author(req.getAuthor()) // 设置作者 builder.author(req.getAuthor()) // 设置作者
.enableSpringdoc() .enableSpringdoc()
.outputDir(USER_DIR + mybatisPlusGeneratorYml.getOutputDir() + JAVA_PATH); // 指定输出目录 .outputDir(USER_DIR + mybatisPlusGeneratorYml.getOutputDir() + JAVA_PATH); // 指定输出目录
}) })
//熟路类型转换配置
.dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> { .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
int typeCode = metaInfo.getJdbcType().TYPE_CODE; int typeCode = metaInfo.getJdbcType().TYPE_CODE;
if (typeCode == Types.SMALLINT) { if (typeCode == Types.SMALLINT) {
@ -55,20 +56,22 @@ public class MybatisPlusGeneratorServiceImpl implements IMybatisPlusGeneratorSer
return typeRegistry.getColumnType(metaInfo); return typeRegistry.getColumnType(metaInfo);
})) }))
//包路径配置
.packageConfig(builder -> { .packageConfig(builder -> {
builder.parent(mybatisPlusGeneratorYml.getPackagePath()) // 设置父包名 builder.parent(mybatisPlusGeneratorYml.getPackagePath()) // 设置父包名
.controller(req.buildModule("api")) .controller(req.buildModule("api"))
.service(req.buildModule("service")) .service(req.buildModule("service"))
.serviceImpl(req.buildModule("service.impl")) .serviceImpl(req.buildModule("service.impl"))
.mapper(req.buildModule("mapper"))
.entity(req.buildModule("entity"))
.pathInfo(map); // 设置mapperXml生成路径 .pathInfo(map); // 设置mapperXml生成路径
}) })
.strategyConfig(builder -> { //设置生成的表和表的前缀
builder .strategyConfig(builder -> builder.addInclude(req.getTableNames()).addTablePrefix("tbl_").build())
.addInclude(req.getTableNames()) //设置实体开启lombok
.addTablePrefix("tbl_"); // 设置需要生成的表名 .strategyConfig(builder -> builder.entityBuilder().enableLombok().build())
}) //设置controller开启Rest风格接口
.strategyConfig(builder -> builder.controllerBuilder().enableRestStyle().build())
//设置使用自定义模板
.templateConfig(builder -> builder.entity("/templates/generator/entity.java"))
.execute(); .execute();
return true; return true;
} }

1
ds-commons/ds-config/ds-mybatisplus-support/src/main/java/com/ds/commons/support/generator/vo/GeneratorReq.java

@ -24,6 +24,7 @@ public class GeneratorReq {
@NotBlank(message = "要进行生成的表名列表不能为空") @NotBlank(message = "要进行生成的表名列表不能为空")
private List<String> tableNames; private List<String> tableNames;
public String buildModule(String p) { public String buildModule(String p) {
if (StringUtils.isEmpty(moduleName)){ if (StringUtils.isEmpty(moduleName)){
return p; return p;

166
ds-commons/ds-config/ds-mybatisplus-support/src/main/resources/templates/generator/entity.java.ftl

@ -0,0 +1,166 @@
package ${package.Entity};
<#list table.importPackages as pkg>
import ${pkg};
</#list>
<#if springdoc>
import io.swagger.v3.oas.annotations.media.Schema;
<#elseif swagger>
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
</#if>
<#if entityLombokModel>
import lombok.Getter;
import lombok.Setter;
<#if chainModel>
import lombok.experimental.Accessors;
</#if>
</#if>
<#if superEntityClass??>
<#elseif activeRecord>
<#elseif entitySerialVersionUID>
import java.io.Serial;
</#if>
/**
* <p>
* ${table.comment!}
* </p>
*
* @author ${author}
* @since ${date}
*/
<#if entityLombokModel>
@Getter
@Setter
<#if chainModel>
@Accessors(chain = true)
</#if>
</#if>
<#if table.convert>
@TableName("${schemaName}${table.name}")
</#if>
<#if springdoc>
@Schema(name = "${entity}", description = "$!{table.comment}")
<#elseif swagger>
@ApiModel(value = "${entity}对象", description = "${table.comment!}")
</#if>
<#if superEntityClass??>
public class ${entity} extends ${superEntityClass}<#if activeRecord><${entity}></#if> {
<#elseif activeRecord>
public class ${entity} extends Model<${entity}> {
<#elseif entitySerialVersionUID>
public class ${entity} implements Serializable {
<#else>
public class ${entity} {
</#if>
<#if entitySerialVersionUID>
@Serial
private static final long serialVersionUID = 1L;
</#if>
<#-- ---------- BEGIN 字段循环遍历 ---------->
<#list table.fields as field>
<#if field.keyFlag>
<#assign keyPropertyName="${field.propertyName}"/>
</#if>
<#if field.comment!?length gt 0>
<#if springdoc>
@Schema(description = "${field.comment}")
<#elseif swagger>
@ApiModelProperty("${field.comment}")
<#else>
/**
* ${field.comment}
*/
</#if>
</#if>
<#if field.keyFlag>
<#-- 主键 -->
<#if field.keyIdentityFlag>
@TableId(value = "${field.annotationColumnName}", type = IdType.AUTO)
<#elseif idType??>
@TableId(value = "${field.annotationColumnName}", type = IdType.${idType})
<#elseif field.convert>
@TableId("${field.annotationColumnName}")
</#if>
<#-- 普通字段 -->
<#elseif field.fill??>
<#-- ----- 存在字段填充设置 ----->
<#if field.convert>
@TableField(value = "${field.annotationColumnName}", fill = FieldFill.${field.fill})
<#else>
@TableField(fill = FieldFill.${field.fill})
</#if>
<#elseif field.convert>
@TableField("${field.annotationColumnName}")
</#if>
<#-- 乐观锁注解 -->
<#if field.versionField>
@Version
</#if>
<#-- 逻辑删除注解 -->
<#if field.logicDeleteField>
@TableLogic
</#if>
private ${field.propertyType} ${field.propertyName};
</#list>
<#------------ END 字段循环遍历 ---------->
<#if !entityLombokModel>
<#list table.fields as field>
<#if field.propertyType == "boolean">
<#assign getprefix="is"/>
<#else>
<#assign getprefix="get"/>
</#if>
public ${field.propertyType} ${getprefix}${field.capitalName}() {
return ${field.propertyName};
}
<#if chainModel>
public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
<#else>
public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
</#if>
this.${field.propertyName} = ${field.propertyName};
<#if chainModel>
return this;
</#if>
}
</#list>
</#if>
<#if entityColumnConstant>
<#list table.fields as field>
public static final String ${field.name?upper_case} = "${field.name}";
</#list>
</#if>
<#if activeRecord>
@Override
public Serializable pkVal() {
<#if keyPropertyName??>
return this.${keyPropertyName};
<#else>
return null;
</#if>
}
</#if>
<#if !entityLombokModel>
@Override
public String toString() {
return "${entity}{" +
<#list table.fields as field>
<#if field_index==0>
"${field.propertyName} = " + ${field.propertyName} +
<#else>
", ${field.propertyName} = " + ${field.propertyName} +
</#if>
</#list>
"}";
}
</#if>
}

167
ds-commons/ds-config/ds-mybatisplus-support/src/main/resources/templates/generator/entity.java.vm

@ -0,0 +1,167 @@
package ${package.Entity};
#foreach($pkg in ${table.importPackages})
import ${pkg};
#end
#if(${springdoc})
import io.swagger.v3.oas.annotations.media.Schema;
#elseif(${swagger})
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
#end
#if(${entityLombokModel})
import lombok.Getter;
import lombok.Setter;
#if(${chainModel})
import lombok.experimental.Accessors;
#end
#end
#if(${superEntityClass})
#elseif(${activeRecord})
#elseif(${entitySerialVersionUID})
import java.io.Serial;
#end
/**
* <p>
* $!{table.comment}
* </p>
*
* @author ${author}
* @since ${date}
*/
#if(${entityLombokModel})
@Getter
@Setter
#if(${chainModel})
@Accessors(chain = true)
#end
#end
#if(${table.convert})
@TableName("${schemaName}${table.name}")
#end
#if(${springdoc})
@Schema(name = "${entity}", description = "$!{table.comment}")
#elseif(${swagger})
@ApiModel(value = "${entity}对象", description = "$!{table.comment}")
#end
#if(${superEntityClass})
public class ${entity} extends ${superEntityClass}#if(${activeRecord})<${entity}>#end {
#elseif(${activeRecord})
public class ${entity} extends Model<${entity}> {
#elseif(${entitySerialVersionUID})
public class ${entity} implements Serializable {
#else
public class ${entity} {
#end
#if(${entitySerialVersionUID})
@Serial
private static final long serialVersionUID = 1L;
#end
## ---------- BEGIN 字段循环遍历 ----------
#foreach($field in ${table.fields})
#if(${field.keyFlag})
#set($keyPropertyName=${field.propertyName})
#end
#if("$!field.comment" != "")
#if(${springdoc})
@Schema(description = "${field.comment}")
#elseif(${swagger})
@ApiModelProperty("${field.comment}")
#else
/**
* ${field.comment}
*/
#end
#end
#if(${field.keyFlag})
## 主键
#if(${field.keyIdentityFlag})
@TableId(value = "${field.annotationColumnName}", type = IdType.AUTO)
#elseif(!$null.isNull(${idType}) && "$!idType" != "")
@TableId(value = "${field.annotationColumnName}", type = IdType.${idType})
#elseif(${field.convert})
@TableId("${field.annotationColumnName}")
#end
## 普通字段
#elseif(${field.fill})
## ----- 存在字段填充设置 -----
#if(${field.convert})
@TableField(value = "${field.annotationColumnName}", fill = FieldFill.${field.fill})
#else
@TableField(fill = FieldFill.${field.fill})
#end
#elseif(${field.convert})
@TableField("${field.annotationColumnName}")
#end
## 乐观锁注解
#if(${field.versionField})
@Version
#end
## 逻辑删除注解
#if(${field.logicDeleteField})
@TableLogic
#end
private ${field.propertyType} ${field.propertyName};
#end
## ---------- END 字段循环遍历 ----------
#if(!${entityLombokModel})
#foreach($field in ${table.fields})
#if(${field.propertyType.equals("boolean")})
#set($getprefix="is")
#else
#set($getprefix="get")
#end
public ${field.propertyType} ${getprefix}${field.capitalName}() {
return ${field.propertyName};
}
#if(${chainModel})
public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
#else
public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
#end
this.${field.propertyName} = ${field.propertyName};
#if(${chainModel})
return this;
#end
}
#end
## --foreach end---
#end
## --end of #if(!${entityLombokModel})--
#if(${entityColumnConstant})
#foreach($field in ${table.fields})
public static final String ${field.name.toUpperCase()} = "${field.name}";
#end
#end
#if(${activeRecord})
@Override
public Serializable pkVal() {
#if(${keyPropertyName})
return this.${keyPropertyName};
#else
return null;
#end
}
#end
#if(!${entityLombokModel})
@Override
public String toString() {
return "${entity}{" +
#foreach($field in ${table.fields})
#if($!{foreach.index}==0)
"${field.propertyName} = " + ${field.propertyName} +
#else
", ${field.propertyName} = " + ${field.propertyName} +
#end
#end
"}";
}
#end
}

23
pom.xml

@ -28,6 +28,9 @@
<spring.cloud.alibaba.version>2022.0.0.0-RC2</spring.cloud.alibaba.version> <spring.cloud.alibaba.version>2022.0.0.0-RC2</spring.cloud.alibaba.version>
<springdoc.version>2.1.0</springdoc.version> <springdoc.version>2.1.0</springdoc.version>
<knife4j.version>4.3.0</knife4j.version> <knife4j.version>4.3.0</knife4j.version>
<mysql.version>8.0.24</mysql.version>
<mybatisplus.version>3.5.3.2</mybatisplus.version>
<velocity.version>2.3</velocity.version>
</properties> </properties>
<dependencyManagement> <dependencyManagement>
@ -53,6 +56,26 @@
<type>pom</type> <type>pom</type>
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatisplus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybatisplus.version}</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>${velocity.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.springdoc</groupId> <groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>

Loading…
Cancel
Save