美文网首页
idea后台接口开发如何接收app端传过来的参数

idea后台接口开发如何接收app端传过来的参数

作者: 墨色尘埃 | 来源:发表于2017-08-16 09:53 被阅读0次

    IDEA部分:
    dao层LayerMapper

    public interface LayerMapper {
    
        List<LayerInfo> selectLayerList();
    
        List<PointModel> selectLayerPointList(double latitude0,
                                              double longitude0,
                                              double latitude1,
                                              double longitude1);
    
    //    List<PointModel> selectLayerPointList();
    //    List<PointModel> selectLayerPointList(String tableName);
    //    List<PointModel> selectLayerPointList(@Param("tableName")String tableName);
    //    List<PointModel> selectLayerPointList(Map<String, String> map1);
    
        List<LinkedHashMap<String, Object>> superManagerSelect(String sql);
    }
    

    例如第三个方法superManagerSelect,如何接收sql参数呢?
    接口实现LayerListMapper.xml:

        <select id="superManagerSelect" parameterType="String" resultType="java.util.LinkedHashMap">
            ${value}
        </select>
    

    控制类LayerController:这里的superManagerSelect(@RequestParam String table)其实应该就是在value = "/superManagerSelect"地址后面进行拼接,组成......../superManagerSelect?table=layer_1这样的网址。RequestParam字面意思不就是“ request from向…请求[要求] ”吗

        /**
         * 传入整条sql语句查询
         */
        @RequestMapping(value = "/superManagerSelect", method = RequestMethod.GET)
            public ResponseObj<List<LinkedHashMap<String, Object>>> superManagerSelect(@RequestParam String table) {
            String sql = "SELECT * FROM" + " "+table;
    //        String sql = "SELECT * FROM layer_1 WHERE LATITUDE > #{2} AND #{0} > LATITUDE AND LONGITUDE > #{1} AND #{3}
    // > LONGITUDE";
    
            try {
                List<LinkedHashMap<String, Object>> linkedHashMaps = layerMapper.superManagerSelect(sql);
                return new ResponseObj<>(linkedHashMaps, null);
            } catch (Exception e) {
                e.printStackTrace();
                return new ResponseObj<>(null, new Error("服务器请求异常"));
            }
        }
    

    android studio部分:
    PointApi:
    此例中使用getManagerSelect方法,如果不适用@Query进行拼接,网址是:http://127.0.0.1:10002/layer/superManagerSelect 。使用@Query进行拼接,在浏览器上访问的网址就是:http://127.0.0.1:10002/layer/superManagerSelect?table=layer_1

    public interface PointApi {
        @GET("user1/getLayerConfig")
        Observable<ResponseObj<Map<String, LayerConfig1>>> getLayerConfig();
    
        @GET("layer/selectLayerList")
        Observable<ResponseObj<List<LayerConfig>>> getLayerInfo();
    
        /**
         * 查询在屏幕内的数据
         *
         * @return
         */
        @GET("layer/selectLayerPointList")
        Observable<ResponseObj<Map<String, List<Map<String, Object>>>>> getLayerPointList(
                @Query("latitude0") double latitude0, @Query("longitude0") double longitude0,
                @Query("latitude1") double latitude1, @Query("longitude1") double longitude1);
    
        /**
         * 查询所有数据
         */
    //    @GET("layer/selectLayerPointList")
    //    Observable<ResponseObj<Map<String,List<Map<String, Object>>>>> getLayerPointList();
    
    //    @GET("layer/selectLayerPointList")
    //    Observable<ResponseObj<Map<String,List<Map<String, Object>>>>> getLayerPointList();
    
        @GET("layer/superManagerSelect")
        Observable<ResponseObj<List<LinkedHashMap<String, Object>>>> getManagerSelect(@Query("table")String table);
    
    }
    

    实现:这里调用接口方法getManagerSelect(table)将table参数传进去,对应接口类PointApi中getManagerSelect方法,然后自动根据@GET("layer/superManagerSelect")的地址去对应后台相同的地址,并将参数table带过来给后台使用,后台接收到table参数进行查询.......

        public void getPoints(String table) {
            subScribe = ApiManager.getInstance()
                    .pointApiService()
                    .getManagerSelect(table)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(listResponseObj -> {
                        List<LinkedHashMap<String, Object>> data = listResponseObj.data;
                        Log.e("LayerConfig", "通过查询sql语句查询点获取成功:" + data.get(0).toString());
    
                    }, throwable -> {
                        Log.e("LayerConfig", "通过查询sql语句查询点未获取成功");
                    });
        }
    

    相关文章

      网友评论

          本文标题:idea后台接口开发如何接收app端传过来的参数

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