MyBatis中遇到的一些问题

来源:互联网 发布:forespider爬虫软件 编辑:程序博客网 时间:2024/05/19 19:56

此篇文章记录在使用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">SELECTproduct_no,product_name,brand_no,brand_name,FROMpsc_product_dimWHEREproduct_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
原创粉丝点击