zoukankan      html  css  js  c++  java
  • JAVA 基础

    后台给前端传json的时候,在data里想要一个list<map<>>

    查找数据库得到list<String>之后,要用map使字段名和值一一对应,比如:

      字段名 putPlace 值123 

      json格式要求:"data":[{"putPlace":"123"}]

      具体操作:

        1、新建listmap对象。

    List<Map<String, String>> listmap = new ArrayList<>();

        2、遍历list,并且new一个Map<String,String>对象map,使用map.put(,)将键值对放到map中,左边是键,右边是值。

        3、将map放到listmap中,遍历结束。

    for (int i = 0; i < list.size(); i++) {
                Map<String, String> map = new HashMap<>();
                map.put("putPlace", list.get(i));
                listmap.add(map);
            }

    实例:

    repository:

    @Query(value = "select a.put_place 
    " +
                "from casque as a join agent_casque_bind as b on a.id = b.casque_id and b.agent_id = ?1
    " +
                "group by a.put_place",nativeQuery = true)
        List<String> findPutPlace(String userId);

    service:

    
    
    /**
    * 获取代理商头盔投放场所(去重)
    * @param userId 代理商id
    * @return 结果
    */
    public List<Map<String,String>> queryPutPlace(String userId){
            List<String> list = this.placeWatchCountRepository.findPutPlace(userId);
            List<Map<String, String>> listmap = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                Map<String, String> map = new HashMap<>();
                map.put("putPlace", list.get(i));
                listmap.add(map);
            }
            return listmap;
        }

    controller:

    /**
         * 获取代理商头盔投放场所(去重)
         * @param user 当前登陆用户
         * @return 结果
         */
        @GetMapping("/placeWatch/putPlace")
        public Result putPlaceQuery(@CurrentUser TokenModel user){
            Result result = new Result();
            try{
                List<Map<String, String>> list = this.placeWatchService.queryPutPlace(user.getId());
                return result.success(list);
            }catch (Exception e){
                e.printStackTrace();
                return result.failure(ErrorCodeType.FIND_ERR.getMsg());
            }
        }
  • 相关阅读:
    ObjectForScripting 注册
    取消mysql表中timestamp字段的自动更新
    分片与非分片使用聚合的区别
    java类加载过程
    springboot中使用solr8
    第四章
    获取cookie的两种方式和session共享解决方案
    剑指 Offer 57. 和为s的两个数字
    第三章
    第五章
  • 原文地址:https://www.cnblogs.com/Kohinur/p/11389816.html
Copyright © 2011-2022 走看看