MyBatis中遇到的一些问题

此篇文章记录在使用MyBatis的时候遇到的一些问题,不持续更新~


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

原因:这个问题的情况是,根据表中的一些字段筛选数据的时候,比如resultType='java.lang.String'  但是表中根据这些筛选条件得到的数据不唯一,而且在mapper中的返回类型是String  就会出现上边的这种问题

办法:在mapper中将对应方法的返回值改为List<String>  然后再service中将重复数据(list中的数据都是一样的).get(0)就好(这里需要注意list中有数据,否则会有边界异常)


2:Mybatis中对MYSQL的批量操作

说明:批量操作的好处就是减少与数据库的交互次数,在大数据量下降低系统压力,提升业务效率,不多说直接上代码

2.1:批量添加

  mapper文件:参数为list

     Integer insertIntoCdcDelivery(List<CdcDeliveryEntity> records);
 xml文件:使用foreach标签实现批量操作
<insert id="insertIntoCdcDelivery">
		INSERT INTO im_cdc_delivery (
			row_id,
			bill_date,
			bill_no,
			bill_type,
			corp_no,
			warehouse_no,
			customer_no,
			parent_no,
			parent_type,
			relative_no,
			relative_type,
			box_num,
			sku_num,
			bill_amount,
			bill_status,
			remark
		)
		VALUES
			<foreach collection = "list" item = "item" index="index" separator=","> 
			<![CDATA[
			(
				uuid_short(), #{item.billDate,jdbcType=DATE}, #{item.billNo,jdbcType=VARCHAR}, 
				#{item.billType,jdbcType=VARCHAR}, #{item.corpNo,jdbcType=VARCHAR}, #{item.warehouseNo,jdbcType=VARCHAR}, 
				#{item.customerNo,jdbcType=VARCHAR}, #{item.parentNo,jdbcType=VARCHAR}, #{item.parentType,jdbcType=VARCHAR}, 
				#{item.relativeNo,jdbcType=VARCHAR}, #{item.relativeType,jdbcType=VARCHAR}, #{item.boxNum,jdbcType=INTEGER}, 
				#{item.skuNum,jdbcType=INTEGER}, #{item.billAmount,jdbcType=DECIMAL}, #{item.billStatus,jdbcType=TINYINT}, 
				#{item.remark,jdbcType=VARCHAR}
			)
			]]>
			</foreach>
</insert>

2.2:批量查询

  mapper文件:
List<PscProductDimEntity> SelectProductInfoByNo(@Param("list")List<String> list);

 xml文件:resultMap就不贴出来了,还有就是mybatis的自动生成实体+mapper+xml可以参考我的这篇文章MybatisGenerator生成代码

<select id="SelectProductInfoByNo" resultMap="SelectProductInfoByNoMap">
		SELECT
			product_no,
			product_name,
			brand_no,
			brand_name,
		FROM
			psc_product_dim
		WHERE
			product_no in 
		<foreach item="item" index="index" collection="list"  open="(" separator="," close=")" > 
                #{item} 
        </foreach> 
</select>


3:org.apache.ibatis.binding.BindingException: Parameter 'planned_time_status' not found

如果出现了这种问题,你确定参数拿到了,那就最后检查下mapper中是不是没有使用@Param注解


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值