Commit 59be930f2e93d3247486c9d2c88dbde929c56c24
1 parent
5442f652
添加 serviceImpl.vm 的模板
Showing
7 changed files
with
143 additions
and
117 deletions
ruoyi-generator/src/main/resources/vm/java/serviceImpl.java.vm deleted
| 1 | -package ${packageName}.service.impl; | |
| 2 | - | |
| 3 | -import java.util.List; | |
| 4 | -#foreach ($column in $columns) | |
| 5 | -#if($column.javaField == 'createTime' || $column.javaField == 'updateTime') | |
| 6 | -import com.ruoyi.common.utils.DateUtils; | |
| 7 | -#break | |
| 8 | -#end | |
| 9 | -#end | |
| 10 | -import org.springframework.beans.factory.annotation.Autowired; | |
| 11 | -import org.springframework.stereotype.Service; | |
| 12 | -import ${packageName}.mapper.${ClassName}Mapper; | |
| 13 | -import ${packageName}.domain.${ClassName}; | |
| 14 | -import ${packageName}.service.I${ClassName}Service; | |
| 15 | - | |
| 16 | -/** | |
| 17 | - * ${functionName}Service业务层处理 | |
| 18 | - * | |
| 19 | - * @author ${author} | |
| 20 | - * @date ${datetime} | |
| 21 | - */ | |
| 22 | -@Service | |
| 23 | -public class ${ClassName}ServiceImpl implements I${ClassName}Service | |
| 24 | -{ | |
| 25 | - @Autowired | |
| 26 | - private ${ClassName}Mapper ${className}Mapper; | |
| 27 | - | |
| 28 | - /** | |
| 29 | - * 查询${functionName} | |
| 30 | - * | |
| 31 | - * @param ${pkColumn.javaField} ${functionName}ID | |
| 32 | - * @return ${functionName} | |
| 33 | - */ | |
| 34 | - @Override | |
| 35 | - public ${ClassName} select${ClassName}ById(${pkColumn.javaType} ${pkColumn.javaField}) | |
| 36 | - { | |
| 37 | - return ${className}Mapper.select${ClassName}ById(${pkColumn.javaField}); | |
| 38 | - } | |
| 39 | - | |
| 40 | - /** | |
| 41 | - * 查询${functionName}列表 | |
| 42 | - * | |
| 43 | - * @param ${className} ${functionName} | |
| 44 | - * @return ${functionName} | |
| 45 | - */ | |
| 46 | - @Override | |
| 47 | - public List<${ClassName}> select${ClassName}List(${ClassName} ${className}) | |
| 48 | - { | |
| 49 | - return ${className}Mapper.select${ClassName}List(${className}); | |
| 50 | - } | |
| 51 | - | |
| 52 | - /** | |
| 53 | - * 新增${functionName} | |
| 54 | - * | |
| 55 | - * @param ${className} ${functionName} | |
| 56 | - * @return 结果 | |
| 57 | - */ | |
| 58 | - @Override | |
| 59 | - public int insert${ClassName}(${ClassName} ${className}) | |
| 60 | - { | |
| 61 | -#foreach ($column in $columns) | |
| 62 | -#if($column.javaField == 'createTime') | |
| 63 | - ${className}.setCreateTime(DateUtils.getNowDate()); | |
| 64 | -#end | |
| 65 | -#end | |
| 66 | - return ${className}Mapper.insert${ClassName}(${className}); | |
| 67 | - } | |
| 68 | - | |
| 69 | - /** | |
| 70 | - * 修改${functionName} | |
| 71 | - * | |
| 72 | - * @param ${className} ${functionName} | |
| 73 | - * @return 结果 | |
| 74 | - */ | |
| 75 | - @Override | |
| 76 | - public int update${ClassName}(${ClassName} ${className}) | |
| 77 | - { | |
| 78 | -#foreach ($column in $columns) | |
| 79 | -#if($column.javaField == 'updateTime') | |
| 80 | - ${className}.setUpdateTime(DateUtils.getNowDate()); | |
| 81 | -#end | |
| 82 | -#end | |
| 83 | - return ${className}Mapper.update${ClassName}(${className}); | |
| 84 | - } | |
| 85 | - | |
| 86 | - /** | |
| 87 | - * 批量删除${functionName} | |
| 88 | - * | |
| 89 | - * @param ${pkColumn.javaField}s 需要删除的${functionName}ID | |
| 90 | - * @return 结果 | |
| 91 | - */ | |
| 92 | - @Override | |
| 93 | - public int delete${ClassName}ByIds(${pkColumn.javaType}[] ${pkColumn.javaField}s) | |
| 94 | - { | |
| 95 | - return ${className}Mapper.delete${ClassName}ByIds(${pkColumn.javaField}s); | |
| 96 | - } | |
| 97 | - | |
| 98 | - /** | |
| 99 | - * 删除${functionName}信息 | |
| 100 | - * | |
| 101 | - * @param ${pkColumn.javaField} ${functionName}ID | |
| 102 | - * @return 结果 | |
| 103 | - */ | |
| 104 | - @Override | |
| 105 | - public int delete${ClassName}ById(${pkColumn.javaType} ${pkColumn.javaField}) | |
| 106 | - { | |
| 107 | - return ${className}Mapper.delete${ClassName}ById(${pkColumn.javaField}); | |
| 108 | - } | |
| 109 | -} |
src/main/java/cn/iocoder/dashboard/modules/system/enums/SysErrorCodeConstants.java
| ... | ... | @@ -75,4 +75,7 @@ public interface SysErrorCodeConstants { |
| 75 | 75 | // ========== 文件 1002009000 ========== |
| 76 | 76 | ErrorCode FILE_PATH_EXISTS = new ErrorCode(1002009001, "文件路径已经存在"); |
| 77 | 77 | |
| 78 | + // ========== 字典类型(测试) 1002010000 ========== | |
| 79 | + ErrorCode TEST_DEMO_NOT_EXISTS = new ErrorCode(1002010000, "字典类型不存在}"); | |
| 80 | + | |
| 78 | 81 | } | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/system/service/test/impl/SysTestDemoServiceImpl.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.modules.system.service.test.impl; | |
| 2 | + | |
| 3 | +import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil; | |
| 4 | +import cn.iocoder.dashboard.common.pojo.PageResult; | |
| 5 | +import cn.iocoder.dashboard.modules.system.controller.test.vo.SysTestDemoCreateReqVO; | |
| 6 | +import cn.iocoder.dashboard.modules.system.controller.test.vo.SysTestDemoPageReqVO; | |
| 7 | +import cn.iocoder.dashboard.modules.system.controller.test.vo.SysTestDemoUpdateReqVO; | |
| 8 | +import cn.iocoder.dashboard.modules.system.convert.test.SysTestDemoConvert; | |
| 9 | +import cn.iocoder.dashboard.modules.system.dal.mysql.dao.test.SysTestDemoMapper; | |
| 10 | +import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.test.SysTestDemoDO; | |
| 11 | +import cn.iocoder.dashboard.modules.system.service.test.SysTestDemoService; | |
| 12 | +import org.springframework.stereotype.Service; | |
| 13 | +import org.springframework.validation.annotation.Validated; | |
| 14 | + | |
| 15 | +import javax.annotation.Resource; | |
| 16 | +import java.util.Collection; | |
| 17 | +import java.util.List; | |
| 18 | + | |
| 19 | +import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.TEST_DEMO_NOT_EXISTS; | |
| 20 | + | |
| 21 | +/** | |
| 22 | +* 字典类型 Service 实现类 | |
| 23 | +* | |
| 24 | +* @author 芋艿 | |
| 25 | +*/ | |
| 26 | +@Service | |
| 27 | +@Validated | |
| 28 | +public class SysTestDemoServiceImpl implements SysTestDemoService { | |
| 29 | + | |
| 30 | + @Resource | |
| 31 | + private SysTestDemoMapper testDemoMapper; | |
| 32 | + | |
| 33 | + @Override | |
| 34 | + public Long createTestDemo(SysTestDemoCreateReqVO createReqVO) { | |
| 35 | + // 插入 | |
| 36 | + SysTestDemoDO testDemo = SysTestDemoConvert.INSTANCE.convert(createReqVO); | |
| 37 | + testDemoMapper.insert(testDemo); | |
| 38 | + // 返回 | |
| 39 | + return testDemo.getId(); | |
| 40 | + } | |
| 41 | + | |
| 42 | + @Override | |
| 43 | + public void updateTestDemo(SysTestDemoUpdateReqVO updateReqVO) { | |
| 44 | + // 校验存在 | |
| 45 | + this.validateTestDemoExists(updateReqVO.getId()); | |
| 46 | + // 更新 | |
| 47 | + SysTestDemoDO updateObj = SysTestDemoConvert.INSTANCE.convert(updateReqVO); | |
| 48 | + testDemoMapper.updateById(updateObj); | |
| 49 | + } | |
| 50 | + | |
| 51 | + @Override | |
| 52 | + public void deleteTestDemo(Long id) { | |
| 53 | + // 校验存在 | |
| 54 | + this.validateTestDemoExists(id); | |
| 55 | + // 更新 | |
| 56 | + testDemoMapper.deleteById(id); | |
| 57 | + } | |
| 58 | + | |
| 59 | + private void validateTestDemoExists(Long id) { | |
| 60 | + if (testDemoMapper.selectById(id) == null) { | |
| 61 | + throw ServiceExceptionUtil.exception(TEST_DEMO_NOT_EXISTS); | |
| 62 | + } | |
| 63 | + } | |
| 64 | + | |
| 65 | + @Override | |
| 66 | + public SysTestDemoDO getTestDemo(Long id) { | |
| 67 | + return testDemoMapper.selectById(id); | |
| 68 | + } | |
| 69 | + | |
| 70 | + @Override | |
| 71 | + public List<SysTestDemoDO> getTestDemoList(Collection<Long> ids) { | |
| 72 | + return testDemoMapper.selectBatchIds(ids); | |
| 73 | + } | |
| 74 | + | |
| 75 | + @Override | |
| 76 | + public PageResult<SysTestDemoDO> getTestDemoPage(SysTestDemoPageReqVO pageReqVO) { | |
| 77 | + return testDemoMapper.selectPage(pageReqVO); | |
| 78 | + } | |
| 79 | + | |
| 80 | +} | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java
| ... | ... | @@ -3,6 +3,7 @@ package cn.iocoder.dashboard.modules.tool.service.codegen.impl; |
| 3 | 3 | import cn.hutool.extra.template.TemplateConfig; |
| 4 | 4 | import cn.hutool.extra.template.TemplateEngine; |
| 5 | 5 | import cn.hutool.extra.template.TemplateUtil; |
| 6 | +import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil; | |
| 6 | 7 | import cn.iocoder.dashboard.common.pojo.PageParam; |
| 7 | 8 | import cn.iocoder.dashboard.common.pojo.PageResult; |
| 8 | 9 | import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; |
| ... | ... | @@ -59,11 +60,12 @@ public class ToolCodegenEngine { |
| 59 | 60 | // 全局 Java Bean |
| 60 | 61 | globalBindingMap.put("PageResultClassName", PageResult.class.getName()); |
| 61 | 62 | globalBindingMap.put("DateUtilsClassName", DateUtils.class.getName()); |
| 63 | + globalBindingMap.put("ServiceExceptionUtilClassName", ServiceExceptionUtil.class.getName()); | |
| 62 | 64 | // VO 类,独有字段 |
| 63 | 65 | globalBindingMap.put("PageParamClassName", PageParam.class.getName()); |
| 64 | 66 | // DO 类,独有字段 |
| 65 | 67 | globalBindingMap.put("baseDOFields", ToolCodegenBuilder.BASE_DO_FIELDS); |
| 66 | - globalBindingMap.put("baseDOClassName", BaseDO.class.getName()); | |
| 68 | + globalBindingMap.put("BaseDOClassName", BaseDO.class.getName()); | |
| 67 | 69 | globalBindingMap.put("QueryWrapperClassName", QueryWrapperX.class.getName()); |
| 68 | 70 | globalBindingMap.put("BaseMapperClassName", BaseMapperX.class.getName()); |
| 69 | 71 | } |
| ... | ... | @@ -92,8 +94,8 @@ public class ToolCodegenEngine { |
| 92 | 94 | // String result = templateEngine.getTemplate("codegen/controller/vo/respVO.vm").render(bindingMap); |
| 93 | 95 | // String result = templateEngine.getTemplate("codegen/convert/convert.vm").render(bindingMap); |
| 94 | 96 | // String result = templateEngine.getTemplate("codegen/enums/errorcode.vm").render(bindingMap); |
| 95 | - String result = templateEngine.getTemplate("codegen/service/service.vm").render(bindingMap); | |
| 96 | -// String result = templateEngine.getTemplate("codegen/service/serviceImpl.vm").render(bindingMap); | |
| 97 | +// String result = templateEngine.getTemplate("codegen/service/service.vm").render(bindingMap); | |
| 98 | + String result = templateEngine.getTemplate("codegen/service/serviceImpl.vm").render(bindingMap); | |
| 97 | 99 | System.out.println(result); |
| 98 | 100 | } |
| 99 | 101 | ... | ... |
src/main/resources/codegen/dal/do.vm
| ... | ... | @@ -3,7 +3,7 @@ package ${basePackage}.${table.moduleName}.dal.mysql.dataobject.${table.business |
| 3 | 3 | import lombok.*; |
| 4 | 4 | import java.util.*; |
| 5 | 5 | import com.baomidou.mybatisplus.annotation.*; |
| 6 | -import ${baseDOClassName}; | |
| 6 | +import ${BaseDOClassName}; | |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | 9 | * ${table.classComment} DO | ... | ... |
src/main/resources/codegen/enums/errorcode.vm
| 1 | -// ========== ${table.classCommet} TODO 补充编号 ========== | |
| 2 | -ErrorCode ${simpleClassName_underlineCase.toUpperCase()}_NOT_FOUND = new ErrorCode(TODO 补充编号, "${table.classComment}不存在}"); | |
| 1 | +// ========== ${table.classComment} TODO 补充编号 ========== | |
| 2 | +ErrorCode ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS = new ErrorCode(TODO 补充编号, "${table.classComment}不存在}"); | ... | ... |
src/main/resources/codegen/service/serviceImpl.vm
| ... | ... | @@ -10,6 +10,12 @@ import ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo.*; |
| 10 | 10 | import ${basePackage}.${table.moduleName}.dal.mysql.dataobject.${table.businessName}.${table.className}DO; |
| 11 | 11 | import ${PageResultClassName}; |
| 12 | 12 | |
| 13 | +import ${basePackage}.${table.moduleName}.convert.${table.businessName}.${table.className}Convert; | |
| 14 | +import ${basePackage}.${table.moduleName}.dal.mysql.dao.${table.businessName}.${table.className}Mapper; | |
| 15 | +import ${basePackage}.${table.moduleName}.service.${table.businessName}.${table.className}Service; | |
| 16 | + | |
| 17 | +import ${ServiceExceptionUtilClassName}; | |
| 18 | + | |
| 13 | 19 | import static ${basePackage}.${table.moduleName}.enums.${simpleModuleName.substring(0,1).toUpperCase()}${simpleModuleName.substring(1)}ErrorCodeConstants.*; |
| 14 | 20 | |
| 15 | 21 | /** |
| ... | ... | @@ -17,14 +23,58 @@ import static ${basePackage}.${table.moduleName}.enums.${simpleModuleName.substr |
| 17 | 23 | * |
| 18 | 24 | * @author ${table.author} |
| 19 | 25 | */ |
| 26 | +@Service | |
| 20 | 27 | @Validated |
| 21 | -public class ${table.className}ServiceImpl implements {table.className}Service { | |
| 28 | +public class ${table.className}ServiceImpl implements ${table.className}Service { | |
| 22 | 29 | |
| 23 | 30 | @Resource |
| 24 | 31 | private ${table.className}Mapper ${classNameVar}Mapper; |
| 25 | 32 | |
| 26 | 33 | @Override |
| 27 | - public ${primaryColumn.javaType} create${simpleClassName}(@Valid ${table.className}CreateReqVO createReqVO) { | |
| 34 | + public ${primaryColumn.javaType} create${simpleClassName}(${table.className}CreateReqVO createReqVO) { | |
| 35 | + // 插入 | |
| 36 | + ${table.className}DO ${classNameVar} = ${table.className}Convert.INSTANCE.convert(createReqVO); | |
| 37 | + ${classNameVar}Mapper.insert(${classNameVar}); | |
| 38 | + // 返回 | |
| 39 | + return ${classNameVar}.getId(); | |
| 40 | + } | |
| 41 | + | |
| 42 | + @Override | |
| 43 | + public void update${simpleClassName}(${table.className}UpdateReqVO updateReqVO) { | |
| 44 | + // 校验存在 | |
| 45 | + this.validate${simpleClassName}Exists(updateReqVO.getId()); | |
| 46 | + // 更新 | |
| 47 | + ${table.className}DO updateObj = ${table.className}Convert.INSTANCE.convert(updateReqVO); | |
| 48 | + ${classNameVar}Mapper.updateById(updateObj); | |
| 49 | + } | |
| 50 | + | |
| 51 | + @Override | |
| 52 | + public void delete${simpleClassName}(${primaryColumn.javaType} id) { | |
| 53 | + // 校验存在 | |
| 54 | + this.validate${simpleClassName}Exists(id); | |
| 55 | + // 更新 | |
| 56 | + ${classNameVar}Mapper.deleteById(id); | |
| 57 | + } | |
| 58 | + | |
| 59 | + private void validate${simpleClassName}Exists(${primaryColumn.javaType} id) { | |
| 60 | + if (${classNameVar}Mapper.selectById(id) == null) { | |
| 61 | + throw ServiceExceptionUtil.exception(${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS); | |
| 62 | + } | |
| 63 | + } | |
| 64 | + | |
| 65 | + @Override | |
| 66 | + public ${table.className}DO get${simpleClassName}(${primaryColumn.javaType} id) { | |
| 67 | + return ${classNameVar}Mapper.selectById(id); | |
| 68 | + } | |
| 69 | + | |
| 70 | + @Override | |
| 71 | + public List<${table.className}DO> get${simpleClassName}List(Collection<${primaryColumn.javaType}> ids) { | |
| 72 | + return ${classNameVar}Mapper.selectBatchIds(ids); | |
| 73 | + } | |
| 74 | + | |
| 75 | + @Override | |
| 76 | + public PageResult<${table.className}DO> get${simpleClassName}Page(${table.className}PageReqVO pageReqVO) { | |
| 77 | + return ${classNameVar}Mapper.selectPage(pageReqVO); | |
| 28 | 78 | } |
| 29 | 79 | |
| 30 | 80 | } | ... | ... |