Commit 2210ed5cf876ce80792c8d28cba80988b85fcd7d
1 parent
763e2079
增加 service 模板
Showing
10 changed files
with
97 additions
and
195 deletions
ruoyi-generator/src/main/java/com/ruoyi/generator/util/VelocityInitializer.java deleted
| 1 | -package com.ruoyi.generator.util; | |
| 2 | - | |
| 3 | -import java.util.Properties; | |
| 4 | - | |
| 5 | -import org.apache.velocity.app.Velocity; | |
| 6 | -import com.ruoyi.common.constant.Constants; | |
| 7 | - | |
| 8 | -/** | |
| 9 | - * VelocityEngine工厂 | |
| 10 | - * | |
| 11 | - * @author RuoYi | |
| 12 | - */ | |
| 13 | -public class VelocityInitializer { | |
| 14 | - /** | |
| 15 | - * 初始化vm方法 | |
| 16 | - */ | |
| 17 | - public static void initVelocity() { | |
| 18 | - Properties p = new Properties(); | |
| 19 | - try { | |
| 20 | - // 加载classpath目录下的vm文件 | |
| 21 | - p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); | |
| 22 | - // 定义字符集 | |
| 23 | - p.setProperty(Velocity.ENCODING_DEFAULT, Constants.UTF8); | |
| 24 | - p.setProperty(Velocity.OUTPUT_ENCODING, Constants.UTF8); | |
| 25 | - // 初始化Velocity引擎,指定配置Properties | |
| 26 | - Velocity.init(p); | |
| 27 | - } catch (Exception e) { | |
| 28 | - throw new RuntimeException(e); | |
| 29 | - } | |
| 30 | - } | |
| 31 | -} |
ruoyi-generator/src/main/resources/vm/java/domain.java.vm deleted
| 1 | -package ${packageName}.domain; | |
| 2 | - | |
| 3 | -#foreach ($import in $importList) | |
| 4 | -import ${import}; | |
| 5 | -#end | |
| 6 | -import org.apache.commons.lang3.builder.ToStringBuilder; | |
| 7 | -import org.apache.commons.lang3.builder.ToStringStyle; | |
| 8 | -import com.ruoyi.common.annotation.Excel; | |
| 9 | -#if($table.crud) | |
| 10 | -import com.ruoyi.common.core.domain.BaseEntity; | |
| 11 | -#elseif($table.tree) | |
| 12 | -import com.ruoyi.common.core.domain.TreeEntity; | |
| 13 | -#end | |
| 14 | - | |
| 15 | -/** | |
| 16 | - * ${functionName}对象 ${tableName} | |
| 17 | - * | |
| 18 | - * @author ${author} | |
| 19 | - * @date ${datetime} | |
| 20 | - */ | |
| 21 | -#if($table.crud) | |
| 22 | -#set($Entity="BaseEntity") | |
| 23 | -#elseif($table.tree) | |
| 24 | -#set($Entity="TreeEntity") | |
| 25 | -#end | |
| 26 | -public class ${ClassName} extends ${Entity} | |
| 27 | -{ | |
| 28 | - private static final long serialVersionUID = 1L; | |
| 29 | - | |
| 30 | -#foreach ($column in $columns) | |
| 31 | -#if(!$table.isSuperColumn($column.javaField)) | |
| 32 | - /** $column.columnComment */ | |
| 33 | -#if($column.list) | |
| 34 | -#set($parentheseIndex=$column.columnComment.indexOf("(")) | |
| 35 | -#if($parentheseIndex != -1) | |
| 36 | -#set($comment=$column.columnComment.substring(0, $parentheseIndex)) | |
| 37 | -#else | |
| 38 | -#set($comment=$column.columnComment) | |
| 39 | -#end | |
| 40 | -#if($parentheseIndex != -1) | |
| 41 | - @Excel(name = "${comment}", readConverterExp = "$column.readConverterExp()") | |
| 42 | -#elseif($column.javaType == 'Date') | |
| 43 | - @JsonFormat(pattern = "yyyy-MM-dd") | |
| 44 | - @Excel(name = "${comment}", width = 30, dateFormat = "yyyy-MM-dd") | |
| 45 | -#else | |
| 46 | - @Excel(name = "${comment}") | |
| 47 | -#end | |
| 48 | -#end | |
| 49 | - private $column.javaType $column.javaField; | |
| 50 | - | |
| 51 | -#end | |
| 52 | -#end | |
| 53 | -#foreach ($column in $columns) | |
| 54 | -#if(!$table.isSuperColumn($column.javaField)) | |
| 55 | -#if($column.javaField.length() > 2 && $column.javaField.substring(1,2).matches("[A-Z]")) | |
| 56 | -#set($AttrName=$column.javaField) | |
| 57 | -#else | |
| 58 | -#set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)}) | |
| 59 | -#end | |
| 60 | - public void set${AttrName}($column.javaType $column.javaField) | |
| 61 | - { | |
| 62 | - this.$column.javaField = $column.javaField; | |
| 63 | - } | |
| 64 | - | |
| 65 | - public $column.javaType get${AttrName}() | |
| 66 | - { | |
| 67 | - return $column.javaField; | |
| 68 | - } | |
| 69 | -#end | |
| 70 | -#end | |
| 71 | - | |
| 72 | - @Override | |
| 73 | - public String toString() { | |
| 74 | - return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) | |
| 75 | -#foreach ($column in $columns) | |
| 76 | -#if($column.javaField.length() > 2 && $column.javaField.substring(1,2).matches("[A-Z]")) | |
| 77 | -#set($AttrName=$column.javaField) | |
| 78 | -#else | |
| 79 | -#set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)}) | |
| 80 | -#end | |
| 81 | - .append("${column.javaField}", get${AttrName}()) | |
| 82 | -#end | |
| 83 | - .toString(); | |
| 84 | - } | |
| 85 | -} |
ruoyi-generator/src/main/resources/vm/java/mapper.java.vm deleted
| 1 | -package ${packageName}.mapper; | |
| 2 | - | |
| 3 | -import java.util.List; | |
| 4 | -import ${packageName}.domain.${ClassName}; | |
| 5 | - | |
| 6 | -/** | |
| 7 | - * ${functionName}Mapper接口 | |
| 8 | - * | |
| 9 | - * @author ${author} | |
| 10 | - * @date ${datetime} | |
| 11 | - */ | |
| 12 | -public interface ${ClassName}Mapper | |
| 13 | -{ | |
| 14 | - /** | |
| 15 | - * 查询${functionName} | |
| 16 | - * | |
| 17 | - * @param ${pkColumn.javaField} ${functionName}ID | |
| 18 | - * @return ${functionName} | |
| 19 | - */ | |
| 20 | - public ${ClassName} select${ClassName}ById(${pkColumn.javaType} ${pkColumn.javaField}); | |
| 21 | - | |
| 22 | - /** | |
| 23 | - * 查询${functionName}列表 | |
| 24 | - * | |
| 25 | - * @param ${className} ${functionName} | |
| 26 | - * @return ${functionName}集合 | |
| 27 | - */ | |
| 28 | - public List<${ClassName}> select${ClassName}List(${ClassName} ${className}); | |
| 29 | - | |
| 30 | - /** | |
| 31 | - * 新增${functionName} | |
| 32 | - * | |
| 33 | - * @param ${className} ${functionName} | |
| 34 | - * @return 结果 | |
| 35 | - */ | |
| 36 | - public int insert${ClassName}(${ClassName} ${className}); | |
| 37 | - | |
| 38 | - /** | |
| 39 | - * 修改${functionName} | |
| 40 | - * | |
| 41 | - * @param ${className} ${functionName} | |
| 42 | - * @return 结果 | |
| 43 | - */ | |
| 44 | - public int update${ClassName}(${ClassName} ${className}); | |
| 45 | - | |
| 46 | - /** | |
| 47 | - * 删除${functionName} | |
| 48 | - * | |
| 49 | - * @param ${pkColumn.javaField} ${functionName}ID | |
| 50 | - * @return 结果 | |
| 51 | - */ | |
| 52 | - public int delete${ClassName}ById(${pkColumn.javaType} ${pkColumn.javaField}); | |
| 53 | - | |
| 54 | - /** | |
| 55 | - * 批量删除${functionName} | |
| 56 | - * | |
| 57 | - * @param ${pkColumn.javaField}s 需要删除的数据ID | |
| 58 | - * @return 结果 | |
| 59 | - */ | |
| 60 | - public int delete${ClassName}ByIds(${pkColumn.javaType}[] ${pkColumn.javaField}s); | |
| 61 | -} |
src/main/java/cn/iocoder/dashboard/DashboardApplication.java
| ... | ... | @@ -8,11 +8,12 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 8 | 8 | @EnableAdminServer // TODO 芋艿:需要迁移出去 |
| 9 | 9 | public class DashboardApplication { |
| 10 | 10 | |
| 11 | - public static void main(String[] args) { | |
| 12 | - // 设置读取的配置文件 | |
| 13 | - System.setProperty("spring.config.name", "application,db"); | |
| 11 | +// static { | |
| 12 | +// // 设置读取的配置文件 | |
| 13 | +// System.setProperty("spring.config.name", "application,db"); | |
| 14 | +// } | |
| 14 | 15 | |
| 15 | - // 启动 Spring Boot | |
| 16 | + public static void main(String[] args) { | |
| 16 | 17 | SpringApplication.run(DashboardApplication.class, args); |
| 17 | 18 | } |
| 18 | 19 | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/system/controller/dept/vo/dept/SysDeptCreateReqVO.java
| ... | ... | @@ -3,9 +3,11 @@ package cn.iocoder.dashboard.modules.system.controller.dept.vo.dept; |
| 3 | 3 | import io.swagger.annotations.ApiModel; |
| 4 | 4 | import lombok.Data; |
| 5 | 5 | import lombok.EqualsAndHashCode; |
| 6 | +import lombok.ToString; | |
| 6 | 7 | |
| 7 | 8 | @ApiModel("部门创建 Request VO") |
| 8 | 9 | @Data |
| 9 | 10 | @EqualsAndHashCode(callSuper = true) |
| 11 | +@ToString(callSuper = true) | |
| 10 | 12 | public class SysDeptCreateReqVO extends SysDeptBaseVO { |
| 11 | 13 | } | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java
| ... | ... | @@ -10,14 +10,16 @@ import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX; |
| 10 | 10 | import cn.iocoder.dashboard.framework.mybatis.core.query.QueryWrapperX; |
| 11 | 11 | import cn.iocoder.dashboard.modules.tool.dal.mysql.dataobject.codegen.ToolCodegenColumnDO; |
| 12 | 12 | import cn.iocoder.dashboard.modules.tool.dal.mysql.dataobject.codegen.ToolCodegenTableDO; |
| 13 | +import cn.iocoder.dashboard.util.collection.CollectionUtils; | |
| 13 | 14 | import cn.iocoder.dashboard.util.date.DateUtils; |
| 14 | 15 | import org.springframework.stereotype.Component; |
| 15 | 16 | |
| 16 | -import java.util.Date; | |
| 17 | 17 | import java.util.HashMap; |
| 18 | 18 | import java.util.List; |
| 19 | 19 | import java.util.Map; |
| 20 | 20 | |
| 21 | +import static cn.hutool.core.text.CharSequenceUtil.*; | |
| 22 | + | |
| 21 | 23 | /** |
| 22 | 24 | * 代码生成的引擎,用于具体生成代码 |
| 23 | 25 | * 目前基于 {@link org.apache.velocity.app.Velocity} 模板引擎实现 |
| ... | ... | @@ -49,7 +51,7 @@ public class ToolCodegenEngine { |
| 49 | 51 | |
| 50 | 52 | private void initGlobalBindingMap() { |
| 51 | 53 | // 全局配置 |
| 52 | - globalBindingMap.put("basePackage", "cn.iocoder.dashboard.modules"); // TODO 基础包 | |
| 54 | + globalBindingMap.put("basePackage", "cn.iocoder.dashboard.modules"); // TODO 基础包, 抽成参数 | |
| 53 | 55 | // 全局 Java Bean |
| 54 | 56 | globalBindingMap.put("PageResultClassName", PageResult.class.getName()); |
| 55 | 57 | globalBindingMap.put("DateUtilsClassName", DateUtils.class.getName()); |
| ... | ... | @@ -64,16 +66,21 @@ public class ToolCodegenEngine { |
| 64 | 66 | |
| 65 | 67 | public void execute(ToolCodegenTableDO table, List<ToolCodegenColumnDO> columns) { |
| 66 | 68 | // 创建 bindingMap |
| 67 | - Map<String, Object> bindingMap = new HashMap<>(); | |
| 69 | + Map<String, Object> bindingMap = new HashMap<>(globalBindingMap); | |
| 68 | 70 | bindingMap.put("table", table); |
| 69 | 71 | bindingMap.put("columns", columns); |
| 70 | - bindingMap.put("hasDateColumn", columns.stream().anyMatch(codegenColumnDO -> | |
| 71 | - codegenColumnDO.getJavaType().equals(Date.class.getSimpleName()))); | |
| 72 | - bindingMap.putAll(globalBindingMap); | |
| 72 | + bindingMap.put("primaryColumn", CollectionUtils.findFirst(columns, ToolCodegenColumnDO::getPrimaryKey)); | |
| 73 | + bindingMap.put("simpleClassName", upperFirst(toCamelCase(subAfter( // 去掉第一个驼峰,例如说 SysUser 去掉后是 User | |
| 74 | + toUnderlineCase(table.getClassName()), '_', false)))); | |
| 73 | 75 | // 执行生成 |
| 74 | 76 | // String result = templateEngine.getTemplate("codegen/dal/do.vm").render(bindingMap); |
| 75 | 77 | // String result = templateEngine.getTemplate("codegen/dal/mapper.vm").render(bindingMap); |
| 76 | - String result = templateEngine.getTemplate("codegen/controller/vo/pageReqVO.vm").render(bindingMap); | |
| 78 | +// String result = templateEngine.getTemplate("codegen/controller/vo/pageReqVO.vm").render(bindingMap); | |
| 79 | +// String result = templateEngine.getTemplate("codegen/controller/vo/baseVO.vm").render(bindingMap); | |
| 80 | +// String result = templateEngine.getTemplate("codegen/controller/vo/createReqVO.vm").render(bindingMap); | |
| 81 | +// String result = templateEngine.getTemplate("codegen/controller/vo/updateReqVO.vm").render(bindingMap); | |
| 82 | +// String result = templateEngine.getTemplate("codegen/controller/vo/respVO.vm").render(bindingMap); | |
| 83 | + String result = templateEngine.getTemplate("codegen/service/service.vm").render(bindingMap); | |
| 77 | 84 | System.out.println(result); |
| 78 | 85 | } |
| 79 | 86 | ... | ... |
src/main/java/cn/iocoder/dashboard/util/collection/CollectionUtils.java
| ... | ... | @@ -86,6 +86,13 @@ public class CollectionUtils { |
| 86 | 86 | return !CollectionUtil.isEmpty(from) ? from.get(0) : null; |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | + public static <T> T findFirst(List<T> from, Predicate<T> predicate) { | |
| 90 | + if (CollUtil.isEmpty(from)) { | |
| 91 | + return null; | |
| 92 | + } | |
| 93 | + return from.stream().filter(predicate).findFirst().orElse(null); | |
| 94 | + } | |
| 95 | + | |
| 89 | 96 | public static <T> void addIfNotNull(Collection<T> coll, T item) { |
| 90 | 97 | if (item == null) { |
| 91 | 98 | return; | ... | ... |
src/main/resources/application.yaml
| ... | ... | @@ -2,6 +2,13 @@ spring: |
| 2 | 2 | application: |
| 3 | 3 | name: dashboard |
| 4 | 4 | |
| 5 | + # 数据源配置项 TODO 多数据源;TODO 监控配置 | |
| 6 | + datasource: | |
| 7 | + url: jdbc:mysql://127.0.1:33061/ruoyi-vue-pro?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT | |
| 8 | + driver-class-name: com.mysql.jdbc.Driver | |
| 9 | + username: root | |
| 10 | + password: 123456 | |
| 11 | + | |
| 5 | 12 | # Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优 |
| 6 | 13 | redis: |
| 7 | 14 | host: 127.0.0.1 # 地址 | ... | ... |
src/main/resources/codegen/service/service.vm
0 → 100644
| 1 | +package ${basePackage}.${table.moduleName}.service.${table.businessName}; | |
| 2 | + | |
| 3 | +import java.util.*; | |
| 4 | +import javax.validation.*; | |
| 5 | +import ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo.*; | |
| 6 | +import ${basePackage}.${table.moduleName}.dal.mysql.dataobject.${table.businessName}.${table.className}DO; | |
| 7 | +import ${PageResultClassName}; | |
| 8 | + | |
| 9 | +/** | |
| 10 | +* ${table.classComment} Service 接口 | |
| 11 | +* | |
| 12 | +* @author ${table.author} | |
| 13 | +*/ | |
| 14 | +public interface ${table.className}Service { | |
| 15 | + | |
| 16 | + /** | |
| 17 | + * 创建${table.classComment} | |
| 18 | + * | |
| 19 | + * @param createReqVO 创建信息 | |
| 20 | + * @return ${class.description} | |
| 21 | + */ | |
| 22 | + ${primaryColumn.javaType} create${simpleClassName}(@Valid ${table.className}CreateReqVO createReqVO); | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * 更新${table.classComment} | |
| 26 | + * | |
| 27 | + * @param updateReqVO 更新信息 | |
| 28 | + */ | |
| 29 | + void update${simpleClassName}(@Valid ${table.className}UpdateReqVO updateReqVO); | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * 删除${table.classComment} | |
| 33 | + * | |
| 34 | + * @param id 编号 | |
| 35 | + */ | |
| 36 | + void delete${simpleClassName}(${primaryColumn.javaType} id); | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * 获得${table.classComment} | |
| 40 | + * | |
| 41 | + * @param id 编号 | |
| 42 | + * @return ${class.description} | |
| 43 | + */ | |
| 44 | + ${table.className}DO get${simpleClassName}(${primaryColumn.javaType} id); | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * 获得${table.classComment}列表 | |
| 48 | + * | |
| 49 | + * @param ids 编号 | |
| 50 | + * @return ${class.classComment}列表 | |
| 51 | + */ | |
| 52 | + List<${table.className}DO> get${simpleClassName}List(Collection<${primaryColumn.javaType}> ids); | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * 获得${table.classComment}分页 | |
| 56 | + * | |
| 57 | + * @param pageReqVO 分页查询 | |
| 58 | + * @return ${class.classComment}分页 | |
| 59 | + */ | |
| 60 | + PageResult<${table.className}DO> get${simpleClassName}Page(${table.className}PageReqVO pageReqVO); | |
| 61 | + | |
| 62 | +} | ... | ... |
src/main/resources/db.yaml deleted