用Mybatis返回Map,List<Map>

返回Map,Mybatis配置如下 :

<select id="getCountyHashMap" resultType="java.util.HashMap">
    select name,id from
    tsql_test_region where
    id=#{id}
  </select>

ServiceImpl如下 :

public Map<String, Long> getCountyHashMap(long id) {
    Map<String, Object> regionMap = regionInfoMapper.getCountyHashMap(id);
    Map<String, Long> resultMap = new HashMap<String, Long>();
    String region = null;
    Long vid = null;
    for (Map.Entry<String, Object> entry : regionMap.entrySet()) {
      if ("NAME".equals(entry.getKey())) {
        region = (String) entry.getValue();
      } else if ("ID".equals(entry.getKey())) {
        vid = ((java.math.BigDecimal) entry.getValue()).longValue();
      }
    }
    resultMap.put(region, vid);
    return resultMap;
  }

Controller如下 :

@RequestMapping(value = "/region3", method = RequestMethod.GET)
  public @ResponseBody
  Map<String, Long> getCountyMap(@RequestParam(required = true) int regionId) {
    return regionInfoService.getCountyHashMap(regionId);
  }

结果为 :

     返回List<Map>类似 :

Mybatis配置 :

<select id="getRegionHashMap" resultType="java.util.HashMap">
    select name,id from
    tsql_test_region order by id
  </select>

ServiceImpl如下 :

public Map<String, Long> getRegionHashMap() {
    List<Map<String, Object>> regionMap = regionInfoMapper
        .getRegionHashMap();
    Map<String, Long> resultMap = new HashMap<String, Long>();
    for (Map<String, Object> map : regionMap) {
      String region = null;
      Long id = null;
      for (Map.Entry<String, Object> entry : map.entrySet()) {
        if ("NAME".equals(entry.getKey())) {
          region = (String) entry.getValue();
        } else if ("ID".equals(entry.getKey())) {
          id = ((java.math.BigDecimal) entry.getValue()).longValue();
        }
      }
      resultMap.put(region, id);
    }
    return resultMap;
  }

Controller如下 :

@RequestMapping(value = "/region2", method = RequestMethod.GET)
  public @ResponseBody
  Map<String, Long> getRegionMap() {
    return regionInfoService.getRegionHashMap();
  }

结果为 :

时间: 2025-01-16 13:01:47

用Mybatis返回Map,List<Map>的相关文章

myBatis中的注解@Param、返回值为Map、JAVA读取Excel并解析文本、Class.getResource()和ClassLoader.getResource()

myBatis中的注解@Param:http://blog.csdn.net/gao36951/article/details/44258217:  http://www.cnblogs.com/thomas12112406/p/6217211.html. myBatis返回值为Map:http://blog.csdn.net/werewr342352321df/article/details/11892755. ====================== JAVA读取Excel并解析文本:h

mybatis返回集合类型为map时

Mybatis返回值为map时 使用条件:当映射属性超出了一个Bean时: 第一步: 第二步: 第三步: 此方式只能当返回值为lIst集合时. 原文地址:https://www.cnblogs.com/xiaofengshan/p/12347989.html

MyBatis 返回Map&lt;String,Object&gt;类型

<resultMap type="map" id="stringMap"> <result property="contentIntr" column="contentIntr" javaType="string" /> </resultMap> <!-- 导出所有数据 --> <select id="exportAll" resul

Mybatis返回Map的一种实现

Mybatis返回Map的一种实现 前言 在使用Mybatis进行系统开发的时候,有时候我们会有这么一种需求:我们希望通过Mybatis查询某一个表返回的结果是一个Map,而这个Map的Key是表的一个字段,Value是另一个字段.然而当我们按照Mybatis的做法,指定查询Mapper语句的resultType为map时返回的结果是一个Map列表(表中有多条记录时),而且每个元素Map对应的是表的一行记录(Key为每个字段的名称,Value为对应的值),这跟我们的需求是不相符合的.那有什么方法

Mybatis,返回Map的时候,将Map内的Key转换为驼峰的命名

每次使用mybatis的时候,简单的连表查询,用Map接收的时候,都是像DB定义的字段一样,类似以下 student_name,student_id,没有转换为驼峰,但是又不能因为这一个定义一个javabean来映射数据库字段集合,这样,会有无穷无尽的javabean,完全不是办法. 然后我看了下mybatis-spring-boot的配置文档http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

mybatis返回map类型数据空值字段不显示的解决方法

在日常开发中,查询数据返回类型为map,数据库中有些自动值为null,则返回的结果中没有值为空的字段,则如何显示值为空的字段呢? Spring boot + MyBatis返回map中null值默认不显示,如要调整为null值显示需要在配置文件中添加属性,如下图红框中所示: 2.Mybatis使用IFNULL(P1,P2)函数 原文地址:https://www.cnblogs.com/panchanggui/p/10919421.html

处理Mybatis返回的结果集为Map类型

最有用到mybatis返回一个map结果集,然后就针对性的在网上找了一些相关大牛的总结:1.https://www.cnblogs.com/jwdd/p/10046270.html2.https://www.codercto.com/a/33475.html 然后经过自己的测试终于可以应用实际业务中1.如果你确定返回的数据只有一条,你可以这样整 xml中: <select id="searchncomedateByInvestID" resultMap="java.ut

mybatis 返回值类型是Map

<select id="selectByMemberKey" resultType="java.util.HashMap"> SELECT member_id ,expire FROM member_key_make_up WHERE member_key = #{memberKey,jdbcType=VARCHAR} </select> public interface MemberKeyMakeUpMapper { HashMap<

深入了解MyBatis返回值

深入了解MyBatis返回值 想了解返回值,我们需要了解resultType,resultMap以及接口方法中定义的返回值. 我们先看resultType和resultMap resultType和resultMap 大家应该都知道在MyBatis的<select>标签中有两种设置返回值的方式,分别是resultMap和resultType. 处理resultMap和resultType的代码如下: private void setStatementResultMap( String resu

java Map及Map.Entry详解及用途

Map是java中的接口,Map.Entry是Map的一个内部接口. Map提供了一些常用方法,如keySet().entrySet()等方法. keySet()方法返回值是Map中key值的集合:entrySet()的返回值也是返回一个Set集合,此集合的类型为Map.Entry. Map.Entry是Map声明的一个内部接口,此接口为泛型,定义为Entry<K,V>.它表示Map中的一个实体(一个key-value对).接口中有getKey(),getValue方法. 由以上可以得出,遍历