diff --git a/demo/demo/README.md b/demo/demo/README.md index 3375c3b..36e7c1e 100644 --- a/demo/demo/README.md +++ b/demo/demo/README.md @@ -22,6 +22,12 @@ 用例树表 +1、主键类型改为tinyint,自增 + +2、默认新增节点为叶子节点 + +3、将IS_LEAF改为tinyint类型,默认值为0,当非0的时候表明非叶子节点 + 增:传入参数有【节点名称、父节点ID、创建者(先传入,后面直接获取当前用户信息)】,自动生成节点ID(唯一) 删:传入参数有【节点ID】或者【节点名称】 @@ -30,11 +36,8 @@ 查:传入参数有【节点ID、节点名称】(至少一个) -1、主键类型改为tinyint,自增 - -2、默认新增节点为叶子节点 +用例信息表 -3、将IS_LEAF改为tinyint类型,默认值为0,当非0的时候表明非叶子节点 +1、主键类型改为tinyint,自增 -commit -修改了NOW()代替LocalDateTime.now() \ No newline at end of file +暂且:默认所有用例名称唯一 \ No newline at end of file diff --git a/demo/demo/pom.xml b/demo/demo/pom.xml index e192665..49252ff 100644 --- a/demo/demo/pom.xml +++ b/demo/demo/pom.xml @@ -73,6 +73,12 @@ org.springframework.boot spring-boot-starter-data-redis + + + com.github.pagehelper + pagehelper-spring-boot-starter + 1.4.7 + diff --git a/demo/demo/src/main/java/demo/Controller/CaseInfoController.java b/demo/demo/src/main/java/demo/Controller/CaseInfoController.java new file mode 100644 index 0000000..2bcf928 --- /dev/null +++ b/demo/demo/src/main/java/demo/Controller/CaseInfoController.java @@ -0,0 +1,140 @@ +package demo.Controller; + +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import demo.Entity.*; +import demo.Service.CaseInfoService; +import demo.Service.CaseTreeService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@Slf4j +@RestController +@RequestMapping("/caseinfo") +@Validated +public class CaseInfoController { + @Autowired + private CaseInfoService caseInfoService; + @Autowired + private CaseTreeService caseTreeService; + + @PostMapping("/insert") + public Result insert(@Validated CaseInfoVO caseInfoVO) { + if(caseInfoVO.getCaseName() == null || caseInfoVO.getNodeId() == null){ + return Result.error(401, "新增用例包括用例名称、用例树节点"); + } + + CaseInfo caseInfo = caseInfoService.findByCaseName(caseInfoVO.getCaseName()); + if(caseInfo != null){ + return Result.error(401, "用例已存在"); + } + + CaseTree caseTree = caseTreeService.findByNodeId(caseInfoVO.getNodeId()); + if(caseTree == null){ + return Result.error(401, "用例树节点不存在"); + } + else if(caseTree != null && caseTree.getIsLeaf() != 0) { + return Result.error(401, "非叶子节点不能新增用例"); + } + + caseInfoService.insert(caseInfoVO); + return Result.success("新建用例成功"); + } + + @DeleteMapping("/delete") + public Result delete(@Validated CaseInfoVO caseInfoVO) { + if(caseInfoVO.getId() != null){ + CaseInfo caseInfo = caseInfoService.findById(caseInfoVO.getId()); + if(caseInfo != null){ + if(caseInfoVO.getCaseName() != null && caseInfo.getCaseName() != caseInfoVO.getCaseName()){ + return Result.error(401, "用例不存在"); + } + caseInfoService.delete(caseInfo.getId()); + return Result.success("删除用例成功"); + } + else { + return Result.error(401, "用例不存在"); + } + } + else if(caseInfoVO.getCaseName() != null){ + CaseInfo caseInfo = caseInfoService.findByCaseName(caseInfoVO.getCaseName()); + if(caseInfo != null){ + caseInfoService.delete(caseInfo.getId()); + return Result.success("删除用例成功"); + } + else { + return Result.error(401, "用例不存在"); + } + } + else { + return Result.error(401, "id或用例名称不能全为空"); + } + } + + @PutMapping("/update") + public Result update(@Validated CaseInfoVO caseInfoVO) { + if(caseInfoVO.getId() != null){ + CaseInfo caseInfo = caseInfoService.findByCaseName(caseInfoVO.getCaseName()); + if(caseInfo != null){ + CaseTree caseTree = caseTreeService.findByNodeId(caseInfoVO.getNodeId()); + if(caseTree == null || caseTree != null && caseTree.getIsLeaf() > 0){ + return Result.error(401, "新的用例树节点不是叶子节点,不能更新用例"); + } + caseInfo.setCaseName(caseInfoVO.getCaseName() != null ? caseInfoVO.getCaseName() : caseInfo.getCaseName()); + caseInfo.setNodeId(caseInfoVO.getNodeId() != null ? caseInfoVO.getNodeId() : caseInfo.getNodeId()); + caseInfo.setEditor(caseInfoVO.getEditor() != null ? caseInfoVO.getEditor() : caseInfo.getEditor()); + caseInfoService.update(caseInfo); + return Result.success("更新用例成功"); + } + else { + return Result.error(401, "用例不存在"); + } + } + else if(caseInfoVO.getCaseName() != null){ + CaseInfo caseInfo = caseInfoService.findByCaseName(caseInfoVO.getCaseName()); + if(caseInfo != null){ + CaseTree caseTree = caseTreeService.findByNodeId(caseInfoVO.getNodeId()); + if(caseTree == null || caseTree != null && caseTree.getIsLeaf() > 0){ + return Result.error(401, "新的用例树节点不是叶子节点,不能更新用例"); + } + caseInfo.setNodeId(caseInfoVO.getNodeId() != null ? caseInfoVO.getNodeId() : caseInfo.getNodeId()); + caseInfo.setEditor(caseInfoVO.getEditor() != null ? caseInfoVO.getEditor() : caseInfo.getEditor()); + log.info(caseInfo.getEditor()); + caseInfoService.update(caseInfo); + return Result.success("更新用例成功"); + } + else { + return Result.error(401, "用例不存在"); + } + }else { + return Result.error(401, "缺少用例主键或用例名称,无法更新用例"); + } + } + + @PostMapping("/query") + public Result>> find( + @RequestParam(defaultValue = "0", value = "offset") int offset, + @RequestParam(defaultValue = "4", value = "limit") int limit) { + PageHelper.offsetPage(offset, limit); + List caseInfos = caseInfoService.find(); + PageInfo pageInfo = new PageInfo<>(caseInfos); + PageResult> pageResult = new PageResult<>(pageInfo.getTotal(), caseInfos); + return Result.success(pageResult); + } + + @PostMapping("queryByCaseName") + public Result>> findByCaseName( + @RequestParam(defaultValue = "0", value = "offset") int offset, + @RequestParam(defaultValue = "2", value = "limit") int limit, + @RequestParam(value = "caseName") String caseName) { + PageHelper.offsetPage(offset, limit); + List caseInfos = caseInfoService.findByLikeCaseName(caseName); + PageInfo pageInfo = new PageInfo<>(caseInfos); + PageResult> pageResult = new PageResult<>(pageInfo.getTotal(), caseInfos); + return Result.success(pageResult); + } +} diff --git a/demo/demo/src/main/java/demo/Entity/CaseInfo.java b/demo/demo/src/main/java/demo/Entity/CaseInfo.java index 9b8738e..9f67b7b 100644 --- a/demo/demo/src/main/java/demo/Entity/CaseInfo.java +++ b/demo/demo/src/main/java/demo/Entity/CaseInfo.java @@ -1,4 +1,35 @@ package demo.Entity; +import lombok.Data; + +import javax.persistence.*; +import java.util.Date; + +@Entity +@Data +@Table(name = "case_info") public class CaseInfo { -} + @Id + @Column(length = 32, nullable = false) + private int id; + + @Column(length = 50) + private String caseName; + + @Column(length = 50) + private int nodeId; + + @Column(length = 50) + private String creator; + + @Temporal(TemporalType.TIMESTAMP) + @Column() + private Date createTime; + + @Column(length = 50) + private String editor; + + @Temporal(TemporalType.TIMESTAMP) + @Column() + private Date updateTime; +} \ No newline at end of file diff --git a/demo/demo/src/main/java/demo/Entity/CaseInfoVO.java b/demo/demo/src/main/java/demo/Entity/CaseInfoVO.java new file mode 100644 index 0000000..530aec4 --- /dev/null +++ b/demo/demo/src/main/java/demo/Entity/CaseInfoVO.java @@ -0,0 +1,26 @@ +package demo.Entity; + +import lombok.Data; + +import javax.persistence.*; +import java.util.Date; + +@Entity +@Data +public class CaseInfoVO { + @Id + @Column(length = 32, nullable = false) + private Integer id; + + @Column(length = 50) + private String caseName; + + @Column(length = 50) + private Integer nodeId; + + @Column(length = 50) + private String creator; + + @Column(length = 50) + private String editor; +} \ No newline at end of file diff --git a/demo/demo/src/main/java/demo/Entity/PageResult.java b/demo/demo/src/main/java/demo/Entity/PageResult.java new file mode 100644 index 0000000..bc602fa --- /dev/null +++ b/demo/demo/src/main/java/demo/Entity/PageResult.java @@ -0,0 +1,16 @@ +package demo.Entity; + +import lombok.Data; + +@Data +public class PageResult { + private long total; + private T items; + + public PageResult(long total, T items) { + this.total = total; + this.items = items; + } + + // Getters and Setters for the fields... +} diff --git a/demo/demo/src/main/java/demo/Mapper/CaseInfoMapper.java b/demo/demo/src/main/java/demo/Mapper/CaseInfoMapper.java new file mode 100644 index 0000000..1d2658b --- /dev/null +++ b/demo/demo/src/main/java/demo/Mapper/CaseInfoMapper.java @@ -0,0 +1,32 @@ +package demo.Mapper; + +import demo.Entity.CaseInfo; +import org.apache.ibatis.annotations.*; + +import java.util.List; + +@Mapper +public interface CaseInfoMapper { + @Insert("INSERT INTO case_info (CASE_NAME, NODE_ID, CREATOR, CREATE_TIME, EDITOR, UPDATE_TIME) " + + "VALUES (#{caseName}, #{nodeId}, #{creator}, NOW(), #{creator}, NOW())") + void insert(@Param("caseName") String caseName, @Param("nodeId") Integer nodeId, @Param("creator") String creator); + + @Select("SELECT * FROM case_info WHERE CASE_NAME = #{caseName}") + CaseInfo findByCaseName(@Param("caseName") String caseName); + + @Select("SELECT * FROM case_info WHERE ID = #{id}") + CaseInfo findById(@Param("id") Integer id); + + @Delete("DELETE FROM case_info WHERE ID = #{id}") + void delete(@Param("id") Integer id); + + @Update("UPDATE case_info SET CASE_NAME = #{caseName}, NODE_ID = #{nodeId}, EDITOR = #{editor}, " + + "UPDATE_TIME = NOW() WHERE ID = #{id}") + void update(@Param("id") Integer id, @Param("caseName") String caseName, @Param("nodeId") Integer nodeId, @Param("editor") String editor); + + @Select("SELECT * FROM case_info") + List find(); + + @Select("SELECT * FROM case_info WHERE CASE_NAME LIKE CONCAT('%', #{likeCaseName}, '%')") + List findByLikeCaseName(@Param("likeCaseName") String likeCaseName); +} diff --git a/demo/demo/src/main/java/demo/Service/CaseInfoService.java b/demo/demo/src/main/java/demo/Service/CaseInfoService.java index f193373..bb6ceaf 100644 --- a/demo/demo/src/main/java/demo/Service/CaseInfoService.java +++ b/demo/demo/src/main/java/demo/Service/CaseInfoService.java @@ -1,16 +1,47 @@ package demo.Service; +import demo.Entity.CaseInfo; +import demo.Entity.CaseInfoVO; +import demo.Mapper.CaseInfoMapper; +import org.apache.ibatis.annotations.Param; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.PutMapping; -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import java.util.Date; import java.util.List; -import java.util.Optional; @Service public class CaseInfoService { + @Autowired + private CaseInfoMapper caseInfoMapper; + + public void insert(CaseInfoVO caseInfoVO) { + caseInfoMapper.insert(caseInfoVO.getCaseName(), caseInfoVO.getNodeId(), caseInfoVO.getCreator()); + } + + public CaseInfo findByCaseName(String caseName) { + return caseInfoMapper.findByCaseName(caseName); + } + + public CaseInfo findById(Integer id) { + return caseInfoMapper.findById(id); + } + + public void delete(Integer id) { + caseInfoMapper.delete(id); + } + + public void update(CaseInfo caseInfo) { + caseInfoMapper.update(caseInfo.getId(), caseInfo.getCaseName(), caseInfo.getNodeId(), caseInfo.getEditor()); + } + + public List find(){ + return caseInfoMapper.find(); + } + + public List findByLikeCaseName(String likeCaseName) { + return caseInfoMapper.findByLikeCaseName(likeCaseName); + } // @PersistenceContext // private EntityManager entityManager;