feat: 添加代码生成器

This commit is contained in:
landaiqing
2024-05-11 21:52:18 +08:00
parent fce3423ad7
commit 69a589fe5c
108 changed files with 2606 additions and 69 deletions

View File

@@ -0,0 +1,30 @@
<?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>
<groupId>com.schisandra</groupId>
<artifactId>schisandra-cloud-storage-gen</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>schisandra-cloud-storage-gen</name>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.schisandra</groupId>
<artifactId>easy-gen-code-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,58 @@
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.schisandra.core.config.GenConfig;
import com.schisandra.core.config.MapperConfig;
import com.schisandra.core.core.sdk.PostCurFiledContextAware;
import com.schisandra.core.entity.TableInfo;
import com.schisandra.core.utils.MySQLToJavaTypeConverter;
import org.apache.velocity.VelocityContext;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class BizPutContextHandler implements PostCurFiledContextAware {
public static String underlineToHump(String str, boolean removePre) {
if (removePre) {
str = str.substring(str.indexOf("_"));
}
str = str.toLowerCase();
Pattern compile = Pattern.compile("_[a-z]");
Matcher matcher = compile.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, matcher.group(0).toUpperCase().replace("_", ""));
}
matcher.appendTail(sb);
return StrUtil.upperFirst(sb.toString());
}
private static String convert(String str) {
if (str == null || str.isEmpty()) {
return str;
}
char firstChar = str.charAt(0);
char lowerFirstChar = Character.toLowerCase(firstChar);
return lowerFirstChar + str.substring(1);
}
@Override
public void doAware(GenConfig genConfig, MapperConfig mapperConfig, VelocityContext context) {
// 处理导包标志位
List<TableInfo> files = JSONObject.parseArray(JSONObject.toJSONString(context.get("fields")), TableInfo.class);
for (TableInfo tableInfo : files) {
MySQLToJavaTypeConverter.Item typeInfo = tableInfo.getTypeInfo(tableInfo.getType());
if (typeInfo.isFlag()) {
context.put("import" + typeInfo.getType(), true);
}
}
context.put("modelName", underlineToHump(genConfig.getJdbc().getTableName(), Convert.toBool(context.get("removePre"))));
context.put("_modelName", convert(context.get("modelName").toString()));
context.put("tableName", genConfig.getJdbc().getTableName());
}
}

View File

@@ -0,0 +1,9 @@
import com.schisandra.core.core.CodeGenerationCode;
public class GenCodeMain {
public static void main(String[] args) {
CodeGenerationCode.doGenCode(new BizPutContextHandler());
}
}

View File

@@ -0,0 +1,69 @@
# auth模块映射关系 ${module} 占位符
# 模板文件和生成类的映射关系 多个文件 数组形式配置
mappers:
-
- fileId: 001
template: genCode/template/DemoDTO.java.vm
filePath: /schisandra-cloud-storage-${module}/schisandra-cloud-storage-${module}-application/schisandra-cloud-storage-${module}-application-controller/src/main/java/com/schisandra/${module}/application/dto
name: ${modelName}DTO
ext: java
- fileId: 002
template: genCode/template/DemoController.java.vm
filePath: /schisandra-cloud-storage-${module}/schisandra-cloud-storage-${module}-application/schisandra-cloud-storage-${module}-application-controller/src/main/java/com/schisandra/${module}/application/controller
name: ${modelName}Controller
ext: java
- fileId: 003
template: genCode/template/DemoDTOConverter.java.vm
filePath: /schisandra-cloud-storage-${module}/schisandra-cloud-storage-${module}-application/schisandra-cloud-storage-${module}-application-controller/src/main/java/com/schisandra/${module}/application/convert
name: ${modelName}DTOConverter
ext: java
- fileId: 004
template: genCode/template/DemoBO.java.vm
filePath: /schisandra-cloud-storage-${module}/schisandra-cloud-storage-${module}-domain/src/main/java/com/schisandra/${module}/domain/entity
name: ${modelName}BO
ext: java
- fileId: 005
template: genCode/template/DemoDomainService.java.vm
filePath: /schisandra-cloud-storage-${module}/schisandra-cloud-storage-${module}-domain/src/main/java/com/schisandra/${module}/domain/service
name: ${modelName}DomainService
ext: java
- fileId: 006
template: genCode/template/DemoDomainServiceImpl.java.vm
filePath: /schisandra-cloud-storage-${module}/schisandra-cloud-storage-${module}-domain/src/main/java/com/schisandra/${module}/domain/service/impl
name: ${modelName}DomainServiceImpl
ext: java
- fileId: 007
template: genCode/template/DemoBOConverter.java.vm
filePath: /schisandra-cloud-storage-${module}/schisandra-cloud-storage-${module}-domain/src/main/java/com/schisandra/${module}/domain/convert
name: ${modelName}BOConverter
ext: java
- fileId: 008
template: genCode/template/DemoService.java.vm
filePath: /schisandra-cloud-storage-${module}/schisandra-cloud-storage-${module}-infra/src/main/java/com/schisandra/${module}/infra/basic/service
name: ${modelName}Service
ext: java
- fileId: 009
template: genCode/template/DemoTable.java.vm
filePath: /schisandra-cloud-storage-${module}/schisandra-cloud-storage-${module}-infra/src/main/java/com/schisandra/${module}/infra/basic/entity
name: ${modelName}
ext: java
- fileId: 010
template: genCode/template/DemoServiceImpl.java.vm
filePath: /schisandra-cloud-storage-${module}/schisandra-cloud-storage-${module}-infra/src/main/java/com/schisandra/${module}/infra/basic/service/impl
name: ${modelName}ServiceImpl
ext: java
- fileId: 011
template: genCode/template/DemoDao.java.vm
filePath: /schisandra-cloud-storage-${module}/schisandra-cloud-storage-${module}-infra/src/main/java/com/schisandra/${module}/infra/basic/mapper
name: ${modelName}Dao
ext: java
- fileId: 012
template: genCode/template/DemoXml.xml.vm
filePath: /schisandra-cloud-storage-${module}/schisandra-cloud-storage-${module}-infra/src/main/resources/mapper
name: ${modelName}Dao
ext: xml

View File

@@ -0,0 +1,33 @@
# 使用步骤
# 01 替换数据库连接信息及对应表 jdbc:*
# 02 替换模板映射关系 mapperInfos
# 03 替换作者信息 params:loser
# 04 替换模块 params:module
# 05 替换 controller 前缀 params:api
# 数据库连接信息
jdbc:
dbName: schisandra-cloud-storage
tableName: subject_liked
url: jdbc:mysql://1.95.0.111:3306/
username: schisandra
password: LDQ20020618xxx
driver: com.mysql.cj.jdbc.Driver
# 使用的模板与生成文件映射给关系
mapperInfos: genCode/subjectLikedMapper.yml
# 全局参数
params:
# 作者
author: schisandra
# 模块
module: subject
# controller 通用前缀
api: /subjectLiked
# 生成对象是否移除前缀
removePre: false
# 使用内置函数赋值给变量 FunctionUtils 中替换
genDate: now()
# win 需要补充模板具体目录
templateBasePath: E:/JavaProjects_IDEA/schisandra-cloud-storage/schisandra-cloud-storage-gen/src/main/resources/

View File

@@ -0,0 +1,24 @@
package com.schisandra.${module}.domain.entity;
import lombok.Data;
import java.io.Serializable;
/**
* ${tableComment} bo
*
* @author ${author}
* @since ${genDate}
*/
@Data
public class ${modelName}BO implements Serializable {
#foreach($field in $fields)
/**
* ${field.comment}
*/
private $field.type $field.name;
#end
}

View File

@@ -0,0 +1,21 @@
package com.schisandra.${module}.domain.convert;
${module}.domain.entity.${modelName}BO;
${module}.infra.basic.entity.${modelName};
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
/**
* ${tableComment} bo转换器
*
* @author ${author}
* @since ${genDate}
*/
@Mapper
public interface ${modelName}BOConverter {
${modelName}BOConverter INSTANCE = Mappers.getMapper(${modelName}BOConverter.class);
${modelName} convertBOToEntity(${modelName}BO ${_modelName}BO);
}

View File

@@ -0,0 +1,97 @@
package com.schisandra.${module}.application.controller;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
${module}.application.convert.${modelName}DTOConverter;
${module}.application.dto.${modelName}DTO;
${module}.common.entity.Result;
${module}.domain.entity.${modelName}BO;
${module}.domain.service.${modelName}DomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* ${tableComment} controller
*
* @author ${author}
* @since ${genDate}
*/
@RestController
@RequestMapping("${api}/")
@Slf4j
public class ${modelName}Controller {
@Resource
private ${modelName}DomainService ${_modelName}DomainService;
/**
* 新增${tableComment}
*/
@RequestMapping("add")
public Result<Boolean> add(@RequestBody ${modelName}DTO ${_modelName}DTO) {
try {
if (log.isInfoEnabled()) {
log.info("${modelName}Controller.add.dto:{}", JSON.toJSONString(${_modelName}DTO));
}
#foreach($field in $fields)
Preconditions.checkNotNull(${_modelName}DTO.get${field.upName}(), "${field.comment}不能为空");
#end
${modelName}BO ${modelName}BO = ${modelName}DTOConverter.INSTANCE.convertDTOToBO(${_modelName}DTO);
return Result.ok(${_modelName}DomainService.add(${modelName}BO));
} catch (Exception e) {
log.error("${modelName}Controller.register.error:{}", e.getMessage(), e);
return Result.fail("新增${tableComment}失败");
}
}
/**
* 修改${tableComment}
*/
@RequestMapping("update")
public Result<Boolean> update(@RequestBody ${modelName}DTO ${_modelName}DTO) {
try {
if (log.isInfoEnabled()) {
log.info("${modelName}Controller.update.dto:{}", JSON.toJSONString(${_modelName}DTO));
}
#foreach($field in $fields)
Preconditions.checkNotNull(${_modelName}DTO.get${field.upName}(), "${field.comment}不能为空");
#end
${modelName}BO ${_modelName}BO = ${modelName}DTOConverter.INSTANCE.convertDTOToBO(${_modelName}DTO);
return Result.ok(${_modelName}DomainService.update(${_modelName}BO));
} catch (Exception e) {
log.error("${modelName}Controller.update.error:{}", e.getMessage(), e);
return Result.fail("更新${tableComment}信息失败");
}
}
/**
* 删除${tableComment}
*/
@RequestMapping("delete")
public Result<Boolean> delete(@RequestBody ${modelName}DTO ${_modelName}DTO) {
try {
if (log.isInfoEnabled()) {
log.info("${modelName}Controller.delete.dto:{}", JSON.toJSONString(${_modelName}DTO));
}
#foreach($field in $fields)
Preconditions.checkNotNull(${_modelName}DTO.get${field.upName}(), "${field.comment}不能为空");
#end
${modelName}BO ${_modelName}BO = ${modelName}DTOConverter.INSTANCE.convertDTOToBO(${_modelName}DTO);
return Result.ok(${_modelName}DomainService.delete(${_modelName}BO));
} catch (Exception e) {
log.error("${modelName}Controller.delete.error:{}", e.getMessage(), e);
return Result.fail("删除${tableComment}信息失败");
}
}
}

View File

@@ -0,0 +1,24 @@
package com.schisandra.${module}.application.dto;
import lombok.Data;
import java.io.Serializable;
/**
* ${tableComment} dto
*
* @author ${author}
* @since ${genDate}
*/
@Data
public class ${modelName}DTO implements Serializable {
#foreach($field in $fields)
/**
* ${field.comment}
*/
private $field.type $field.name;
#end
}

View File

@@ -0,0 +1,21 @@
package com.schisandra.${module}.application.convert;
${module}.application.dto.${modelName}DTO;
${module}.domain.entity.${modelName}BO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
/**
* ${tableComment} dto转换器
*
* @author ${author}
* @since ${genDate}
*/
@Mapper
public interface ${modelName}DTOConverter {
${modelName}DTOConverter INSTANCE = Mappers.getMapper(${modelName}DTOConverter.class);
${modelName}BO convertDTOToBO(${modelName}DTO ${_modelName}DTO);
}

View File

@@ -0,0 +1,17 @@
package com.schisandra.${module}.infra.basic.mapper;
${module}.infra.basic.entity.${modelName};
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;
/**
* ${tableComment} 表数据库访问层
*
* @author ${author}
* @since ${genDate}
*/
@Repository
public interface ${modelName}Dao extends BaseMapper<${modelName}> {
}

View File

@@ -0,0 +1,26 @@
package com.schisandra.${module}.domain.service;${module}.domain.entity.${modelName}BO;
/**
* ${tableComment} 领域service
*
* @author ${author}
* @since ${genDate}
*/
public interface ${modelName}DomainService {
/**
* 添加 ${tableComment} 信息
*/
Boolean add(${modelName}BO ${_modelName}BO);
/**
* 更新 ${tableComment} 信息
*/
Boolean update(${modelName}BO ${_modelName}BO);
/**
* 删除 ${tableComment} 信息
*/
Boolean delete(${modelName}BO ${_modelName}BO);
}

View File

@@ -0,0 +1,48 @@
package com.schisandra.${module}.domain.service.impl;
${module}.common.enums.IsDeletedFlagEnum;
${module}.domain.convert.${modelName}BOConverter;
${module}.domain.entity.${modelName}BO;
${module}.domain.service.${modelName}DomainService;
${module}.infra.basic.entity.${modelName};
${module}.infra.basic.service.${modelName}Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* ${tableComment} 领域service实现了
*
* @author ${author}
* @since ${genDate}
*/
@Service
@Slf4j
public class ${modelName}DomainServiceImpl implements ${modelName}DomainService {
@Resource
private ${modelName}Service ${_modelName}Service;
@Override
public Boolean add(${modelName}BO ${_modelName}BO) {
${modelName} ${_modelName} = ${modelName}BOConverter.INSTANCE.convertBOToEntity(${_modelName}BO);
${_modelName}.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
return ${_modelName}Service.insert(${_modelName}) > 0;
}
@Override
public Boolean update(${modelName}BO ${_modelName}BO) {
${modelName} ${_modelName} = ${modelName}BOConverter.INSTANCE.convertBOToEntity(${_modelName}BO);
return ${_modelName}Service.update(${_modelName}) > 0;
}
@Override
public Boolean delete(${modelName}BO ${_modelName}BO) {
${modelName} ${_modelName} = new ${modelName}();
${_modelName}.setId(${_modelName}BO.getId());
${_modelName}.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
return ${_modelName}Service.update(${_modelName}) > 0;
}
}

View File

@@ -0,0 +1,48 @@
package com.schisandra.${module}.infra.basic.service;${module}.infra.basic.entity.${modelName};
/**
* ${tableComment} 表服务接口
*
* @author ${author}
* @since ${genDate}
*/
public interface ${modelName}Service {
/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
${modelName} queryById(Long id);
/**
* 新增数据
*
* @param ${_modelName} 实例对象
* @return 实例对象
*/
int insert(${modelName} ${_modelName});
/**
* 修改数据
*
* @param ${_modelName} 实例对象
* @return 实例对象
*/
int update(${modelName} ${_modelName});
/**
* 通过主键删除数据
*
* @param id 主键
* @return 是否成功
*/
boolean deleteById(Long id);
/**
* 根据条件查询角色
*/
${modelName} queryByCondition(${modelName} ${_modelName});
}

View File

@@ -0,0 +1,87 @@
package com.schisandra.${module}.infra.basic.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
${module}.infra.basic.entity.${modelName};
${module}.infra.basic.mapper.${modelName}Dao;
${module}.infra.basic.service.${modelName}Service;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Objects;
/**
* ${tableComment} 表服务实现类
*
* @author ${author}
* @since ${genDate}
*/
@Service("${modelName}Service")
public class ${modelName}ServiceImpl implements ${modelName}Service {
@Resource
private ${modelName}Dao ${_modelName}Dao;
/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
@Override
public ${modelName} queryById(Long id) {
return this.${_modelName}Dao.selectById(id);
}
/**
* 新增数据
*
* @param ${_modelName} 实例对象
* @return 实例对象
*/
@Override
public int insert(${modelName} ${_modelName}) {
return this.${_modelName}Dao.insert(${_modelName});
}
/**
* 修改数据
*
* @param ${_modelName} 实例对象
* @return 实例对象
*/
@Override
public int update(${modelName} ${_modelName}) {
return this.${_modelName}Dao.updateById(${_modelName});
}
/**
* 通过主键删除数据
*
* @param id 主键
* @return 是否成功
*/
@Override
public boolean deleteById(Long id) {
return this.${_modelName}Dao.deleteById(id) > 0;
}
/**
* 条件查询
*
* @param ${_modelName} 条件
* @return 实例对象
*/
@Override
public ${modelName} queryByCondition(${modelName} ${_modelName}) {
LambdaQueryWrapper<${modelName}> queryWrapper = Wrappers.<${modelName}>lambdaQuery()
#foreach($field in $fields)
.eq(Objects.nonNull(${_modelName}.get${field.upName}()), ${modelName}::get${field.upName}, ${_modelName}.get${field.upName}())
#end
;
return ${_modelName}Dao.selectOne(queryWrapper);
}
}

View File

@@ -0,0 +1,33 @@
package com.schisandra.${module}.infra.basic.entity;
import lombok.Data;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
/**
* ${tableComment} 实体类
*
* @author ${author}
* @since ${genDate}
*/
@Data
@TableName("${tableName}")
public class ${modelName} implements Serializable {
#foreach($field in $fields)
/**
* ${field.comment}
*/
#if($field.keyType=='PRI')
@TableId(value = "`$field.col`", type = IdType.AUTO)
#end
#if($field.keyType!='PRI')
@TableField("`$field.col`")
#end
private $field.type $field.name;
#end
}

View File

@@ -0,0 +1,16 @@
<?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.schisandra.${module}.infra.basic.mapper.${modelName}Dao">
<resultMap id="BaseResultMap" type="com.schisandra.${module}.infra.basic.entity.${modelName}">
#foreach($field in $fields)
#if($field.keyType=='PRI')
<id column="${field.col}" jdbcType="${field.myBatisType}" property="${field.name}"/>
#end
#if($field.keyType!='PRI')
<result column="${field.col}" jdbcType="${field.myBatisType}" property="${field.name}"/>
#end
#end
</resultMap>
</mapper>