首页 新闻 会员 周边 捐助

Mybatis处理多个Sum()结果返回null

0
悬赏园豆:20 [已解决问题] 解决于 2018-08-23 20:23

如下面代码所示,sql可以正常返回求和信息,mybatis为什么会回null ,是不是我的mapper映射有问题???

  • dao
public class CaseListInfo {
    private Integer id;

    private String courtName;

    private String departName;

    private String judgeName;

    private Integer info1;

    private Integer info2;

    private Integer info3;

    private Integer info4;

    private Integer info5;

    private Integer info6;

    private Integer info7;

    private Integer info8;

    private Integer info9;

    private Integer info10;

    private Integer info11;

    private Integer info12;

    private String sMonth;

    private String sYear;
......
}
  • sql语句,正常返回求和
SELECT
    court_name,
    depart_name,
    SUM(info_1),
    SUM(info_2)
FROM
    caselist_info
WHERE
    court_name = '***人民法院'
AND id IS NOT NULL
GROUP BY
    court_name,
    depart_name
  • Mappr 文件
  <resultMap id="departByCourtNameMap" type="com.yshow.web.pojo.CaseListInfo">
        <result column="id" property="id" jdbcType="INTEGER"/>
        <result column="court_name" property="courtName" jdbcType="VARCHAR"/>
        <result column="depart_name" property="departName" jdbcType="VARCHAR"/>
        <result column="info_1" property="info1" jdbcType="INTEGER"/>
        <result column="info_2" property="info2" jdbcType="INTEGER"/>
        <result column="info_3" property="info3" jdbcType="INTEGER"/>
        <result column="info_4" property="info4" jdbcType="INTEGER"/>
        <result column="info_5" property="info5" jdbcType="INTEGER"/>
        <result column="info_6" property="info6" jdbcType="INTEGER"/>
        <result column="info_7" property="info7" jdbcType="INTEGER"/>
        <result column="info_8" property="info8" jdbcType="INTEGER"/>
        <result column="info_9" property="info9" jdbcType="INTEGER"/>
        <result column="info_10" property="info10" jdbcType="INTEGER"/>
        <result column="info_11" property="info11" jdbcType="INTEGER"/>
        <result column="info_12" property="info12" jdbcType="INTEGER"/>
        <result column="s_month" property="sMonth" jdbcType="VARCHAR"/>
        <result column="s_year" property="sYear" jdbcType="VARCHAR"/>
    </resultMap>
    <select id="selectDepartByCourtNameThisMonth" resultMap="departByCourtNameMap" parameterType="string">
        SELECT
        id, court_name, depart_name,
        SUM(info_1) , SUM(info_2),
        SUM(info_3), SUM(info_4), SUM(info_5),
        SUM(info_6), SUM(info_7), SUM(info_8),
        SUM(info_9), SUM(info_10), SUM(info_11),
        SUM(info_12), s_month, s_year
        FROM caselist_info
        <where>
            <if test="courtName != null">
                and court_name = #{courtName}
            </if>
        </where>
        GROUP BY
        court_name,
        depart_name
    </select>
且听风吟_z的主页 且听风吟_z | 初学一级 | 园豆:48
提问于:2018-08-21 20:09
< >
分享
最佳答案
0

查询的结果集与result的column名对不上,可以在sum(...)添加别名。mapper中的sql改为:

SELECT id, court_name, depart_name, SUM(info_1) info_1, SUM(info_2) info_2, SUM(info_3) info_3, SUM(info_4) info_4, SUM(info_5) info_5, SUM(info_6) info_6, SUM(info_7) info_7, SUM(info_8) info_8, SUM(info_9) info_9, SUM(info_10) info_10, SUM(info_11) info_11, SUM(info_12) info_12, s_month, s_year FROM caselist_info <where> <if test="courtName != null"> and court_name = #{courtName} </if> </where> GROUP BY court_name, depart_name

收获园豆:15
风之神 | 菜鸟二级 |园豆:219 | 2018-08-22 18:47

没想到是这个问题,完美解决,谢谢~~

且听风吟_z | 园豆:48 (初学一级) | 2018-08-23 20:22
其他回答(1)
0

是不是查出来就是空的,建议判断转下

mysql sum 空值null 如何转0 ?

 

收获园豆:5
不会摇头的风扇 | 园豆:443 (菜鸟二级) | 2018-08-23 11:18

查出来是对的,已按一楼方法解决~谢谢

支持(0) 反对(0) 且听风吟_z | 园豆:48 (初学一级) | 2018-08-23 20:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册