Browse Source

feat(config):myabtisplus 代码生成器基础集成

dev^2
dashan 1 year ago
parent
commit
57c93aad4c
  1. 4
      .idea/compiler.xml
  2. 2
      .idea/encodings.xml
  3. 10
      ds-biz/ds-miniapps/ds-miniapps-biz/pom.xml
  4. 3
      ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/MiniAppsApplication.java
  5. 6
      ds-biz/ds-miniapps/ds-miniapps-model/pom.xml
  6. 33
      ds-commons/ds-config/ds-mybatisplus-config/pom.xml
  7. 11
      ds-commons/ds-config/ds-mybatisplus-config/src/main/java/com/ds/common/web/config/Main.java
  8. 44
      ds-commons/ds-config/ds-mybatisplus-support/pom.xml
  9. 51
      ds-commons/ds-config/ds-mybatisplus-support/src/main/java/com/ds/commons/support/generator/MybatisPlusGeneratorConfig.java
  10. 29
      ds-commons/ds-config/ds-mybatisplus-support/src/main/java/com/ds/commons/support/generator/api/MybatisPlusGeneratorApi.java
  11. 22
      ds-commons/ds-config/ds-mybatisplus-support/src/main/java/com/ds/commons/support/generator/bean/JDBCInfoYml.java
  12. 24
      ds-commons/ds-config/ds-mybatisplus-support/src/main/java/com/ds/commons/support/generator/bean/MybatisPlusGeneratorYml.java
  13. 17
      ds-commons/ds-config/ds-mybatisplus-support/src/main/java/com/ds/commons/support/generator/config/MybatisPlusGeneratorConfig.java
  14. 11
      ds-commons/ds-config/ds-mybatisplus-support/src/main/java/com/ds/commons/support/generator/service/IMybatisPlusGeneratorService.java
  15. 75
      ds-commons/ds-config/ds-mybatisplus-support/src/main/java/com/ds/commons/support/generator/service/impl/MybatisPlusGeneratorServiceImpl.java
  16. 42
      ds-commons/ds-config/ds-mybatisplus-support/src/main/java/com/ds/commons/support/generator/vo/GeneratorReq.java
  17. 25
      ds-commons/ds-config/ds-web-config/src/main/java/com/ds/commons/web/config/exection/WebGlobalExceptionHandler.java
  18. 2
      ds-commons/ds-config/pom.xml

4
.idea/compiler.xml

@ -9,11 +9,13 @@ @@ -9,11 +9,13 @@
<outputRelativeToContentRoot value="true" />
<module name="ds-utils" />
<module name="ds-miniapps-api" />
<module name="ds-mybatisplus-config" />
<module name="ds-basemodel" />
<module name="ds-web-config" />
<module name="ds-miniapps-model" />
<module name="ds-miniapps-biz" />
<module name="ds-gateway" />
<module name="ds-mybatisplus-support" />
<module name="ds-timer-task" />
</profile>
</annotationProcessing>
@ -30,6 +32,8 @@ @@ -30,6 +32,8 @@
<module name="ds-miniapps-api" options="-parameters" />
<module name="ds-miniapps-biz" options="-parameters" />
<module name="ds-miniapps-model" options="-parameters" />
<module name="ds-mybatisplus-config" options="-parameters" />
<module name="ds-mybatisplus-support" options="-parameters" />
<module name="ds-timer-task" options="-parameters" />
<module name="ds-utils" options="-parameters" />
<module name="ds-web-config" options="-parameters" />

2
.idea/encodings.xml

@ -9,6 +9,8 @@ @@ -9,6 +9,8 @@
<file url="file://$PROJECT_DIR$/ds-biz/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/ds-biz/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/ds-commons/ds-basemodel/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/ds-commons/ds-config/ds-mybatisplus-config/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/ds-commons/ds-config/ds-mybatisplus-support/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/ds-commons/ds-config/ds-web-config/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/ds-commons/ds-config/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/ds-commons/ds-config/src/main/resources" charset="UTF-8" />

10
ds-biz/ds-miniapps/ds-miniapps-biz/pom.xml

@ -41,6 +41,16 @@ @@ -41,6 +41,16 @@
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.ds.common.web.config</groupId>
<artifactId>ds-mybatisplus-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.ds.common.web.config</groupId>
<artifactId>ds-mybatisplus-support</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

3
ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/MiniAppsApplication.java

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
package com.ds.miniapps.biz;
import com.ds.commons.support.generator.config.MybatisPlusGeneratorConfig;
import com.ds.commons.web.config.exection.WebGlobalExceptionHandler;
import com.ds.commons.web.config.swagger.SwaggerConfig;
import org.springframework.boot.SpringApplication;
@ -11,7 +12,7 @@ import org.springframework.context.annotation.Import; @@ -11,7 +12,7 @@ import org.springframework.context.annotation.Import;
* @since 2023/8/27
*/
@SpringBootApplication
@Import({SwaggerConfig.class, WebGlobalExceptionHandler.class})
@Import({SwaggerConfig.class, WebGlobalExceptionHandler.class, MybatisPlusGeneratorConfig.class})
public class MiniAppsApplication {
public static void main(String[] args) {

6
ds-biz/ds-miniapps/ds-miniapps-model/pom.xml

@ -18,7 +18,11 @@ @@ -18,7 +18,11 @@
</properties>
<dependencies>
<dependency>
<groupId>com.ds.common.web.config</groupId>
<artifactId>ds-mybatisplus-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

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

@ -0,0 +1,33 @@ @@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.ds</groupId>
<artifactId>ds-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.ds.common.web.config</groupId>
<artifactId>ds-mybatisplus-config</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.24</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.2</version>
</dependency>
</dependencies>
</project>

11
ds-commons/ds-config/ds-mybatisplus-config/src/main/java/com/ds/common/web/config/Main.java

@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
package com.ds.common.web.config;
/**
* @author ds
* @since 2023/9/2
*/
public class Main {
public static void main(String[] args) {
System.out.println(System.getProperty("user.dir"));
}
}

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

@ -0,0 +1,44 @@ @@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.ds</groupId>
<artifactId>ds-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.ds.common.web.config</groupId>
<artifactId>ds-mybatisplus-support</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency>
<groupId>com.ds.common.web.config</groupId>
<artifactId>ds-mybatisplus-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.ds.common.web.config</groupId>
<artifactId>ds-web-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

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

@ -0,0 +1,51 @@ @@ -0,0 +1,51 @@
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();
}
}

29
ds-commons/ds-config/ds-mybatisplus-support/src/main/java/com/ds/commons/support/generator/api/MybatisPlusGeneratorApi.java

@ -0,0 +1,29 @@ @@ -0,0 +1,29 @@
package com.ds.commons.support.generator.api;
import com.ds.commons.basemodel.response.R;
import com.ds.commons.support.generator.service.IMybatisPlusGeneratorService;
import com.ds.commons.support.generator.vo.GeneratorReq;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author ds
* @since 2023/9/4
*/
@RestController
@RequestMapping("/support")
@AllArgsConstructor
@Tag(name="代码生成器",description = "代码生成器,用于生成entity,service,controller,xml 文件")
public class MybatisPlusGeneratorApi {
private final IMybatisPlusGeneratorService mybatisPlusGeneratorService;
@PostMapping("/generator")
public R<Boolean> generator(@RequestBody GeneratorReq req){
return R.success(mybatisPlusGeneratorService.generator(req));
}
}

22
ds-commons/ds-config/ds-mybatisplus-support/src/main/java/com/ds/commons/support/generator/bean/JDBCInfoYml.java

@ -0,0 +1,22 @@ @@ -0,0 +1,22 @@
package com.ds.commons.support.generator.bean;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author ds
* @since 2023/9/4
*/
@Data
@Component
@ConfigurationProperties(prefix = "spring.datasource")
public class JDBCInfoYml {
private String url;
private String username;
private String password;
}

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

@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
package com.ds.commons.support.generator.bean;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author ds
* @since 2023/9/4
*/
@Data
@Component
@ConfigurationProperties(prefix = "mp.generator")
public class MybatisPlusGeneratorYml {
private String outputDir;
private String entityOutputDir;
private String packagePath;
private String entityPackagePath;
}

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

@ -0,0 +1,17 @@ @@ -0,0 +1,17 @@
package com.ds.commons.support.generator.config;
import com.ds.commons.support.generator.api.MybatisPlusGeneratorApi;
import com.ds.commons.support.generator.bean.JDBCInfoYml;
import com.ds.commons.support.generator.bean.MybatisPlusGeneratorYml;
import com.ds.commons.support.generator.service.impl.MybatisPlusGeneratorServiceImpl;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
/**
* @author ds
* @since 2023/9/4
*/
@Component
@Import({JDBCInfoYml.class, MybatisPlusGeneratorYml.class, MybatisPlusGeneratorServiceImpl.class, MybatisPlusGeneratorApi.class})
public class MybatisPlusGeneratorConfig {
}

11
ds-commons/ds-config/ds-mybatisplus-support/src/main/java/com/ds/commons/support/generator/service/IMybatisPlusGeneratorService.java

@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
package com.ds.commons.support.generator.service;
import com.ds.commons.support.generator.vo.GeneratorReq;
/**
* @author ds
* @since 2023/9/4
*/
public interface IMybatisPlusGeneratorService {
boolean generator(GeneratorReq req);
}

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

@ -0,0 +1,75 @@ @@ -0,0 +1,75 @@
package com.ds.commons.support.generator.service.impl;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.ds.commons.support.generator.bean.JDBCInfoYml;
import com.ds.commons.support.generator.bean.MybatisPlusGeneratorYml;
import com.ds.commons.support.generator.service.IMybatisPlusGeneratorService;
import com.ds.commons.support.generator.vo.GeneratorReq;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
/**
* @author ds
* @since 2023/9/4
*/
@Service
@AllArgsConstructor
public class MybatisPlusGeneratorServiceImpl implements IMybatisPlusGeneratorService {
private final MybatisPlusGeneratorYml mybatisPlusGeneratorYml;
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 JAVA_PATH ="/src/main/java/";
private static final String XML_PATH = "/src/main/resources/";
@Override
public boolean generator(GeneratorReq req) {
Map<OutputFile,String> map = new HashMap<>();
map.put(OutputFile.entity, USER_DIR + mybatisPlusGeneratorYml.getEntityOutputDir() + JAVA_PATH + mybatisPlusGeneratorYml.getEntityPackagePath().replace(".","/")+ req.buildPath("/entity"));
map.put(OutputFile.xml, USER_DIR + mybatisPlusGeneratorYml.getOutputDir() + XML_PATH + mybatisPlusGeneratorYml.getPackagePath().replace(".","/") + req.buildPath("/mapper"));
FastAutoGenerator.create(jdbcInfoYml.getUrl(), jdbcInfoYml.getUsername(), jdbcInfoYml.getPassword())
.globalConfig(builder -> {
builder.author(req.getAuthor()) // 设置作者
.enableSpringdoc()
.outputDir(USER_DIR + mybatisPlusGeneratorYml.getOutputDir() + JAVA_PATH); // 指定输出目录
})
.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(mybatisPlusGeneratorYml.getPackagePath()) // 设置父包名
.controller(req.buildModule("api"))
.service(req.buildModule("service"))
.serviceImpl(req.buildModule("service.impl"))
.mapper(req.buildModule("mapper"))
.entity(req.buildModule("entity"))
.pathInfo(map); // 设置mapperXml生成路径
})
.strategyConfig(builder -> {
builder
.addInclude(req.getTableNames())
.addTablePrefix("tbl_"); // 设置需要生成的表名
})
.execute();
return true;
}
}

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

@ -0,0 +1,42 @@ @@ -0,0 +1,42 @@
package com.ds.commons.support.generator.vo;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import java.util.List;
/**
* @author ds
* @since 2023/9/4
*/
@Data
@Schema(description = "代码生成传参")
public class GeneratorReq {
@Schema(description = "作者",requiredMode = Schema.RequiredMode.NOT_REQUIRED,defaultValue = "ds")
private String author="ds";
@Schema(description = "子模块名称",requiredMode = Schema.RequiredMode.NOT_REQUIRED,defaultValue = "")
private String moduleName;
@Schema(description = "要进行生成的表名列表")
@NotBlank(message = "要进行生成的表名列表不能为空")
private List<String> tableNames;
public String buildModule(String p) {
if (StringUtils.isEmpty(moduleName)){
return p;
}else{
return p+"."+moduleName;
}
}
public String buildPath(String p) {
if (StringUtils.isEmpty(moduleName)){
return p;
}else{
return p+"/"+moduleName;
}
}
}

25
ds-commons/ds-config/ds-web-config/src/main/java/com/ds/commons/web/config/exection/WebGlobalExceptionHandler.java

@ -14,6 +14,7 @@ import org.springframework.web.HttpRequestMethodNotSupportedException; @@ -14,6 +14,7 @@ import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@ -131,4 +132,28 @@ public class WebGlobalExceptionHandler { @@ -131,4 +132,28 @@ public class WebGlobalExceptionHandler {
log.error(request.getMethod()+"-"+request.getRequestURI() + ":自定义内部异常",e);
return R.failure(e);
}
/**
* 处理Error异常
*
* @param req
* @param e
* @return
*/
@ExceptionHandler(value = Error.class)
@ResponseBody
public R<Void> errorHandler(HttpServletRequest req, Error e) {
log.error("URL : " + req.getRequestURL().toString());
log.error("HTTP_METHOD : " + req.getMethod());
log.error("未知Error!原因是:", e);
// 发送警报通知
// sendAlertNotification();
// 执行清理操作(关闭数据库连接、释放资源等)
// cleanup();
// 终止应用程序
System.exit(1);
return R.failure();
}
}

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

@ -13,6 +13,8 @@ @@ -13,6 +13,8 @@
<packaging>pom</packaging>
<modules>
<module>ds-web-config</module>
<module>ds-mybatisplus-config</module>
<module>ds-mybatisplus-support</module>
</modules>
<properties>

Loading…
Cancel
Save