Browse Source

新增用例信息表的增删改查

chubin
郑楚滨 3 months ago
parent
commit
2a77313e13
  1. 15
      demo/demo/README.md
  2. 6
      demo/demo/pom.xml
  3. 140
      demo/demo/src/main/java/demo/Controller/CaseInfoController.java
  4. 33
      demo/demo/src/main/java/demo/Entity/CaseInfo.java
  5. 26
      demo/demo/src/main/java/demo/Entity/CaseInfoVO.java
  6. 16
      demo/demo/src/main/java/demo/Entity/PageResult.java
  7. 32
      demo/demo/src/main/java/demo/Mapper/CaseInfoMapper.java
  8. 41
      demo/demo/src/main/java/demo/Service/CaseInfoService.java

15
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()
暂且:默认所有用例名称唯一

6
demo/demo/pom.xml

@ -73,6 +73,12 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.7</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>

140
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<String> 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<String> 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<String> 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<PageResult<List<CaseInfo>>> find(
@RequestParam(defaultValue = "0", value = "offset") int offset,
@RequestParam(defaultValue = "4", value = "limit") int limit) {
PageHelper.offsetPage(offset, limit);
List<CaseInfo> caseInfos = caseInfoService.find();
PageInfo<CaseInfo> pageInfo = new PageInfo<>(caseInfos);
PageResult<List<CaseInfo>> pageResult = new PageResult<>(pageInfo.getTotal(), caseInfos);
return Result.success(pageResult);
}
@PostMapping("queryByCaseName")
public Result<PageResult<List<CaseInfo>>> 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<CaseInfo> caseInfos = caseInfoService.findByLikeCaseName(caseName);
PageInfo<CaseInfo> pageInfo = new PageInfo<>(caseInfos);
PageResult<List<CaseInfo>> pageResult = new PageResult<>(pageInfo.getTotal(), caseInfos);
return Result.success(pageResult);
}
}

33
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;
}

26
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;
}

16
demo/demo/src/main/java/demo/Entity/PageResult.java

@ -0,0 +1,16 @@
package demo.Entity;
import lombok.Data;
@Data
public class PageResult<T> {
private long total;
private T items;
public PageResult(long total, T items) {
this.total = total;
this.items = items;
}
// Getters and Setters for the fields...
}

32
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<CaseInfo> find();
@Select("SELECT * FROM case_info WHERE CASE_NAME LIKE CONCAT('%', #{likeCaseName}, '%')")
List<CaseInfo> findByLikeCaseName(@Param("likeCaseName") String likeCaseName);
}

41
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<CaseInfo> find(){
return caseInfoMapper.find();
}
public List<CaseInfo> findByLikeCaseName(String likeCaseName) {
return caseInfoMapper.findByLikeCaseName(likeCaseName);
}
// @PersistenceContext
// private EntityManager entityManager;

Loading…
Cancel
Save