美文网首页
TestLinkJ - 04 实现第一个接口

TestLinkJ - 04 实现第一个接口

作者: antony已经被占用 | 来源:发表于2019-08-25 21:41 被阅读0次

业务需求分析 - 关键字Ketwords

根据对于TestLink RestAPI的分析,可以发现其接口主要是关于testproject这个最大的业务单元以及隶属于testproject 的测试用例相关的业务对象之间的互操作。如果要直接提供类似服务接口,看上去是比较复杂的。因此,我们从数据库出发,找一个比较简单和独立的业务对象,为其操作提供一个http rest api ,作为第一个实现的接口。
经过观察,发现关键字Keywords是一个符合上述要求的简单业务对象。关键字是作为测试项目/测试用例的一个属性而存在。用户可以自定义关键字,并且在新建和更新用例时,将关键字与用例进行关联。
因此,至少需要提供查询关键字和新增关键的接口。

建表语句

CREATE TABLE /*prefix*/keywords (
  `id` int(10) unsigned NOT NULL auto_increment,
  `keyword` varchar(100) NOT NULL default '',
  `testproject_id` int(10) unsigned NOT NULL default '0',
  `notes` text,
  PRIMARY KEY  (`id`),
  KEY /*prefix*/testproject_id (`testproject_id`),
  KEY /*prefix*/keyword (`keyword`),
  UNIQUE KEY /*prefix*/keyword_testproject_id (`keyword`,`testproject_id`)  
) DEFAULT CHARSET=utf8;

从建表语句中可以看出,关键字的主键是id, 且关键字是testproject项目内生效的,不能跨项目使用。

Mapper

首先根据表结构反推出来业务对象类Keywords

package com.testlink4j.domain;

public class Keywords {
    /**
     * keywords
     *  since 07/11/2019.
     * @author  Antony
     * 
     */

        private int id;
        private String keyword ="";
        private int testproject_id =0;
        private String notes;
        
    public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getKeyword() {
            return keyword;
        }

        public void setKeyword(String keyword) {
            this.keyword = keyword;
        }

        public int getTestproject_id() {
            return testproject_id;
        }

        public void setTestproject_id(int testproject_id) {
            this.testproject_id = testproject_id;
        }

        public String getNotes() {
            return notes;
        }

        public void setNotes(String notes) {
            this.notes = notes;
        }

    public Keywords(int id,String keyword,int testproject_id,String notes) {
        this.id=id;
        this.keyword=keyword;
        this.testproject_id=testproject_id;
        this.notes=notes;
    }
}

DAO 层和Mapper.xml

然后是数据操作层的接口

package com.testlink4j.dao;

import org.apache.ibatis.annotations.Param;
import com.testlink4j.domain.Keywords;

/**
 *  Keywords Dao
 *
 * @author Antony
 */
public interface KeywordsDao {

    /**
     *  
     *
     * @param keyword
     */
    Keywords findByKeyword(@Param("keyword") String keyword);
}

接着在src/mian/resouces下新增
mapper/KeywordsMapper.xml的文件,提供了对keywords表的具体操作。


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.testlink4j.dao.KeywordsDao">
    <resultMap id="BaseResultMap" type="com.testlink4j.domain.Keywords">
        <result column="id" property="id" />
        <result column="keyword" property="keyword" />
        <result column="testproject_id" property="testproject_id" />
        <result column="notes" property="notes" />
    </resultMap>

    <sql id="Base_Column_List">
        id, keyword, testproject_id, notes
    </sql>

    <select id="findByKeyword" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List" />
        from keywords
        where keyword = #{keyword}
    </select>

</mapper>

Service层

完成了Mapper和DAO之后,就可以写服务类了。先定义服务接口

package com.testlink4j.service;

import com.testlink4j.domain.Keywords;

/**
 * Keywords
 *
 * @author Antony
 * 
 */
public interface KeywordsService {

    /**
     * query Keyword infor
     * @param keyword
     */
    Keywords findKeywordByName(String keyword);
}

然后是具体实现

package com.testlink4j.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.testlink4j.dao.KeywordsDao;
import com.testlink4j.domain.Keywords;
import com.testlink4j.service.KeywordsService;

/**
 * KeywordsService Implement
 *
 * @author Antony
 * 
 */
@Service
public class KeywordsServiceImpl implements KeywordsService {

    @Autowired
    private KeywordsDao keywordsDao;

    @Override
    public Keywords findKeywordByName(String keyword) {
        return keywordsDao.findByKeyword(keyword);
    }
}

Controller

最后,就是提供一个Controller给外部调用了。 先实现的是一个查询接口,因此用get方式实现。

package com.testlink4j.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.testlink4j.domain.Keywords;
import com.testlink4j.service.KeywordsService;

/**
 * Keywords RestController
 *
 * @author Antony
 * 
 */
@RestController
public class KeywordsRestController {

    @Autowired
    private KeywordsService keywordsService;

    @RequestMapping(value = "/api/keyword", method = RequestMethod.GET)
    public Keywords findKeyword(@RequestParam(value = "keyword", required = true) String keyword) {
        return keywordsService.findKeywordByName(keyword);
    }   
}

至此,一个简单的http restful接口就实现了。可以通过类似"/api/keyword?keyword=aaa" 的方式来获取某个keyword的具体信息。
后续,我们将引入单元测试和集成测试来测试这个接口服务。

相关文章

  • TestLinkJ - 04 实现第一个接口

    业务需求分析 - 关键字Ketwords 根据对于TestLink RestAPI的分析,可以发现其接口主要是关于...

  • TestLinkJ -02- rest api v2 list

    在着手开始TestLinkJ的开发之前,先来分析下TestLink原生提供的restful api包括了哪些接口。...

  • 第一个java 接口自动化程序

    第一个Java实现的接口测试 御都字数 1065 · 阅读 02019-04-09 08:14一、背景 使用Htt...

  • 00 - TestLinkJ ?

    目标: 实现testlink的迁移,通过这个真实的案例来介绍基于SpringBoot的单元和集成测试。 将test...

  • 适配器模式

    适配器模式指的就是两个完全不相关的接口整合到一块,实现方式就是用第一个接口的实现类实现第二个接口,在这个实现类中传...

  • vue+node全栈移动商城【4】-创建get、send接口,接

    上一节里咱们已经实现了第一个接口,并在请求接口时收到了返回的{a:123},那么接下来,咱们就实现一个简单的get...

  • 手敲数据结构——LinkedList、LinkedListSet

    LinkedList实现 LinkedListSet实现 Set接口 LinkedListMap实现 Map接口

  • 接口

    接口 必须知道的接口特性 接口不可以被实例化 实现类必须实现接口的所有方法 实现类可以实现多个接口 接口中的变量都...

  • Mybatis 实现DAO接口的两种方式

    Mybatis实现DAO接口 Mapper自动实现DAO接口API 编程方式实现DAO接口 :实现DAO 的api...

  • golang-接口

    Go语言规定,只要实现了接口里面的方法,就是该接口的实现类 定义一个接口 接口实现1 接口实现 2 main方法

网友评论

      本文标题:TestLinkJ - 04 实现第一个接口

      本文链接:https://www.haomeiwen.com/subject/gackectx.html