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
- MyBatis中遇到的一些问题
- MyBatis中遇到的一些问题
- mybatis 项目中遇到的一些问题..注意事项
- 记MyBatis和MySQL中遇到的一些小问题
- mybatis中遇到的问题--mybatis.xml
- Spring与Mybatis的整合过程中遇到的一些问题
- 在mybatis中遇到奇怪的问题
- mybatis开发过程中遇到的问题
- mybatis sqlmap中property遇到的问题
- 使用MyBatis过程中遇到的问题
- 关于mybatis中遇到的<foreach> 问题
- MyBatis 实践中遇到的问题
- Mybatis向数据库增加数据时遇到的一些问题
- Mybatis+SpringMVC+注解事务遇到的一些问题及解决
- mybatis使用过程遇到的一些问题及解决方法
- 使用springmvc mybatis moven dubbo 遇到的一些问题
- mybatis遇到的问题
- mybatis遇到的问题
- left join加上where条件的困惑,(left join 无用 / 无效 )
- selenium-python
- Jersey
- 怎么在ZBrush中插入多网格
- Tomcat添加源代码包失败 source not found
- MyBatis中遇到的一些问题
- PDF怎么支持中文 KendoUI 导出PDF
- springMvc拦截器的使用
- 从0开始学习 GitHub 系列之「05.Git 进阶」
- Android的IPC方式
- BIT2010上机第一题
- __attribute__((packed))的作用
- 设计模式
- 从其他地方导入Eclipse工程的程序出现红色叹号的解决方式