给EmpMapper开放Restful接口

自古美人都是妖i 提交于 2020-04-29 15:19:17

本文例程下载: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--

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!