From 69a0dede9bab47d813ccbe9f345f4eaacbb020c0 Mon Sep 17 00:00:00 2001 From: starplatinumora <2500441778@qq.com> Date: Sat, 15 Jan 2022 20:19:25 +0800 Subject: [PATCH] add jpa starp --- .../controller/GeneratorController.java | 1 + .../src/main/resources/template.json | 22 +++- .../code-generator/jpa-starp/starp-entity.ftl | 60 ++++++++++ .../jpa-starp/starp-jpa-controller.ftl | 106 ++++++++++++++++++ .../jpa-starp/starp-repository.ftl | 26 +++++ 5 files changed, 214 insertions(+), 1 deletion(-) create mode 100644 generator-web/src/main/resources/templates/code-generator/jpa-starp/starp-entity.ftl create mode 100644 generator-web/src/main/resources/templates/code-generator/jpa-starp/starp-jpa-controller.ftl create mode 100644 generator-web/src/main/resources/templates/code-generator/jpa-starp/starp-repository.ftl diff --git a/generator-web/src/main/java/com/softdev/system/generator/controller/GeneratorController.java b/generator-web/src/main/java/com/softdev/system/generator/controller/GeneratorController.java index c5a369d..12704ba 100644 --- a/generator-web/src/main/java/com/softdev/system/generator/controller/GeneratorController.java +++ b/generator-web/src/main/java/com/softdev/system/generator/controller/GeneratorController.java @@ -84,6 +84,7 @@ public class GeneratorController { //3.generate the code by freemarker templates with parameters . Freemarker根据参数和模板生成代码 Map result = generatorService.getResultByParams(paramInfo.getOptions()); + log.info("result {}",result); log.info("table:{} - time:{} ", MapUtil.getString(result,"tableName"),new Date()); return ReturnT.ok().put("outputJson",result); } diff --git a/generator-web/src/main/resources/template.json b/generator-web/src/main/resources/template.json index 6105e07..13c6cb2 100644 --- a/generator-web/src/main/resources/template.json +++ b/generator-web/src/main/resources/template.json @@ -229,5 +229,25 @@ "description": "rr-service" } ] + }, + + { + "group": "jpa-starp", + "templates": [{ + "id": "101", + "name": "starp-entity", + "description": "entity" + }, + { + "id": "102", + "name": "starp-repository", + "description": "repository" + }, + { + "id": "103", + "name": "starp-jpa-controller", + "description": "jpacontroller" + } + ] } -] \ No newline at end of file +] diff --git a/generator-web/src/main/resources/templates/code-generator/jpa-starp/starp-entity.ftl b/generator-web/src/main/resources/templates/code-generator/jpa-starp/starp-entity.ftl new file mode 100644 index 0000000..4818345 --- /dev/null +++ b/generator-web/src/main/resources/templates/code-generator/jpa-starp/starp-entity.ftl @@ -0,0 +1,60 @@ +<#if isWithPackage?exists && isWithPackage==true>package ${packageName}.entity; + +<#if isAutoImport?exists && isAutoImport==true> +<#if isLombok?exists && isLombok==true>import lombok.Data; +import java.util.Date; +import java.util.List; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.GeneratedValue; +<#if isSwagger?exists && isSwagger==true> +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +/** + * @description ${classInfo.classComment} + * @author ${authorName} + * @date ${.now?string('yyyy-MM-dd')} + */ +@Entity +@Builder +@AllArgsConstructor +<#if isLombok?exists && isLombok==true>@Data +<#if isComment?exists && isComment==true>@Table(name="${classInfo.originTableName}")<#if isSwagger?exists && isSwagger==true> +@ApiModel("${classInfo.classComment}") +public class ${classInfo.className} implements Serializable { + + private static final long serialVersionUID = 1L; + + @Id + @ApiModelProperty("id") + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + +<#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0> +<#list classInfo.fieldList as fieldItem > + <#if isComment?exists && isComment==true>/** + * ${fieldItem.fieldComment} + */<#if isSwagger?exists && isSwagger==true> + @ApiModelProperty("${fieldItem.fieldComment}") + <#if isComment?exists && isComment==true>@Column(name="${fieldItem.columnName}") + private ${fieldItem.fieldClass} ${fieldItem.fieldName}; + + + public ${classInfo.className}() { + } + + +<#if isLombok?exists && isLombok==false> + public ${fieldItem.fieldClass} get${fieldItem.fieldName?cap_first}() { + return ${fieldItem.fieldName}; + } + + public void set${fieldItem.fieldName?cap_first}(${fieldItem.fieldClass} ${fieldItem.fieldName}) { + this.${fieldItem.fieldName} = ${fieldItem.fieldName}; + } + +} diff --git a/generator-web/src/main/resources/templates/code-generator/jpa-starp/starp-jpa-controller.ftl b/generator-web/src/main/resources/templates/code-generator/jpa-starp/starp-jpa-controller.ftl new file mode 100644 index 0000000..f9470fc --- /dev/null +++ b/generator-web/src/main/resources/templates/code-generator/jpa-starp/starp-jpa-controller.ftl @@ -0,0 +1,106 @@ +<#if isWithPackage?exists && isWithPackage==true>package ${packageName}.controller; +<#if isAutoImport?exists && isAutoImport==true> +import ${packageName}.entity.${classInfo.className}; +import ${packageName}.repository.${classInfo.className}Repository; +import org.springframework.data.domain.Example; +import org.springframework.data.domain.Pageable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.data.domain.ExampleMatcher; +import org.springframework.data.domain.PageRequest; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; +import java.util.List; +import java.util.Map; +import java.util.Optional; + + +/** + * @description ${classInfo.classComment} + * @author ${authorName} + * @date ${.now?string('yyyy-MM-dd')} + */ + +@Slf4j +@Api(tags = "${classInfo.className?uncap_first}") +@CrossOrigin +@RestController +@RequestMapping("/${classInfo.className?uncap_first}") +public class ${classInfo.className}Controller { + + @Autowired + private ${classInfo.className}Repository ${classInfo.className?uncap_first}Repository; + + /** + * 新增或编辑 + */ + @PostMapping("/save") + @ApiOperation(value = "save ${classInfo.className}", notes = "save ${classInfo.className}") + public Object save(@RequestBody ${classInfo.className} ${classInfo.className?uncap_first}){ + try { + return ReturnT.success(${classInfo.className?uncap_first}Repository.save(${classInfo.className?uncap_first})); + } catch (Exception e) { + e.printStackTrace(); + return ReturnT.error("保存失败"); + } + + } + + /** + * 删除 + */ + @PostMapping("/delete") + @ApiOperation(value = "delete ${classInfo.className}", notes = "delete ${classInfo.className}") + public Object delete(int id){ + Optional<${classInfo.className}> ${classInfo.className?uncap_first}=${classInfo.className?uncap_first}Repository.findById(id); + if(${classInfo.className?uncap_first}.isPresent()){ + ${classInfo.className?uncap_first}Repository.deleteById(id); + return ${returnUtilSuccess}("删除成功"); + }else{ + return ${returnUtilFailure}("没有找到该对象"); + } + } + + /** + * 查询 + */ + @PostMapping("/find") + @ApiOperation(value = "find ${classInfo.className} by id", notes = "find ${classInfo.className} by id") + public Object find(int id){ + Optional<${classInfo.className}> ${classInfo.className?uncap_first}=${classInfo.className?uncap_first}Repository.findById(id); + if(${classInfo.className?uncap_first}.isPresent()){ + return ${returnUtilSuccess}(${classInfo.className?uncap_first}.get()); + }else{ + return ${returnUtilFailure}("没有找到该对象"); + } + } + + /** + * 分页查询 + */ + @PostMapping("/list") + @ApiOperation(value = "list ${classInfo.className}", notes = "list ${classInfo.className}") + public Object list(@RequestBody ${classInfo.className} ${classInfo.className?uncap_first}, + @RequestParam(required = false, defaultValue = "0") int pageNumber, + @RequestParam(required = false, defaultValue = "10") int pageSize) { + + try { + //创建匹配器,需要查询条件请修改此处代码 + ExampleMatcher matcher = ExampleMatcher.matchingAll(); + + //创建实例 + Example<${classInfo.className}> example = Example.of(${classInfo.className?uncap_first}, matcher); + //分页构造 + Pageable pageable = PageRequest.of(pageNumber,pageSize); + + return ReturnT.success(${classInfo.className?uncap_first}Repository.findAll(example, pageable)); + + } catch (Exception e) { + e.printStackTrace(); + return ReturnT.error(e.getMessage()); + } + + } + +} diff --git a/generator-web/src/main/resources/templates/code-generator/jpa-starp/starp-repository.ftl b/generator-web/src/main/resources/templates/code-generator/jpa-starp/starp-repository.ftl new file mode 100644 index 0000000..c3643a2 --- /dev/null +++ b/generator-web/src/main/resources/templates/code-generator/jpa-starp/starp-repository.ftl @@ -0,0 +1,26 @@ +<#if isWithPackage?exists && isWithPackage==true>package ${packageName}.repository; +<#if isAutoImport?exists && isAutoImport==true>import ${packageName}.entity.${classInfo.className}; + +<#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0> + <#list classInfo.fieldList as fieldItem > + <#if fieldItem.fieldClass == "Date"> + <#assign importDdate = true /> + + + +import java.util.List; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; + +/** + * @description ${classInfo.classComment} + * @author ${authorName} + * @date ${.now?string('yyyy-MM-dd')} + */ +@Repository +public interface ${classInfo.className}Repository extends JpaRepository<${classInfo.className},Integer> { + + + +}