最近做項目的時候碰到一個問題,查詢一個表單,返回多個字段和函數計算的值,對於mybatis來說返回類型就不好定義了,想了半天,查了很多的資料,
最后成功解決問題,下面詳細介紹一下。
一 需求分析
計算當天所有的評價人數,評價分數,評價次數,表的結構如下:

二 實現
定義一個返回類:
public class SellerAllEvalPo {
private Integer totalScore;
private Integer totalEval;
private Integer totalPeople;
public Integer getTotalScore() {
return totalScore;
}
public void setTotalScore(Integer totalScore) {
this.totalScore = totalScore;
}
public Integer getTotalEval() {
return totalEval;
}
public void setTotalEval(Integer totalEval) {
this.totalEval = totalEval;
}
public Integer getTotalPeople() {
return totalPeople;
}
public void setTotalPeople(Integer totalPeople) {
this.totalPeople = totalPeople;
}
}
sql語句:
<select id="getSellerAllScore" resultType="SellerAllEvalPo">
select sum(eval_num) as total_eval, sum(total_score) as total_score from seller_eval_day
<where>
<if test="startTime != null">
and business_day >= #{startTime}
</if>
<if test="endTime != null">
and business_day <= #{endTime}
</if>
<if test="sellerId != null">
and seller_id = #{sellerId}
</if>
</where>
</select>
成功解決。
