总结:
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>
修改完后,重新执行了一下测试用例,测试通过。