tim工具包-自动生成api接口

相关文章:

  • tim工具包-dao层查询工具
  • tim工具包-sql管理平台-admin
  • tim工具包-自动生成api接口
  • tim工具包-MyMath牛逼的计算工具
  • tim工具包-数据处理工具

配合dao 工具 ApiSql 自动生成接口


import com.utils.ApiSql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
public class ApiController extends BaseController {
    @Autowired
    ApiSql apiSql;

    @RequestMapping(value = "/api/getItem/{groups}/{codes}", produces = {"application/json"}, consumes = {
            "application/json"})
    public Object getItem(@RequestBody Map param, @PathVariable(name = "groups", required = true) String groups,
                          @PathVariable(name = "codes", required = true) String codes) throws Throwable {
        Map runSqlByParamItem = apiSql.runSqlByParamItem(param, groups, codes);
        return doOutData(runSqlByParamItem);
    }

    @RequestMapping(value = "/api/getList/{groups}/{codes}", produces = {"application/json"}, consumes = {
            "application/json"})
    public Object getList(@RequestBody Map param, @PathVariable(name = "groups", required = true) String groups,
                          @PathVariable(name = "codes", required = true) String codes) throws Throwable {
        List> runSqlByParam = apiSql.runSqlByParam(param, groups, codes);
        return doOutData(runSqlByParam);
    }

    @RequestMapping(value = "/api/getPage/{groups}/{codes}", produces = {"application/json"}, consumes = {
            "application/json"})
    public Object exeSql(@RequestBody Map param, @PathVariable(name = "groups", required = true) String groups,
                         @PathVariable(name = "codes", required = true) String codes) throws Throwable {
        try {
            apiSql.exeSql(param, groups, codes);
        } catch (Exception e) {
            return e.getMessage();
        }
        return doOutData("0000");
    }

    @RequestMapping(value = "/api/exeSql/{groups}/{codes}", produces = {"application/json"}, consumes = {
            "application/json"})
    public Object getPage(@RequestBody Map param, @PathVariable(name = "groups", required = true) String groups,
                          @PathVariable(name = "codes", required = true) String codes) throws Throwable {
        Object runSqlByParamPage = apiSql.runSqlByParamPage(param, groups, codes);
        return doOutData(runSqlByParamPage);
    }



    @RequestMapping(value = "/webapi/getItem/{groups}/{codes}")
    public Object getWebItem(HttpServletRequest request, @PathVariable(name = "groups", required = true) String groups,
                             @PathVariable(name = "codes", required = true) String codes) throws Throwable {
        Map param = getParamMap(request);
        Map runSqlByParamItem = apiSql.runSqlByParamItem(param, groups, codes);
        return doOutData(runSqlByParamItem);
    }

    @RequestMapping(value = "/webapi/getList/{groups}/{codes}")
    public Object getWebList(HttpServletRequest request, @PathVariable(name = "groups", required = true) String groups,
                          @PathVariable(name = "codes", required = true) String codes) throws Throwable {
        Map param = getParamMap(request);
        List> runSqlByParam = apiSql.runSqlByParam(param, groups, codes);
        return doOutData(runSqlByParam);
    }

    @RequestMapping(value = "/webapi/getPage/{groups}/{codes}")
    public Object exeSqlWeb(HttpServletRequest request, @PathVariable(name = "groups", required = true) String groups,
                         @PathVariable(name = "codes", required = true) String codes) throws Throwable {
        try {
            Map param = getParamMap(request);
            apiSql.exeSql(param, groups, codes);
        } catch (Exception e) {
            return e.getMessage();
        }
        return doOutData("0000");
    }

    @RequestMapping(value = "/webapi/exeSql/{groups}/{codes}")
    public Object getPageWeb(HttpServletRequest request, @PathVariable(name = "groups", required = true) String groups,
                          @PathVariable(name = "codes", required = true) String codes) throws Throwable {
        Map param = getParamMap(request);
        Object runSqlByParamPage = apiSql.runSqlByParamPage(param, groups, codes);
        return doOutData(runSqlByParamPage);
    }

}

将以上代码拷贝至应用中

根据body 传参调用以下接口

  • getItem
  • getList
  • exeSql
  • getPage

如果是web端调用带web的接口

  • getItemWeb
  • getListWeb
  • exeSqlWeb
  • getPageWeb

例如 http://127.0.0.1:8080/witbuild/api/getList/test/test

  • test test 则为对应数据库中配置的脚本的groups 和codes
  • 如不明白groups 和codes 是什么请查看这一片文章 《tim工具包-dao层查询工具》

一个脚本调用getItem 则查询单个数据
调用getList 则为查询一组数据
getPage 则为分页查询
exeSql 执行脚本内容

你可能感兴趣的:(java,工具)