1. 程式人生 > >Mybatis_ 返回List 結構型別資料

Mybatis_ 返回List 結構型別資料

注意:

不管採用怎麼樣的方法,最終對映的欄位名 會被作為 hashMap 的 key , 

如下面 的 id , value 會被作為hashMap 的 key.

也就是無法直接讓  map<key, value> 直接對映為查詢的結果,即使只有2列

xxx - mapper.xml

resultMap定義

<!-- TODO 測試返回 HashMap-->
  <resultMap id="testResultMap" type="java.util.HashMap">
    <result column="id"  property="id" jdbcType="INTEGER"  javaType="int" />
    <result column="campaign_id" property="value" jdbcType="INTEGER" javaType="int" />
  </resultMap>
查詢mapper
<!-- TODO 返回 List<hashMap> 測試 -->
  <select id="testQueryHashMap" resultMap="testResultMap" >
    select id, campaign_id
    from report_campaign
  </select>


測試程式碼:

@Test
    public void testQueryHashMap()
    {
        CampaignReportMapper campaignReportMapper = sqlSession.getMapper(CampaignReportMapper.class);
        List<Map<String,Object>> result = campaignReportMapper.testQueryHashMap();

        for(int i=0; i< result.size(); i++){
            Map<String,Object> tmp = result.get(i);
            System.out.println("id : " + tmp.get("id"));
            System.out.println("campaign_id : " + tmp.get("campaign_id"));
            System.out.println("------------------ ");

            System.out.println("id : " + tmp.get("id"));
            System.out.println("campaign_id : " + tmp.get("value"));
            System.out.println("------------------ ");

        }
    }


效果: