感觉Mybatis用Map接收结果集挺强的
1、Map接收需要指定MapKey,通常为主键
/**
* 销售榜单
* @param year
* @param month
* @return
*/
@MapKey("id")
List<Map<String, Object>> salesList(@Param("year") String year,@Param("month") String month);
2、分组查询,根据产品名称分组。
<!--销售榜单-->
<select id="salesList" resultType="java.util.Map">
select products.name,SUM(orderitem.buynum) as totalsalnum
from orders,products,orderItem
where orders.id=orderItem.order_id
and products.id=orderItem.product_id
and orders.paystate=1
and year(ordertime)=#{year}
and month(ordertime)=#{month}
group by products.name
order by totalsalnum desc
</select>
3、年函数,月函数
3.1、测试SQL
SELECT month(orders.ordertime) from orders;
SELECT year(orders.ordertime) from orders;
SELECT orders.ordertime from orders;
3.2、结果
4、测试
@Test
public void salesList(){
//select products.name,SUM(orderitem.buynum) as totalsalnum
// from orders,products,orderItem
// where orders.id=orderItem.order_id and products.id=orderItem.product_id
// and orders.paystate=1
// and year(ordertime)=? and month(ordertime)=?
// group by products.name
// order by totalsalnum desc
//Parameters: 2016(String), 2(String)
List<Map<String, Object>> mapList = productDao.salesList("2016", "2");
mapList.forEach(map-> {
Set<String> keySet = map.keySet();
keySet.forEach(key-> System.out.println(key +"------"+ map.get(key)));
Object name = map.get("name");
//name------培育男孩
//totalsalnum------1
//name------学会宽容
//totalsalnum------1
});