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

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

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>
	

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值