如下面代码所示,sql可以正常返回求和信息,mybatis为什么会回null ,是不是我的mapper映射有问题???
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;
......
}
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
<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>
查询的结果集与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
没想到是这个问题,完美解决,谢谢~~