dashan
1 year ago
18 changed files with 409 additions and 2 deletions
@ -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> |
@ -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")); |
||||||
|
} |
||||||
|
} |
@ -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> |
@ -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(); |
||||||
|
} |
||||||
|
} |
@ -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)); |
||||||
|
} |
||||||
|
} |
@ -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; |
||||||
|
|
||||||
|
} |
@ -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; |
||||||
|
|
||||||
|
} |
@ -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 { |
||||||
|
} |
@ -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); |
||||||
|
} |
@ -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; |
||||||
|
} |
||||||
|
} |
@ -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; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue