需求:我要查询数据库中某一个表的最大值,最小值,平均值,累加和,在mybatis中应当怎么返回
解决方法,写一个实体类,在xml中返回这个实体类,将查询的每一项与实体类的属性相对应
代码如下:
//实体类
public class DataStatistics {
private Float maxData;
private Float minData;
private Float sumData;
private Float avgData;
public Float getMaxData() {
return maxData;
}
public void setMaxData(Float maxData) {
this.maxData = maxData;
}
public Float getMinData() {
return minData;
}
public void setMinData(Float minData) {
this.minData = minData;
}
public Float getSumData() {
return sumData;
}
public void setSumData(Float sumData) {
this.sumData = sumData;
}
public Float getAvgData() {
return avgData;
}
public void setAvgData(Float avgData) {
this.avgData = avgData;
}
@Override
public String toString() {
return "DataStatistics{" +
"maxData=" + maxData +
", minData=" + minData +
", sumData=" + sumData +
", avgData=" + avgData +
'}';
}
}
xml中的sql语句
<select id="dataStatistics" resultType="com.example.auxiliary.DataStatistics" parameterType="String">
select max(${queryParam}) as maxData ,
min(${queryParam}) as minData,
sum(${queryParam}) as sumData,
avg(${queryParam}) as avgData
from region_data
</select>
dao层的mapper
DataStatistics dataStatistics(@Param("queryParam") String queryParam);