Mybatis異常 (Statement returned more than one row, where no more than one was expected.)

本文详细解析了在使用MyBatis框架进行多表连接查询时遇到的ExecutorException异常,阐述了一对一映射关系配置不当导致的问题,并对比了两种不同的配置方法,推荐采用实体嵌套配置以避免查询结果超出预期的错误。

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

一個很普通的多表連接查詢不能正常顯示結果,一直報

Exception in thread "main" org.apache.ibatis.executor.ExecutorException: Statement returned more than one row, where no more than one was expected.

經過不斷的排查,找出問題原因:

       Mybatis框架在配置*Mapper.xml時要採用正確的方式,在配置一對一映射關係時,最好採用實體嵌套配置的方式,如果在配置Java類類型時,採用寫sql查詢的方式映射,會造成上述異常

1)不穩定配置方法:

<sql id="test1">

//多表連接查詢此處為偽代碼

select  a.*  from cc a left join b  on 。。。。。。。。。。。。。。

</sql>


	<resultMap type="java實體類名" id="test2">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="sex" column="name"/>
		<association property="address" resultMap="Test4"/>
	</resultMap>
	<resultMap type="java實體類名" id="test5">
		<id property="id" column="id"/>
		<result property="AddressName" column="name"/>
		<result property="AddressCode" column="name"/>
		
	</resultMap>

<select id="test3" parameterType="map" resultMap="test2">
		<include refid="test1"/>
		
		<where>
			
            
		</where>
	</select>

//最好不要用這種配置方法,用了sql語句,查出的數據可能有多條,容易出異常
	<sql id="Test88">
		SELECT c.*
		FROM  dd c  
		INNER JOIN ...................
		WHERE ....................
	</sql>

	<select id="test4" resultMap="test5">
		<include refid="Test88" />
	</select>


	

2)穩定的配置方法

	
<resultMap type="java實體類名" id="test2">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="sex" column="name"/>
		<association property="address" resultMap="Test4"/>
	</resultMap>

<select id="test3" parameterType="map" resultMap="test2">
		<include refid="test1"/>
		
		<where>
			
            
		</where>
	</select>

//用這種方法
    <resultMap type="Address" id="Test4">
         <id property="id" column="id" />
		 <result property="addressCode" column="addressCode"/>
		
    </resultMap>
	

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值