From 2210ed5cf876ce80792c8d28cba80988b85fcd7d Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 6 Feb 2021 21:18:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20service=20=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../generator/util/VelocityInitializer.java | 31 ------- .../src/main/resources/vm/java/domain.java.vm | 85 ------------------- .../src/main/resources/vm/java/mapper.java.vm | 61 ------------- .../dashboard/DashboardApplication.java | 9 +- .../dept/vo/dept/SysDeptCreateReqVO.java | 2 + .../codegen/impl/ToolCodegenEngine.java | 21 +++-- .../util/collection/CollectionUtils.java | 7 ++ src/main/resources/application.yaml | 7 ++ src/main/resources/codegen/service/service.vm | 62 ++++++++++++++ src/main/resources/db.yaml | 7 -- 10 files changed, 97 insertions(+), 195 deletions(-) delete mode 100644 ruoyi-generator/src/main/java/com/ruoyi/generator/util/VelocityInitializer.java delete mode 100644 ruoyi-generator/src/main/resources/vm/java/domain.java.vm delete mode 100644 ruoyi-generator/src/main/resources/vm/java/mapper.java.vm create mode 100644 src/main/resources/codegen/service/service.vm delete mode 100644 src/main/resources/db.yaml diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/util/VelocityInitializer.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/util/VelocityInitializer.java deleted file mode 100644 index 08fe390ff..000000000 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/util/VelocityInitializer.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.ruoyi.generator.util; - -import java.util.Properties; - -import org.apache.velocity.app.Velocity; -import com.ruoyi.common.constant.Constants; - -/** - * VelocityEngine工厂 - * - * @author RuoYi - */ -public class VelocityInitializer { - /** - * 初始化vm方法 - */ - public static void initVelocity() { - Properties p = new Properties(); - try { - // 加载classpath目录下的vm文件 - p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); - // 定义字符集 - p.setProperty(Velocity.ENCODING_DEFAULT, Constants.UTF8); - p.setProperty(Velocity.OUTPUT_ENCODING, Constants.UTF8); - // 初始化Velocity引擎,指定配置Properties - Velocity.init(p); - } catch (Exception e) { - throw new RuntimeException(e); - } - } -} diff --git a/ruoyi-generator/src/main/resources/vm/java/domain.java.vm b/ruoyi-generator/src/main/resources/vm/java/domain.java.vm deleted file mode 100644 index 9dd824786..000000000 --- a/ruoyi-generator/src/main/resources/vm/java/domain.java.vm +++ /dev/null @@ -1,85 +0,0 @@ -package ${packageName}.domain; - -#foreach ($import in $importList) -import ${import}; -#end -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import com.ruoyi.common.annotation.Excel; -#if($table.crud) -import com.ruoyi.common.core.domain.BaseEntity; -#elseif($table.tree) -import com.ruoyi.common.core.domain.TreeEntity; -#end - -/** - * ${functionName}对象 ${tableName} - * - * @author ${author} - * @date ${datetime} - */ -#if($table.crud) -#set($Entity="BaseEntity") -#elseif($table.tree) -#set($Entity="TreeEntity") -#end -public class ${ClassName} extends ${Entity} -{ - private static final long serialVersionUID = 1L; - -#foreach ($column in $columns) -#if(!$table.isSuperColumn($column.javaField)) - /** $column.columnComment */ -#if($column.list) -#set($parentheseIndex=$column.columnComment.indexOf("(")) -#if($parentheseIndex != -1) -#set($comment=$column.columnComment.substring(0, $parentheseIndex)) -#else -#set($comment=$column.columnComment) -#end -#if($parentheseIndex != -1) - @Excel(name = "${comment}", readConverterExp = "$column.readConverterExp()") -#elseif($column.javaType == 'Date') - @JsonFormat(pattern = "yyyy-MM-dd") - @Excel(name = "${comment}", width = 30, dateFormat = "yyyy-MM-dd") -#else - @Excel(name = "${comment}") -#end -#end - private $column.javaType $column.javaField; - -#end -#end -#foreach ($column in $columns) -#if(!$table.isSuperColumn($column.javaField)) -#if($column.javaField.length() > 2 && $column.javaField.substring(1,2).matches("[A-Z]")) -#set($AttrName=$column.javaField) -#else -#set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)}) -#end - public void set${AttrName}($column.javaType $column.javaField) - { - this.$column.javaField = $column.javaField; - } - - public $column.javaType get${AttrName}() - { - return $column.javaField; - } -#end -#end - - @Override - public String toString() { - return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) -#foreach ($column in $columns) -#if($column.javaField.length() > 2 && $column.javaField.substring(1,2).matches("[A-Z]")) -#set($AttrName=$column.javaField) -#else -#set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)}) -#end - .append("${column.javaField}", get${AttrName}()) -#end - .toString(); - } -} diff --git a/ruoyi-generator/src/main/resources/vm/java/mapper.java.vm b/ruoyi-generator/src/main/resources/vm/java/mapper.java.vm deleted file mode 100644 index 5502baf6d..000000000 --- a/ruoyi-generator/src/main/resources/vm/java/mapper.java.vm +++ /dev/null @@ -1,61 +0,0 @@ -package ${packageName}.mapper; - -import java.util.List; -import ${packageName}.domain.${ClassName}; - -/** - * ${functionName}Mapper接口 - * - * @author ${author} - * @date ${datetime} - */ -public interface ${ClassName}Mapper -{ - /** - * 查询${functionName} - * - * @param ${pkColumn.javaField} ${functionName}ID - * @return ${functionName} - */ - public ${ClassName} select${ClassName}ById(${pkColumn.javaType} ${pkColumn.javaField}); - - /** - * 查询${functionName}列表 - * - * @param ${className} ${functionName} - * @return ${functionName}集合 - */ - public List<${ClassName}> select${ClassName}List(${ClassName} ${className}); - - /** - * 新增${functionName} - * - * @param ${className} ${functionName} - * @return 结果 - */ - public int insert${ClassName}(${ClassName} ${className}); - - /** - * 修改${functionName} - * - * @param ${className} ${functionName} - * @return 结果 - */ - public int update${ClassName}(${ClassName} ${className}); - - /** - * 删除${functionName} - * - * @param ${pkColumn.javaField} ${functionName}ID - * @return 结果 - */ - public int delete${ClassName}ById(${pkColumn.javaType} ${pkColumn.javaField}); - - /** - * 批量删除${functionName} - * - * @param ${pkColumn.javaField}s 需要删除的数据ID - * @return 结果 - */ - public int delete${ClassName}ByIds(${pkColumn.javaType}[] ${pkColumn.javaField}s); -} diff --git a/src/main/java/cn/iocoder/dashboard/DashboardApplication.java b/src/main/java/cn/iocoder/dashboard/DashboardApplication.java index f2092e2b3..f46f4bc3b 100644 --- a/src/main/java/cn/iocoder/dashboard/DashboardApplication.java +++ b/src/main/java/cn/iocoder/dashboard/DashboardApplication.java @@ -8,11 +8,12 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableAdminServer // TODO 芋艿:需要迁移出去 public class DashboardApplication { - public static void main(String[] args) { - // 设置读取的配置文件 - System.setProperty("spring.config.name", "application,db"); +// static { +// // 设置读取的配置文件 +// System.setProperty("spring.config.name", "application,db"); +// } - // 启动 Spring Boot + public static void main(String[] args) { SpringApplication.run(DashboardApplication.class, args); } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/dept/vo/dept/SysDeptCreateReqVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/dept/vo/dept/SysDeptCreateReqVO.java index 3e18aa4dd..6ea550c76 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/dept/vo/dept/SysDeptCreateReqVO.java +++ b/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; import io.swagger.annotations.ApiModel; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.ToString; @ApiModel("部门创建 Request VO") @Data @EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) public class SysDeptCreateReqVO extends SysDeptBaseVO { } diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java b/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java index 5ce35f877..4258cf192 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java +++ b/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; import cn.iocoder.dashboard.framework.mybatis.core.query.QueryWrapperX; import cn.iocoder.dashboard.modules.tool.dal.mysql.dataobject.codegen.ToolCodegenColumnDO; import cn.iocoder.dashboard.modules.tool.dal.mysql.dataobject.codegen.ToolCodegenTableDO; +import cn.iocoder.dashboard.util.collection.CollectionUtils; import cn.iocoder.dashboard.util.date.DateUtils; import org.springframework.stereotype.Component; -import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; +import static cn.hutool.core.text.CharSequenceUtil.*; + /** * 代码生成的引擎,用于具体生成代码 * 目前基于 {@link org.apache.velocity.app.Velocity} 模板引擎实现 @@ -49,7 +51,7 @@ public class ToolCodegenEngine { private void initGlobalBindingMap() { // 全局配置 - globalBindingMap.put("basePackage", "cn.iocoder.dashboard.modules"); // TODO 基础包 + globalBindingMap.put("basePackage", "cn.iocoder.dashboard.modules"); // TODO 基础包, 抽成参数 // 全局 Java Bean globalBindingMap.put("PageResultClassName", PageResult.class.getName()); globalBindingMap.put("DateUtilsClassName", DateUtils.class.getName()); @@ -64,16 +66,21 @@ public class ToolCodegenEngine { public void execute(ToolCodegenTableDO table, List columns) { // 创建 bindingMap - Map bindingMap = new HashMap<>(); + Map bindingMap = new HashMap<>(globalBindingMap); bindingMap.put("table", table); bindingMap.put("columns", columns); - bindingMap.put("hasDateColumn", columns.stream().anyMatch(codegenColumnDO -> - codegenColumnDO.getJavaType().equals(Date.class.getSimpleName()))); - bindingMap.putAll(globalBindingMap); + bindingMap.put("primaryColumn", CollectionUtils.findFirst(columns, ToolCodegenColumnDO::getPrimaryKey)); + bindingMap.put("simpleClassName", upperFirst(toCamelCase(subAfter( // 去掉第一个驼峰,例如说 SysUser 去掉后是 User + toUnderlineCase(table.getClassName()), '_', false)))); // 执行生成 // String result = templateEngine.getTemplate("codegen/dal/do.vm").render(bindingMap); // String result = templateEngine.getTemplate("codegen/dal/mapper.vm").render(bindingMap); - String result = templateEngine.getTemplate("codegen/controller/vo/pageReqVO.vm").render(bindingMap); +// String result = templateEngine.getTemplate("codegen/controller/vo/pageReqVO.vm").render(bindingMap); +// String result = templateEngine.getTemplate("codegen/controller/vo/baseVO.vm").render(bindingMap); +// String result = templateEngine.getTemplate("codegen/controller/vo/createReqVO.vm").render(bindingMap); +// String result = templateEngine.getTemplate("codegen/controller/vo/updateReqVO.vm").render(bindingMap); +// String result = templateEngine.getTemplate("codegen/controller/vo/respVO.vm").render(bindingMap); + String result = templateEngine.getTemplate("codegen/service/service.vm").render(bindingMap); System.out.println(result); } diff --git a/src/main/java/cn/iocoder/dashboard/util/collection/CollectionUtils.java b/src/main/java/cn/iocoder/dashboard/util/collection/CollectionUtils.java index 5142b7926..3d7d54881 100644 --- a/src/main/java/cn/iocoder/dashboard/util/collection/CollectionUtils.java +++ b/src/main/java/cn/iocoder/dashboard/util/collection/CollectionUtils.java @@ -86,6 +86,13 @@ public class CollectionUtils { return !CollectionUtil.isEmpty(from) ? from.get(0) : null; } + public static T findFirst(List from, Predicate predicate) { + if (CollUtil.isEmpty(from)) { + return null; + } + return from.stream().filter(predicate).findFirst().orElse(null); + } + public static void addIfNotNull(Collection coll, T item) { if (item == null) { return; diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 9fd382e14..634cbd325 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -2,6 +2,13 @@ spring: application: name: dashboard + # 数据源配置项 TODO 多数据源;TODO 监控配置 + datasource: + url: jdbc:mysql://127.0.1:33061/ruoyi-vue-pro?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT + driver-class-name: com.mysql.jdbc.Driver + username: root + password: 123456 + # Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优 redis: host: 127.0.0.1 # 地址 diff --git a/src/main/resources/codegen/service/service.vm b/src/main/resources/codegen/service/service.vm new file mode 100644 index 000000000..68a26df89 --- /dev/null +++ b/src/main/resources/codegen/service/service.vm @@ -0,0 +1,62 @@ +package ${basePackage}.${table.moduleName}.service.${table.businessName}; + +import java.util.*; +import javax.validation.*; +import ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo.*; +import ${basePackage}.${table.moduleName}.dal.mysql.dataobject.${table.businessName}.${table.className}DO; +import ${PageResultClassName}; + +/** +* ${table.classComment} Service 接口 +* +* @author ${table.author} +*/ +public interface ${table.className}Service { + + /** + * 创建${table.classComment} + * + * @param createReqVO 创建信息 + * @return ${class.description} + */ + ${primaryColumn.javaType} create${simpleClassName}(@Valid ${table.className}CreateReqVO createReqVO); + + /** + * 更新${table.classComment} + * + * @param updateReqVO 更新信息 + */ + void update${simpleClassName}(@Valid ${table.className}UpdateReqVO updateReqVO); + + /** + * 删除${table.classComment} + * + * @param id 编号 + */ + void delete${simpleClassName}(${primaryColumn.javaType} id); + + /** + * 获得${table.classComment} + * + * @param id 编号 + * @return ${class.description} + */ + ${table.className}DO get${simpleClassName}(${primaryColumn.javaType} id); + + /** + * 获得${table.classComment}列表 + * + * @param ids 编号 + * @return ${class.classComment}列表 + */ + List<${table.className}DO> get${simpleClassName}List(Collection<${primaryColumn.javaType}> ids); + + /** + * 获得${table.classComment}分页 + * + * @param pageReqVO 分页查询 + * @return ${class.classComment}分页 + */ + PageResult<${table.className}DO> get${simpleClassName}Page(${table.className}PageReqVO pageReqVO); + +} diff --git a/src/main/resources/db.yaml b/src/main/resources/db.yaml deleted file mode 100644 index 05018f24f..000000000 --- a/src/main/resources/db.yaml +++ /dev/null @@ -1,7 +0,0 @@ -spring: - # 数据源配置项 TODO 多数据源;TODO 监控配置 - datasource: - url: jdbc:mysql://127.0.1:33061/ruoyi-vue-pro?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT - driver-class-name: com.mysql.jdbc.Driver - username: root - password: 123456