mybatis 查询 resultMap="" 只返回一条数据

当遇到Mybatis查询resultMap配置正确但返回数据异常,仅返回一条的情况,通常问题可能出在SQL字段与resultMap定义的字段映射不一致。检查并确保两者匹配,可以解决此类问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在已确认sql没问题的情况下

检查查询的字段  是否与resultMap 标签中的参数相匹配

如下图修改前:

<resultMap id="BaseResultMap" type="com.koow.kkwwo.pojo.To_pay" >
    <id column="to_pay_id" property="toPayId" jdbcType="INTEGER" />
    <result column="to_pay_orderId" property="toPayOrderid" jdbcType="VARCHAR" />
    <result column="to_pay_money" property="toPayMoney" jdbcType="DECIMAL" />
    <result column="to_pay_on_date" property="toPayOnDate" jdbcType="TIMESTAMP" />
    <result column="to_pay_end_date" property="toPayEndDate" jdbcType="TIMESTAMP" />
    <result column="to_pay_state" property="toPayState" jdbcType="INTEGER" />
    <result column="to_pay_fee" property="toPayFee" jdbcType="INTEGER" />
    <result column="user_id" property="userId" jdbcType="INTEGER" />
    <result column="to_pay_asynchronous_address_df" property="toPayAsynchronousAddressDf" jdbcType="VARCHAR" />
    <result column="to_pay_money_state" property="toPayMoneyState" jdbcType="INTEGER" />
    <result column="to_pay_money_state2" property="toPayMoneyState2" jdbcType="INTEGER" />
    <result column="to_pay_money_state3" property="toPayMoneyState3" jdbcType="INTEGER" />
    <result column="to_pay_sy_message1" property="toPaySyMessage1" jdbcType="VARCHAR" />
    <result column="to_pay_sy_message2" property="toPaySyMessage2" jdbcType="VARCHAR" />
    <result column="to_pay_sy_message3" property="toPaySyMessage3" jdbcType="VARCHAR" />
    <result column="to_pay_sy_message4" property="toPaySyMessage4" jdbcType="VARCHAR" />
    <result column="to_pay_accNo" property="toPayAccno" jdbcType="VARCHAR" />
    <result column="to_pay_accName" property="toPayAccname" jdbcType="VARCHAR" />
    <result column="to_pay_banktype" property="toPayBanktype" jdbcType="VARCHAR" />
    <result column="to_pay_bank_zh_name" property="toPayBankZhName" jdbcType="VARCHAR" />
    <result column="passageway_id" property="passagewayId" jdbcType="INTEGER" />
    <collection property="passageways" javaType="java.util.List" ofType="com.koow.kkwwo.pojo.Passageway">  
            <id column="passageway_id" property="passagewayId" jdbcType="INTEGER" />  
            <result column="passageway_des" property="passagewayDes" jdbcType="VARCHAR" />  
            <result column="passageway_chid" property="passagewayChid" jdbcType="INTEGER" />  
    </collection> 
  </resultMap>

<select id="selectBy_to_pay_state_date" resultMap="BaseResultMap" parameterType="com.koow.kkwwo.pojo.To_pay" >
    select  tp.to_pay_orderId,tp.to_pay_sy_message1,tp.passageway_id,tp.to_pay_money,tp.to_pay_fee,tp.user_id,pa.*
    from to_pay tp left join passageway pa on  tp.passageway_id=pa.passageway_id
    where to_pay_state = #{toPayState,jdbcType=INTEGER} and to_pay_on_date > #{toPayOnDate,jdbcType=VARCHAR} and to_pay_sy_message4  is not null and to_pay_sy_message4!=''
  </select>




修改后:

<resultMap id="ToPayStateDate" type="com.koow.kkwwo.pojo.To_pay" >
    <result column="to_pay_orderId" property="toPayOrderid" jdbcType="VARCHAR" />
    <result column="to_pay_money" property="toPayMoney" jdbcType="DECIMAL" />
    <result column="to_pay_fee" property="toPayFee" jdbcType="INTEGER" />
    <result column="user_id" property="userId" jdbcType="INTEGER" />
    <result column="to_pay_sy_message1" property="toPaySyMessage1" jdbcType="VARCHAR" />
    <result column="passageway_id" property="passagewayId" jdbcType="INTEGER" />
    <collection property="passageways" javaType="java.util.List" ofType="com.koow.kkwwo.pojo.Passageway">  
            <id column="passageway_id" property="passagewayId" jdbcType="INTEGER" />  
            <result column="passageway_des" property="passagewayDes" jdbcType="VARCHAR" />  
            <result column="passageway_chid" property="passagewayChid" jdbcType="INTEGER" />  
    </collection> 
  </resultMap>
<select id="selectBy_to_pay_state_date" resultMap="ToPayStateDate" parameterType="com.koow.kkwwo.pojo.To_pay" >
    select  tp.to_pay_orderId,tp.to_pay_sy_message1,tp.passageway_id,tp.to_pay_money,tp.to_pay_fee,tp.user_id,pa.*
    from to_pay tp left join passageway pa on  tp.passageway_id=pa.passageway_id
    where to_pay_state = #{toPayState,jdbcType=INTEGER} and to_pay_on_date > #{toPayOnDate,jdbcType=VARCHAR} and to_pay_sy_message4  is not null and to_pay_sy_message4!=''
  </select>


### MyBatis resultMap 返回多条数据配置及问题解决方案 当遇到MyBatis的`resultMap`仅返回单条记录的情况时,通常是因为映射文件中的某些设置不正确或缺少必要的字段映射。为了确保能够获取到全部符合条件的数据,在编写SQL查询以及对应的XML映射文件时需要注意几个方面。 #### SQL语句优化 确保编写的SQL语句确实能检索出预期数量的结果集。例如: ```sql SELECT * FROM students WHERE class_id = #{classId} ``` 这条简单的SQL应该能够根据给定条件匹配多个学生对象[^2]。 #### XML映射文件调整 对于一对多关系(如班级对学生),应在相应的Mapper XML中定义合适的`<collection>`标签来表示子项列表,并指定其属性值以完成复杂类型的嵌套加载。具体来说就是修改`ClassInfoDao.xml`如下所示: ```xml <!-- 定义resultMap --> <resultMap type="com.example.ClassInfo" id="BaseResultMap"> <!-- 主键字段 --> <id property="id" column="id"/> <!-- 基本信息字段 --> <result property="name" column="className"/> <!-- 关联的学生集合 --> <collection property="students" ofType="com.example.Student" javaType="ArrayList" column="{classId=id}" select="getStudentsByClassId"/> </resultMap> <!-- 子查询用于填充集合 --> <select id="getStudentsByClassId" parameterType="map" resultType="com.example.Student"> SELECT * FROM student s WHERE s.class_id = #{classId}; </select> ``` 上述代码片段展示了如何利用`<collection>`元素实现从父级实体向子级实体传递参数并执行额外查询的过程[^3]。 另外,如果仅仅是简单的一对一或多对一场景,则只需保证`resultMap`内的所有列都被正确定位即可解决问题。比如在`StudentDao.xml`里添加任意一个非主键作为辅助定位依据就能恢复正常行为: ```xml <resultMap type="com.example.Student" id="studentResultMap"> <id property="id" column="id"/> <result property="name" column="name"/> ... <result property="someColumn" column="some_column"/> <!-- 添加此行 --> </resultMap> ``` 这样做之后,即使存在重复的关键字组合也不会影响最终结果的数量显示了[^4]。 最后提醒一点,务必确认Java端POJO类的设计与数据库表结构相吻合,特别是外键约束和关联关系的部分要特别留意。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值