版权声明:转载请注明原创 https://blog.csdn.net/qq_42151769/article/details/90600679
如我的具体错误如下:
org.apache.ibatis.binding.BindingException: Parameter 'roleUidList' not found. Available parameters are [collection, list]
at org.apache.ibatis.session.defaults.DefaultSqlSession$StrictMap.get(DefaultSqlSession.java:343) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.scripting.xmltags.DynamicContext$ContextAccessor.getProperty(DynamicContext.java:115) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.ognl.OgnlRuntime.getProperty(OgnlRuntime.java:2671) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.ognl.ASTProperty.getValueBody(ASTProperty.java:114) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258) ~[mybatis-3.4.5.jar:3.4.5]
看下我的方法:
//查询t_role,获取到category_menu_uids
if(CollectionUtil.isNotEmpty(roleUidList)){
List<TRole> roleList = roleMapper.queryByRoleUidList(roleUidList);
//["49b42250abcb47ff876bad699cf34f03","1f01cd1d2f474743b241d74008b12333","0a035547bbec404eb3ee0ef43312148d","78ab104b123f4950af14d65798afb756","6606b7e646d545e5a25c70b5e5fade9f","bcf4a9bc21c14b559bcb015fb7912266","9beb7caa2c844b36a02789262dc76fbe","6228ff4e9ebd42c89599b322201a0345","65e22f3d36d94bcea47478aba02895a1","4dea9c4f39d2480983e8c4333d35e036"]
roleList.stream().filter((TRole var) -> null != var && StringUtils.isNotEmpty(var.getCategoryMenuUids())).forEach(var1 -> {
//解析上面的格式
List<String> list = CommonUtil.parseMenuListString(var1.getCategoryMenuUids());
list.stream().filter((String var) -> StringUtils.isNotEmpty(var)).forEach(var2 -> {
menuUidList.add(var2);
});
});
System.out.println("查询到是什么:" + menuUidList);
}
}
mapper.xml的sql如下:
<!--按照主键list查询-->
<select id="queryByRoleUidList" resultType="com.wm.mogu_entity.blog.TRole" parameterType="java.util.Map">
select <include refid="Base_Column_List"/>,category_menu_uids from t_role
<where>
uid in <foreach collection="roleUidList" item="uid" open="(" separator="," close=")">
#{uid}
</foreach>
</where>
</select>
可以看到我明明传入的是roleUidList,但是在执行时还是报错 Parameter 'roleUidList' not found. Available parameters are [collection, list]
原因是:
注意 你可以传递一个 List 实例或者数组作为参数对象传给 MyBatis。当你这么做的时 候,MyBatis 会自动将它包装在一个 Map 中,用名称在作为键。List 实例将会以“list” 作为键,而数组实例将会以“array”作为键。
也就是说传入的为list,会默认组成map,key为list,value就是对应的值 , 同理,传入的为array时,会默认key为array,
那么我们怎么解决呢?
方法一,将sql中的collection的值改为list(因为你传入的为list,如果传入的为array就写array),也就是如下:
<!--按照主键list查询-->
<select id="queryByRoleUidList" resultType="com.wm.mogu_entity.blog.TRole" parameterType="java.util.Map">
select <include refid="Base_Column_List"/>,category_menu_uids from t_role
<where>
uid in <foreach collection="list" item="uid" open="(" separator="," close=")">
#{uid}
</foreach>
</where>
</select>
但是这种方法并不推荐
方法二:
在定义的方法上打上注解
@Param("roleUidList")
此时注意注解的值需要和sql终的collection的值一样,也就是告诉mybatis用我注解的这个value来作为key吧,还有这个注解引入的包是mybatis的
import org.apache.ibatis.annotations.Param;
如下:
package com.wm.mogu_dao.blog;
import com.wm.mogu_base.mapper.common.BaseMapper;
import com.wm.mogu_entity.blog.TRole;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface TRoleMapper extends BaseMapper<TRole> {
/**
* 通过uuid批量查询
* @param roleUidList
* @return
*/
List<TRole> queryByRoleUidList(@Param("roleUidList") List<String> roleUidList);
}
此时问题解决了,试试吧!