业务需求分析 - 关键字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的具体信息。
后续,我们将引入单元测试和集成测试来测试这个接口服务。
网友评论