自动化测试三(castlist)

controller层 

import com.choosefine.automatedtest.cases.caselist.dto.CaseDto;      //dto 
import com.choosefine.automatedtest.cases.caselist.service.CaseListService;  
import com.choosefine.automatedtest.common.controller.BaseController;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Administrator on 2017/7/14.
 */

@RestController
@RequestMapping("/automated/caseList")
public class CaseListController extends BaseController{
    @Autowired
    CaseListService caseListService;

    @ApiOperation(value = "获取case列表")   
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "caseNumber", dataType = "String", required = false, value = "case编号",defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "caseName", dataType = "String", required = false, value = "case名称",defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "operator", dataType = "String", required = false, value = "操作员",defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "pageSize", dataType = "int", required = true, value = "分页大小,默认10", defaultValue = "10"),
            @ApiImplicitParam(paramType = "query", name = "pageNum", dataType = "int", required = true, value = "页码,默认1", defaultValue = "1"),
    })
    @GetMapping   //查询 后面有分页插件
    public String getCaseList(String caseNumber,String caseName,Integer caseType,String operator,Integer pageSize,Integer pageNum){
        return responseSuccess(caseListService.getCase(caseNumber,caseName,caseType,operator,pageSize,pageNum));
    }

    @ApiOperation(value = "新增case")
    @PostMapping
    public String addCase(@RequestBody CaseDto caseDto){        //新增的是dto 一般model层数据不多的就没必要写dto dto是数据传输封装了部分需要传输的model层数据 
        return responseSuccess(caseListService.addCase(caseDto));
    }

    @ApiOperation(value = "删除case")
    @ApiImplicitParams({      
            @ApiImplicitParam(paramType = "query", name = "caseId", dataType = "int", required = true, value = "case的自增id")
    })
    @DeleteMapping  // 删除
    public String deleteCase(Integer caseId){
        return responseSuccess(caseListService.deleteCase(caseId));
    }

    @ApiOperation(value = "获取caseStep")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "caseId", dataType = "int", required = true, value = "case的自增id")
    })
    @GetMapping("/caseStep")    //查询步骤 
    public String getCaseStep(Integer caseId){
        return responseSuccess(caseListService.getCaseStep(caseId));
    }

    @ApiOperation(value = "更新case")
    @PutMapping        // 更新就是修改里面的参数 有body 
    public String updateCase(@RequestBody CaseDto caseDto){
        return responseSuccess(caseListService.updateCase(caseDto));
    }
}

 

 DTO   : 
Data   Transfer   Object数据传输对象 
主要用于远程调用等需要大量传输对象的地方。 
比如我们一张表有100个字段,那么对应的PO就有100个属性。 
但是我们界面上只要显示10个字段, 
客户端用WEB   service来获取数据,没有必要把整个PO对象传递到客户端, 
这时我们就可以用只有这10个属性的DTO来传递结果到客户端,这样也不会暴露服务端表结构.到达客户端以后,如果用这个对象来对应界面显示,那此时它的身份就转为VO 

 po 数据库对应的字段   dto需要传输的字段   vo 需要显示的字段   

DAO是data access object 数据库访问对象  可以把pojo转化为po persist object 持久化对象 po 可以封装成 vo需要显示的数据  dto需要传输的数据  

service 层

import com.choosefine.automatedtest.cases.caselist.dao.CaseListMapper;   
import com.choosefine.automatedtest.cases.caselist.dao.CaseStepMapper;
import com.choosefine.automatedtest.cases.caselist.dto.CaseDto;
import com.choosefine.automatedtest.cases.caselist.model.Case;
import com.choosefine.automatedtest.cases.caselist.model.CaseStep;
import com.choosefine.automatedtest.cases.caselist.vo.CaseListPage;
import com.choosefine.automatedtest.cases.caselist.vo.CaseVo;
import com.choosefine.automatedtest.cases.expect.model.Expect;
import com.choosefine.automatedtest.cases.predata.model.PreData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired; //Autowired 
import org.springframework.stereotype.Service;  //@service 
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;


/**
 * Created by Administrator on 2017/7/14.
 */

@Service
public class CaseListService {
    @Autowired       
    CaseListMapper caseListMapper;  //dao
    @Autowired
    CaseStepMapper caseStepMapper;  //dao

    public CaseListPage getCase(String caseNumber, String caseName, Integer caseType,String operator, Integer pageSize, Integer pageNum){  //6个参数
        PageHelper.startPage(pageNum,pageSize);
        List<Case> list = caseListMapper.getCase(caseNumber,caseName,caseType,operator);
        //todo 根据typeId查到对应的case_type_name,把所有的caseList重新放到caseVo里
        List<CaseVo> caseVos = new ArrayList<CaseVo>();
        for(int i = 0;i < list.size();i ++){
            CaseVo caseVo = new CaseVo();
            caseVo.setCaseId(list.get(i).getCaseId());
            caseVo.setCaseName(list.get(i).getCaseName());
            caseVo.setCaseNumber(list.get(i).getCaseNumber());
            caseVo.setCaseType(list.get(i).getCaseType());
            caseVo.setCreateTime(list.get(i).getCreateTime());
            caseVo.setOperator(list.get(i).getOperator());
            caseVo.setIsValid(list.get(i).getIsValid());
            caseVo.setCaseTypeName(caseListMapper.getTypeName(list.get(i).getCaseType()));
            caseVos.add(i,caseVo);
        }
        PageInfo pageInfo = new PageInfo<>(list);
        CaseListPage caseListPage = new CaseListPage();
        caseListPage.setList(caseVos);
        caseListPage.setPageSize(pageInfo.getPageSize());
        caseListPage.setPageNum(pageInfo.getPageNum());
        caseListPage.setTotalRows(pageInfo.getTotal());
        caseListPage.setTotalPage(pageInfo.getPages());
        return caseListPage;
    }

    @Transactional   //事物  要么做要么都不做
    public int addCase(CaseDto caseDto){  
        //todo 判断是否有重复 byname,若有返回 0
        List<CaseStep> caseSteps = caseDto.getStep();
        List<String> steps = new ArrayList<>();
        for(int i = 0;i < caseSteps.size();i ++){
            steps.add(i,caseSteps.get(i).getByname());
        }
        boolean isRepeat = steps.size() != new HashSet<String>(steps).size();
        if(isRepeat){
            return 0;
        }
        Case aCase = new Case();
        aCase.setCaseName(caseDto.getCaseName());
        aCase.setCaseNumber(caseDto.getCaseNumber());
        aCase.setCaseType(caseDto.getCaseType());
        aCase.setCreateTime(new Date());

        int insertCaseNum = caseListMapper.insertSelective(aCase);
        int caseId = caseListMapper.selectId(caseDto.getCaseNumber());

        CaseStep caseStep = null;
        for(int i = 0;i < caseDto.getStep().size();i ++){
            caseStep = new CaseStep();
            caseStep.setStep(caseDto.getStep().get(i).getStep());
            caseStep.setByname(caseDto.getStep().get(i).getByname());
            caseStep.setExpectResult(caseDto.getStep().get(i).getExpectResult());
            caseStep.setCaseId(caseId);
            caseStepMapper.insertSelective(caseStep);
        }
        return insertCaseNum;
    }

    public int deleteCase(Integer caseId){
        List<CaseStep> list = caseStepMapper.findCaseStep(caseId);
        if(list.size() == 0){
            return caseStepMapper.updateSingleCase(caseId);
        }else{
            return caseStepMapper.updateCase(caseId);
        }
    }

    public List<CaseStep> getCaseStep(Integer caseId){
        return caseStepMapper.findCaseStep(caseId);
    }

    @Transactional    // 事物要么做要么都不做
    public int updateCase(CaseDto caseDto){  // dto是传输数据的一个类
        //todo 判断是否有重复 byname,若有返回 0
        List<CaseStep> caseSteps = caseDto.getStep();
        List<String> steps = new ArrayList<>();
        for(int i = 0;i < caseSteps.size();i ++){
            steps.add(i,caseSteps.get(i).getByname());
        }
        boolean isRepeat = steps.size() != new HashSet<String>(steps).size();
        if(isRepeat){
            return 0;
        }

        Case aCase = new Case();
        aCase.setCaseId(caseDto.getCaseId());
        aCase.setCaseNumber(caseDto.getCaseNumber());
        aCase.setCaseName(caseDto.getCaseName());
        aCase.setCaseType(caseDto.getCaseType());
        int updateNum = caseListMapper.updateByPrimaryKeySelective(aCase);
        //todo 更新 case_list 再更新 pre_data 中关联的 caseName 和 caseNumber
        List<PreData> preData = caseListMapper.findPreData(aCase.getCaseId());
        if(preData.size() != 0){
            int updatePreDataCase = caseListMapper.updatePreDataCase(aCase.getCaseName(),aCase.getCaseNumber(),aCase.getCaseId());
        }
        List<Expect> expects = caseListMapper.findExpect(aCase.getCaseId());
        if(expects.size() != 0){
            int updateExpectCase = caseListMapper.updateExpectCase(aCase.getCaseName(),aCase.getCaseNumber(),aCase.getCaseId());
        }

        CaseStep caseStep = null;
        for(int i = 0;i < caseDto.getStep().size();i ++){
            caseStep = new CaseStep();
            caseStep.setCaseId(aCase.getCaseId());
            caseStep.setId(caseDto.getStep().get(i).getId());
            caseStep.setStep(caseDto.getStep().get(i).getStep());
            caseStep.setByname(caseDto.getStep().get(i).getByname());
            caseStep.setExpectResult(caseDto.getStep().get(i).getExpectResult());
            caseStep.setIsValid(caseDto.getStep().get(i).getIsValid());
            if(caseStep.getId() == null){
                int insertStepNum = caseStepMapper.insertSelective(caseStep);
            }else{
                int updateStepNum = caseStepMapper.updateByPrimaryKeySelective(caseStep);
            }
        }
        return updateNum;
    }
}

 

 model层case类  对应的数据库字段 对应case_list表

import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;

/**
 * Created by Administrator on 2017/7/14.
 */

@Table(name = "case_list")
public class Case {
    @Id
    private Integer caseId;
    private String caseNumber;
    private String caseName;
    private Integer caseType;
    private String operator;
    private Date createTime;
    private Integer isValid;

    public Integer getIsValid() {
        return isValid;
    }

    public void setIsValid(Integer isValid) {
        this.isValid = isValid;
    }

    public String getCaseNumber() {
        return caseNumber;
    }

    public void setCaseNumber(String caseNumber) {
        this.caseNumber = caseNumber;
    }

    public String getCaseName() {
        return caseName;
    }

    public void setCaseName(String caseName) {
        this.caseName = caseName;
    }

    public Integer getCaseType() {
        return caseType;
    }

    public void setCaseType(Integer caseType) {
        this.caseType = caseType;
    }

    public String getOperator() {
        return operator;
    }

    public void setOperator(String operator) {
        this.operator = operator;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Integer getCaseId() {
        return caseId;
    }

    public void setCaseId(Integer caseId) {
        this.caseId = caseId;
    }
}

 

dao 层

import com.choosefine.automatedtest.cases.caselist.dynamicsql.DynamicSql;
import com.choosefine.automatedtest.cases.caselist.model.Case;
import com.choosefine.automatedtest.cases.expect.model.Expect;
import com.choosefine.automatedtest.cases.predata.model.PreData;
import com.choosefine.automatedtest.common.dao.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;


/**
 * Created by Administrator on 2017/7/14.
 */

@Mapper
public interface CaseListMapper extends BaseMapper<Case> {
    @SelectProvider(          //动态sql 
            type = DynamicSql.class,
            method = "dynamicSql"
    )
    public List<Case> getCase(   //获取getCase是个list<case>放的一个类
            @Param("caseNumber") String caseNumber, @Param("caseName") String caseName, @Param("caseType") Integer caseType,@Param("operator") String operator);

    @Select("select case_id from case_list where case_number = #{caseNumber} and is_valid = 1")
    int selectId(@Param("caseNumber") String caseNumber);

    @Select("select * from pre_data where case_id = #{caseId} and is_valid = 1")
    List<PreData> findPreData(@Param("caseId") Integer caseId);

    @Update("update pre_data set case_name = #{caseName},case_number = #{caseNumber} where case_id = #{caseId}")
    int updatePreDataCase(@Param("caseName") String caseName, @Param("caseNumber") String caseNumber, @Param("caseId") Integer caseId);

    @Select("select * from expect where case_id = #{caseId} and is_valid = 1")
    List<Expect> findExpect(@Param("caseId") Integer caseId);

    @Update("update expect set case_name = #{caseName},case_number = #{caseNumber} where case_id = #{caseId}")
    int updateExpectCase(@Param("caseName") String caseName, @Param("caseNumber") String caseNumber, @Param("caseId") Integer caseId);

    @Select("select case_type_name from case_type where id = #{id} and is_valid = 1")
    String getTypeName(@Param("id") Integer id);
}

 

动态sql 

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;


/**
 * Created by Administrator on 2017/7/17.
 */
public class DynamicSql{
    public String dynamicSql(@Param("caseNumber") String caseNumber, @Param("caseName") String caseName,@Param("caseType") Integer caseType, @Param("operator")  String operator){
        SQL sql = new SQL();
        sql.SELECT("*");
        sql.FROM("case_list");
        if(caseNumber != null && !caseNumber.equals("")){
            sql.WHERE("case_number LIKE CONCAT(CONCAT('%', #{caseNumber}), '%')");
        }
        if(caseName != null && !caseName.equals("")){
            sql.WHERE("case_name LIKE CONCAT(CONCAT('%', #{caseName}), '%')");
        }
        if(caseType != null && !caseType.equals("")){
            sql.WHERE("case_type = #{caseType}");
        }
        if(operator != null && !operator.equals("")){
            sql.WHERE("operator LIKE CONCAT(CONCAT('%', #{operator}), '%')");
        }
        sql.WHERE("is_valid = 1");
        sql.ORDER_BY("create_time desc");

        return sql.toString();
    }
}

 

 

..

posted on 2017-08-15 15:41  虫儿aqa  阅读(510)  评论(0编辑  收藏  举报

导航