给EmpMapper开放Restful接口
阅读原文时间:2023年07月10日阅读:1

本文例程下载:https://files.cnblogs.com/files/xiandedanteng/gatling20200428-3.zip

接口控制器代码如下:

请求url和响应都写在了每个接口的注释上。

package com.ufo.gatling.ctrl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ufo.gatling.entity.Emp;
import com.ufo.gatling.mapper.EmpMapper;

@RestController
@RequestMapping("/emp")
public class EmpCtrl {
@Autowired
private EmpMapper empMapper=null;

// http://localhost:8080/emp/test  
// Hello Gatling  
@RequestMapping("/test")  
String test() {  
    return "Hello Gatling";  
}

// http://localhost:8080/emp/  
// \[{"id":1,"name":"Andy","salary":10000},{"id":2,"name":"Bill","salary":12000},{"id":3,"name":"Cindy","salary":16000},{"id":4,"name":"Douglas","salary":17500},{"id":5,"name":"Eliot","salary":18000},{"id":6,"name":"Felix","salary":20000}\]  
@RequestMapping("/")  
List<Emp> findAll() {  
    return empMapper.findAll();  
}

// http://localhost:8080/emp/findbyid/5  
// {"id":5,"name":"Eliot","salary":18000}  
@RequestMapping("/findbyid/{id}")  
Emp findById(@PathVariable("id") long id) {  
    return empMapper.findById(id);  
}

// http://localhost:8080/emp/updateById/1/gatesss/12345  
// ok  
@RequestMapping("/updateById/{id}/{name}/{salary}")  
String updateById(@PathVariable("id") long id,  
              @PathVariable("name") String name,  
              @PathVariable("salary") int salary) {

    Emp emp=new Emp();  
    emp.setId(id);  
    emp.setName(name);  
    emp.setSalary(salary);

    int changedCount=empMapper.updateById(emp);

    return changedCount==1?"ok":"ng";  
}

// http://localhost:8080/emp/deleteById/1  
// ok  
@RequestMapping("/deleteById/{id}")  
String deleteById(@PathVariable("id") long id) {  
    Emp emp=new Emp();  
    emp.setId(id);  
    int changedCount=empMapper.deleteById(emp);  
    return changedCount==1?"ok":"ng";  
}

// http://localhost:8080/emp/insert/1/ufo/99999  
// ok  
@RequestMapping("/insert/{id}/{name}/{salary}")  
String insert(@PathVariable("id") long id,  
              @PathVariable("name") String name,  
              @PathVariable("salary") int salary) {

    Emp emp=new Emp();  
    emp.setId(id);  
    emp.setName(name);  
    emp.setSalary(salary);

    int changedCount=empMapper.insert(emp);

    return changedCount==1?"ok":"ng";  
}

// http://localhost:8080/emp/findHighLevelEmps  
// \[{"id":3,"name":"Cindy","salary":16000},{"id":4,"name":"Douglas","salary":17500},{"id":5,"name":"Eliot","salary":18000},{"id":6,"name":"Felix","salary":20000},{"id":1,"name":"ufo","salary":99999}\]  
@RequestMapping("/findHighLevelEmps")  
List<Emp> findHighLevelEmps() {  
    return empMapper.findHighLevelEmps();  
}  

}

--2020-04-28--