在mybatis中使用MySql查询语句,返回多个数值、count值时可以运用Map
serviceImpl
Map<String,Long> voMap=baseMapper.getCount();
vo.setCountAll(voMap.get("CountAll"));
vo.setCountDatabase(voMap.get("CountDatabase"));
vo.setCountApi(voMap.get("CountApi"));
vo.setCountNoItem(voMap.get("CountNoItem"));
vo.setCountHaveItem(voMap.get("CountHaveItem"));
vo.setCountNoShare(voMap.get("CountNoShare"));
vo.setCountNow(voMap.get("CountNow"));
vo.setCountDay(voMap.get("CountDay"));
dao
Map<String, Long> getCount();
mapper.xml
<select id="getCount" resultType="map">
SELECT
COUNT(*) as CountAll,
COUNT(type='database' OR NULL) as CountDatabase,
COUNT(type!='database' OR NULL) as CountApi,
COUNT(share_type=1 OR NULL) as CountNoItem,
COUNT(share_type=2 OR NULL) as CountHaveItem,
COUNT(share_type=3 OR NULL) as CountNoShare,
COUNT(update_frequ='实时' OR NULL) as CountNow,
COUNT(update_frequ='每日' OR NULL) as CountDay
FROM table
</select>
运用到函数求值返回结果时,如Rounf()...数据类型是BigDecimal。
基础加减乘除,求和,如sum、count...时,返回结果数据类型为Long
需要转换。
SELECT ROUND((Count1/Count2)*100) AS Count3....后面具体语句略
我这里使用的是map作为返回值类型,key对应的sql查询as 后的值,这里为count3
Map<String,Object> toolMap=mallOperationDao.getCount();
BigDecimal count= (BigDecimal) toolMap.get("Count3");
vo.setToolEffect(count.longValue());