mybaits returns a single sum or count, returns multiple sums or multiple counts

1. In our actual development using mybatis, we often need to use SQL to count data, so how do we return the statistical data after writing sql in mybaits? Here are two cases.

Case 1: In the case of multiple sums and counts to be returned.

Map<String, Object> otherProfitSum(@Param("stationId")Integer stationId, @Param("activityId")Integer activityId);

<select id="otherProfitSum" resultType="java.util.HashMap">
    select sum(activity_size)  as activitySizeSum,
    sum(activity_copies) as activityCopiesSum, 
    sum(money) as   moneySum, count(profit) as profitSum 
    from station_other_profit where 
    station_id = #{stationId,jdbcType=INTEGER} and
    activity_id = #{activityId,jdbcType=INTEGER}
  </select>

Note: the as alias in mybatis is the key of Map, and the corresponding value can be obtained through this key

Case 2: In the case of a single sum or count to be returned.

Integer sumOperatingFrozon(@Param("stationId")Integer stationId);

 <select id="sumOperatingFrozon" resultType="java.lang.Integer">
      select sum(freeze_balance) as operatingFrozon
      from station_freeze_balance where station_id = #{stationId} 
  </select>

Note: The type returned by mybatis must be the same as the interface type. Finally, you can call the interface to get the mysql statistical data.

Guess you like

Origin blog.csdn.net/qq_36138652/article/details/90772977
Recommended