mybatis 一对多collection标签查询结果却是一对一问题。

nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 2

collection 标签设置如下

resultMap标签省略。。。。。

<collection property="workPlanDayList" ofType="com.fehorizon.wps.attence.model.workPlan.WorkPlanDay">
        <id column="d_id" jdbcType="BIGINT" property="dId" />
        <result column="w_id" jdbcType="BIGINT" property="wId" />
        <result column="acc_id" jdbcType="BIGINT" property="accId" />
        <result column="d_weekday_num" jdbcType="VARCHAR" property="dWeekdayNum" />
        <result column="d_work_plan_date" jdbcType="VARCHAR" property="dWorkPlanDate" />
        <result column="d_work_plan_des_am" jdbcType="VARCHAR" property="dWorkPlanDesAm" />
        <result column="d_work_plan_des_pm" jdbcType="VARCHAR" property="dWorkPlanDesPm" />
        <result column="d_work_review_des_am" jdbcType="VARCHAR" property="dWorkReviewDesAm" />
        <result column="d_work_review_des_pm" jdbcType="VARCHAR" property="dWorkReviewDesPm" />
    </collection>

查询语句

<select id="selectWorkPlanWeek" parameterType="Map" resultMap="BaseResultMap">
    select 
           w.w_year
           ,w.w_week_num
           ,w.w_work_plan_des
           ,w.w_work_review_des
           ,w.w_status
           ,w.w_plan_verified_date
           ,w.w_review_verified_date
           ,w.w_verified_user_name
           ,d.d_weekday_num
           ,d.d_work_plan_date
           ,d.d_work_plan_des_am
           ,d.d_work_plan_des_pm
           ,d.d_work_review_des_am
           ,d.d_work_review_des_pm
    from wps_work_plan_week w
    LEFT JOIN wps_work_plan_day d
    on w.w_id = d.w_id
    where w.w_year = #{wYear}
    and w.w_week_num = #{wWeekNum}
  </select>

 

查询出的结果

{
    "code": 200,
    "msg": "success",
    "data": [
        {
            "accId": null,
            "userName": null,
            "createDate": null,
            "updateDate": null,
            "workPlanDayList": [
                {
                    "accId": null,
                    "userName": null,
                    "createDate": null,
                    "updateDate": null,
                    "dworkReviewDesAm": null,
                    "dreviewVerifiedDate": null,
                    "dworkPlanDesAm": "sfsfsdf",
                    "dplanSubmitDate": null,
                    "dplanVerifiedDate": null,
                    "dverifiedUserName": null,
                    "dworkPlanDesPm": "asfaffasfd",
                    "dworkReviewDesPm": null,
                    "dverifiedAccId": null,
                    "dreviewSubmitDate": null,
                    "did": null,
                    "wid": null,
                    "dweekNum": null,
                    "dyear": null,
                    "dweekdayNum": "1",
                    "dworkPlanDate": "2019-9-25",
                    "dstatus": null
                }
            ],
            "wreviewSubmitDate": null,
            "wreviewVerifiedDate": "1970-01-01 00:00:00",
            "wverifiedAccId": null,
            "wplanSubmitDate": null,
            "wverifiedUserName": null,
            "wplanVerifiedDate": "1970-01-01 00:00:00",
            "wworkReviewDes": null,
            "wid": null,
            "wyear": "1",
            "wweekNum": "1",
            "wworkPlanDes": "sdf",
            "wstatus": "1"
        },
        {
            "accId": null,
            "userName": null,
            "createDate": null,
            "updateDate": null,
            "workPlanDayList": [
                {
                    "accId": null,
                    "userName": null,
                    "createDate": null,
                    "updateDate": null,
                    "dworkReviewDesAm": null,
                    "dreviewVerifiedDate": null,
                    "dworkPlanDesAm": "sdgsdasdg",
                    "dplanSubmitDate": null,
                    "dplanVerifiedDate": null,
                    "dverifiedUserName": null,
                    "dworkPlanDesPm": "asdgdsgdsaggsdgsdagsdfdsfsdfsadfsad",
                    "dworkReviewDesPm": null,
                    "dverifiedAccId": null,
                    "dreviewSubmitDate": null,
                    "did": null,
                    "wid": null,
                    "dweekNum": null,
                    "dyear": null,
                    "dweekdayNum": "1",
                    "dworkPlanDate": "2019-9-26",
                    "dstatus": null
                }
            ],
            "wreviewSubmitDate": null,
            "wreviewVerifiedDate": "1970-01-01 00:00:00",
            "wverifiedAccId": null,
            "wplanSubmitDate": null,
            "wverifiedUserName": null,
            "wplanVerifiedDate": "1970-01-01 00:00:00",
            "wworkReviewDes": null,
            "wid": null,
            "wyear": "1",
            "wweekNum": "1",
            "wworkPlanDes": "sdf",
            "wstatus": "1"
        }
    ],
    "enumVal": {},
    "success": true
}

workPlanDayList集合中只有一条数据,但它的上级有两条workPlanDayList值不一样

 

修改方案:

collection标签中的字段和查询语句中的字段不一致,缺少 d_id和d.w_id

修改后的sql

 <select id="selectWorkPlanWeek" parameterType="Map" resultMap="BaseResultMap">
    select 
   		w.w_year
   		,w.w_week_num
   		,w.w_work_plan_des
   		,w.w_work_review_des
   		,w.w_status
   		,w.w_plan_verified_date
   		,w.w_review_verified_date
   		,w.w_verified_user_name
   		,d.d_id
   		,d.w_id
   		,d.d_weekday_num
   		,d.d_work_plan_date
   		,d.d_work_plan_des_am
   		,d.d_work_plan_des_pm
   		,d.d_work_review_des_am
   		,d.d_work_review_des_pm
    from wps_work_plan_week w
    LEFT JOIN wps_work_plan_day d
    on w.w_id = d.w_id
    where w.w_year = #{wYear}
    and w.w_week_num = #{wWeekNum}
  </select>

查询结果:

{
    "code": 200,
    "msg": "success",
    "data": [
        {
            "accId": null,
            "userName": null,
            "createDate": null,
            "updateDate": null,
            "workPlanDayList": [
                {
                    "accId": null,
                    "userName": null,
                    "createDate": null,
                    "updateDate": null,
                    "dworkPlanDesAm": "sfsfsdf",
                    "dworkPlanDesPm": "asfaffasfd",
                    "dworkReviewDesAm": null,
                    "dplanSubmitDate": null,
                    "dworkReviewDesPm": null,
                    "dplanVerifiedDate": null,
                    "dweekNum": null,
                    "dweekdayNum": "1",
                    "wid": 1,
                    "did": 1,
                    "dyear": null,
                    "dworkPlanDate": "2019-9-25",
                    "dstatus": null,
                    "dverifiedUserName": null,
                    "dreviewVerifiedDate": null,
                    "dreviewSubmitDate": null,
                    "dverifiedAccId": null
                },
                {
                    "accId": null,
                    "userName": null,
                    "createDate": null,
                    "updateDate": null,
                    "dworkPlanDesAm": "sdgsdasdg",
                    "dworkPlanDesPm": "asdgdsgdsaggsdgsdagsdfdsfsdfsadfsad",
                    "dworkReviewDesAm": null,
                    "dplanSubmitDate": null,
                    "dworkReviewDesPm": null,
                    "dplanVerifiedDate": null,
                    "dweekNum": null,
                    "dweekdayNum": "1",
                    "wid": 1,
                    "did": 2,
                    "dyear": null,
                    "dworkPlanDate": "2019-9-26",
                    "dstatus": null,
                    "dverifiedUserName": null,
                    "dreviewVerifiedDate": null,
                    "dreviewSubmitDate": null,
                    "dverifiedAccId": null
                }
            ],
            "wid": 1,
            "wyear": "1",
            "wweekNum": "1",
            "wworkPlanDes": "sdf",
            "wstatus": "1",
            "wplanVerifiedDate": "1970-01-01 00:00:00",
            "wplanSubmitDate": null,
            "wverifiedAccId": null,
            "wverifiedUserName": null,
            "wreviewVerifiedDate": "1970-01-01 00:00:00",
            "wworkReviewDes": null,
            "wreviewSubmitDate": null
        }
    ],
    "enumVal": {},
    "success": true
}

此时workPlanDayList中是有两条数据了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值