- 浏览: 198392 次
- 性别:
- 来自: 哈尔滨
文章分类
- 全部博客 (267)
- java.lang (8)
- 问题汇总 (21)
- 异常记录 (20)
- 功能实现 (19)
- 面试总结 (25)
- 技巧总结 (8)
- 常用代码 (4)
- 编程习惯 (3)
- 编码规则 (3)
- java.util (10)
- java.io (1)
- JavaWeb (9)
- MySQL (16)
- SVN (3)
- MyBatis (11)
- Velocity (7)
- 其他知识 (10)
- 人生哲理 (1)
- 人生故事 (1)
- 自我感悟 (1)
- shiro (3)
- 基础知识 (0)
- 问题总结 (1)
- Spring 标签 (1)
- Spring (3)
- 点滴生活 (1)
- DOS (1)
- CAS (4)
- Linux (9)
- Storm (6)
- Shell (1)
- regex (1)
- Collection (4)
- poi (1)
- 经典语句 (1)
- NIO (5)
- concurrent (14)
- RPC (1)
- zookeeper (3)
- 待整理 (2)
- Hadoop (9)
- RabbitMq (2)
- flume (1)
- hive (7)
- hbase (4)
- kafka (1)
- scala (1)
- GC (0)
- java.util.concurrent.atomic (1)
- java.lang.ref (6)
- JVM (2)
- algorithm (1)
- conception (1)
- java key word (1)
- sun.misc (1)
最新评论
问题:MyBatis接口中以list作为参数类型,在mapper.xml中以改list的名称进行判定,报错
总结:
1.
2.使用map传递参数,mapper.xml中接收的值为map中的key值
3.在mapper.xml通过po类进行参数接收
4.关于po与map做为参数的比较
博文转载:http://chenzhou123520.iteye.com/blog/1921284
有些许改动,改动的地方是MyBatis的调用方式不同
错误提示:
Error querying database.Cause: org.apache.ibatis.binding.BindingException: Parameter 'studentNameList' not found. Available parameters are [list]
Cause: org.apache.ibatis.binding.BindingException: Parameter 'studentNameList' not found. Available parameters are [list]
单元测试类
mapper.java
mapper.xml
报错原因:
根据报错日志分析,是MyBatis在解析xml时找不到其中声明的studentNameList,
但是在接口中传的参数就是studentNameList,怎么会报错呢?
查询了一下MyBatis官方的说明文档,终于找到了原因,在http://mybatis.github.io/mybatis-3/zh/dynamic-sql.html#foreach里有一段说明:
写道
注意 你可以传递一个 List 实例或者数组作为参数对象传给 MyBatis。当你这么做的时 候,MyBatis 会自动将它包装在一个 Map 中,用名称在作为键。List 实例将会以“list” 作为键,而数组实例将会以“array”作为键。
因为我传的参数只有一个,而且传入的是一个List集合,所以mybatis会自动封装成Map<"list",studentNameList>。在解析的时候会通过“list”作为Map的key值去寻找。但是我在xml中却声明成studentNameList了,所以自然会报错找不到。
解决方式:
第一种就是修改mapper.xml中foreach标签内容,把studentNameList修改为list
不过这种方式我个人不太建议,因为以后如果要扩展该方法,增加集合参数的时候,还得修改xml中的内容。
第二种方式,修改dao中的参数传入方式,手动封装成map,然后把map当参数传进去
Dao方法修改为:
然后修改mapper.xml中的parameterType类型为Map
修改完后,重新执行了一下测试用例,测试通过。
总结:
1.
类型 | List | Array | Map |
XML中接收值 | list | array | 封装的key |
参数类型 | java.util.List | java.util.Array | java.util.Map |
2.使用map传递参数,mapper.xml中接收的值为map中的key值
3.在mapper.xml通过po类进行参数接收
<select id="viewMerchantBrand" resultMap="BaseResultMap" parameterType="user.po.MerchantBrand" > select <include refid="Base_Column_List" /> from usr_merchant_brand where 1 = 1 <if test="merchantId != null" > and merchant_id = #{merchantId,jdbcType=VARCHAR} </if> <if test="brandId != null" ><!--Po类中的属性字段名称 --> and brand_id = #{brandId,jdbcType=INTEGER} </if> </select>
4.关于po与map做为参数的比较
名称 | po | map |
集合如list | 将该集合作为po的属性 | 集合作为key值 |
类属性 | 类中全部属性 | 类中部分属性 |
博文转载:http://chenzhou123520.iteye.com/blog/1921284
有些许改动,改动的地方是MyBatis的调用方式不同
错误提示:
Error querying database.Cause: org.apache.ibatis.binding.BindingException: Parameter 'studentNameList' not found. Available parameters are [list]
Cause: org.apache.ibatis.binding.BindingException: Parameter 'studentNameList' not found. Available parameters are [list]
单元测试类
@Test public void testgetStudentCount(){ List<String> studentNameList = new ArrayList<String>(); studentNameList.add("zhangsan"); studentNameList.add("lisi"); int count = studentDao.getStudentCount(studentNameList); System.out.println(count); }
mapper.java
pulibc int getStudentCount(List<String> studentNameList);
mapper.xml
<select id="getStudentCount" parameterType="java.util.List" resultType="java.lang.Integer"> <![CDATA[ SELECT COUNT(*) FROM t_student WHERE 1=1 ]]> <if test="studentNameList != null"> AND student_name in <foreach collection="studentNameList" item="item" open="(" separator="," close=")"> #{item} </foreach> </if> </select>
报错原因:
根据报错日志分析,是MyBatis在解析xml时找不到其中声明的studentNameList,
但是在接口中传的参数就是studentNameList,怎么会报错呢?
查询了一下MyBatis官方的说明文档,终于找到了原因,在http://mybatis.github.io/mybatis-3/zh/dynamic-sql.html#foreach里有一段说明:
写道
注意 你可以传递一个 List 实例或者数组作为参数对象传给 MyBatis。当你这么做的时 候,MyBatis 会自动将它包装在一个 Map 中,用名称在作为键。List 实例将会以“list” 作为键,而数组实例将会以“array”作为键。
因为我传的参数只有一个,而且传入的是一个List集合,所以mybatis会自动封装成Map<"list",studentNameList>。在解析的时候会通过“list”作为Map的key值去寻找。但是我在xml中却声明成studentNameList了,所以自然会报错找不到。
解决方式:
第一种就是修改mapper.xml中foreach标签内容,把studentNameList修改为list
<if test="list != null"> AND student_name in <foreach collection="list" item="item" open="(" separator="," close=")"> #{item} </foreach> </if>
不过这种方式我个人不太建议,因为以后如果要扩展该方法,增加集合参数的时候,还得修改xml中的内容。
第二种方式,修改dao中的参数传入方式,手动封装成map,然后把map当参数传进去
Dao方法修改为:
public int getStudentCount(List<String> studentNameList){ //把参数手动封装在Map中 Map<String, Object> map = new HashMap<String, Object>(); map.put("studentNameList", studentNameList); return super.count("getStudentCount", map); }
然后修改mapper.xml中的parameterType类型为Map
<!--注意下面的parameterType类型必须修改为Map类型,foreach中引用的List名称不用改变--> <select id="Student.getStudentCount" parameterType="java.util.Map" resultType="java.lang.Integer"> <![CDATA[ SELECT COUNT(*) FROM t_student WHERE 1=1 ]]> <if test="studentNameList != null"> AND student_name in <foreach collection="studentNameList" item="item" open="(" separator="," close=")"> #{item} </foreach> </if> </select>
修改完后,重新执行了一下测试用例,测试通过。
发表评论
-
Publishing failed with multiple errors file not found
2017-11-10 22:39 816问题场景: Java Web项目 ... -
attempted to return null from a method with a primitive return type (int)
2017-10-25 22:52 3137异常背景 mybatis mapper.xml 中 resul ... -
java.lang.UnsupportedOperationException
2017-10-24 21:42 329异常背景: 遍历集合进行元素删除操作 异常描述: Str ... -
can not find xxx.xxx
2017-10-03 10:20 369问题描述: 单元测试一个已离职同事的接口,运行时提示 po ... -
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '
2017-10-02 18:33 548问题场景: 启动 Tomcat 时报错,org.spring ... -
为什么运行接口无返回值
2017-10-02 18:08 727问题场景: 与同事共同开发一个项目,调用其开发的接口,未接收 ... -
java.lang.OutOfMemoryError: Java heap space
2017-10-02 18:02 389问题描述: 新来的同事,安装JDK、Eclipse 、Tom ... -
No result defined for action
2017-09-26 21:56 545问题场景: 项目设计模式:SpringMVC 调试程序过程中 ... -
attempted to return null from a method with a primitive return type (double)
2017-09-26 21:58 1506问题场景: 服务启动正常,MyBatis Interface ... -
No matching bean of type dao found for depende
2017-09-26 21:46 822问题场景: 项目设计模式:Spring MVC + MyBat ... -
1030 Got error 28 from storage engine
2016-05-24 22:25 1273现象:调试程序过程中,突然报错,显示数据检索失败,数据库连接超 ... -
org.apache.shiro.session.UnknownSessionException
2015-06-27 18:19 3835问题说明: 背景:系 ... -
java Excel poi exception
2015-06-11 16:41 638一、 问题:java导出Exce ... -
The reference to entity "locale" must end with the ';' delimiter.
2015-06-02 15:12 1778问题: 修改web.xml 文件中的拦击器中的URL后 报错T ... -
ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2 JD
2015-05-17 16:39 576问题:运行单元测试类(比较重载与重写的区别)时报错, ERRO ... -
Result Maps collection already contains value for mapper.XXX
2015-05-14 08:53 2085问题:运行单元测试类时报错 原因:应该用resultMap来 ... -
Unable to find resource
2015-05-13 20:32 698场景: 配合JS调试时,出现上面的异常,且数据可以正常接收,程 ... -
target is null for setProperty
2015-05-13 15:00 691问题:ognl.OgnlException: target i ... -
常用mapper方法
2015-05-11 19:49 6401.insert,表中有时间字段,且字段值为当前时间,可以在m ... -
确认方法没有错误,但查询结果不正确
2015-05-07 09:37 813问题:在调试某web工程时,发现一个po.getXXX()没有 ...
相关推荐
解决mouse not found,keyboard not found
Ewebeditor License not found. Disabled.doc
编译时报 Looking for pthread_create - not found 的解决办法 linux gcc 编译时报Looking for pthread_create - not found 其解决办法是...
错误提示如下图: 出现这种情况的原因通常是因为先安装了Framework,后安装的IIS...注册成功后,重启一下iis, 理论上应可以解决导致404.17 not found 的大部分问题了; Win7中IIS出现“HTTP 错误 404.17 – Not Foun
解决 version `GLIBC_2.14' not found 解决方法.具体方法可以参考一下。。
bashfdiskcommand not found 解决办法
5积分,相当于免费直接送,60多种 404 not found页面,带图片预览,下载直接用,html+css,总有一款适合你.下载zip解压,直接用
ADC083X.DLL(报错:External model DLL "ADC083X.DLL" not found. GLE=0x000036B1.) 注意!注意! 注意:替换后再次仿真时仍然有可能失败,这和电脑系统有关(毕竟用的是Po_Jie版本的),可以尝试换台电脑试一试! ...
linux下提示bash-command not found.docx
Framework not found IOSurface for architecture arm64所需 IOSurface.framework、FileProvider.framework
Allegro中导入orCAD原理图网络表时,出现以下错误: “Symbol 'BP' for device 'BEEP_BP_BEEP' not found in PSMPATH or must be "dbdoctor"ed”
在Linux中编译时提示找不到mkimage,编译内核提示mkimage command not found – U-Boot images will not be built
Fatal error: Class ‘COM’ not found in XXXXXXXXX 没发现 COM 类,php代码主要是调用一些系统的信息,初想应该是php没配置好,在stackoverflow找到答案,是讨论xampp的,要延伸开来,我加了几个要点: ★ php 根...
在ctax中提示 file 'psfig.sty' not found,我们可以将psfig.sty文件放在所运行的tex文件的文件夹下。
手机调试app时,使用adb调试数据库sqlite,输入命令sqlite3,提示:sqlite3 not found。将此文件导入/system/XBIN目录下,并赋予执行权限。
404-PageNotFound, "404 Page Not Found" 页面的精选列表 找不到 404页 那些无聊 404页的灵感库。访问画廊墙,并选择你喜欢的 !问题大多数情况下,"找不到 404"页被积压所忽略。 但我们知道 ! 事实上,你会面临...
解决Fatal error Class 'Think' not found in runtime.php
File Not found 'DesignIntf.dcu'在D7下的解决方法 在LIABRAY中加入TOOLAPI的安装路径
bash: vi: command not found, 场景: 因为 docker里面没有这个命令,我们安装一下 apt-get update apt-get install vim 如果 这个时候 安装也还是会出现 异常 root@446d7cf1606c:/etc/mysql# apt-get update ...
您是否在网上找了好多资料都无法解决这个问题?launch failed binary not found,那小弟不才,愿以独家秘诀,(图文)手把手教你解决这个问题,绝不坑爹,欢迎下载!