欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis返回一个map集合

程序员文章站 2022-06-30 20:48:08
...

mybatis中resulttype如下定义即可:

 <!--查询订单状态表,返回一个map集合-->
    <select id="selOrdersState" resultType="java.util.HashMap">
        select state_id,state from order_state
    </select>

mapper接口:

 List<Map<Integer, String>> selOrdersState();

下面是将接收到的map进行处理:

 @Override
    public Map<Integer, String> selOrdersState() {
        Map<Integer, String> stateMap = new HashMap<>();
        List<Map<Integer, String>> mapList = orderMapper.selOrdersState();
        for (Map<Integer, String> map: mapList) {
            String state =null;
            Integer state_id = null;
            for (Map.Entry<Integer, String> maps:map.entrySet()) {
                System.out.println("key"+maps.getKey());
                //System.out.println("key"+maps.getValue());
                if("state_id".equals(maps.getKey())){
                    state_id = new Integer(String.valueOf(maps.getValue()));
                }
                if("state".equals(maps.getKey())){
                    state = String.valueOf(maps.getValue());
                }
                stateMap.put(state_id,state);
            }
        }
        return stateMap ;
    }