有時候須要查詢的返回的結果不是javaBean的屬性,這個時候能夠定義resultType="map"java
<select id="statisticsByMonth" resultType="map"> SELECT a.tallyTime,SUM(a.paid) sumPaid FROM( SELECT LOGIN_ID loginId,SUBSTR(TALLY_TIME,1,6) tallyTime,AMOUNT_PAID paid FROM tally) a WHERE a.loginId=${loginId} AND a.tallyTime LIKE '${year}%' GROUP BY a.tallyTime ORDER BY a.tallyTime </select>
好比此處,查詢的時候按照月份來返回每月的支出的總金額.mybatis
2.DAO接口code
public List<Map<Object,Object>> statisticsByMonth( @Param(value="loginId")Integer loginId,@Param(value="year")String year);
mybatis返回的時候會將結果分別封裝在map中,多條結果集就是多個map,能夠用List接收xml
3後臺取值接口
List<Map<Object,Object>> m = tallyDao.statisticsByMonth(loginId, year); for(int i=0;i<m.size();i++){ Map<Object,Object> got = m.get(i);//取出集合中的每個map,map內容是月份-支出總金額的鍵值對 }
先用List接收,而後遍歷,List中的map就是咱們須要查詢的字段值了get