mybatis:parameter ' IDs ' not found. Available parameters are [TemplateID, param1, param2, ValueList]
A very interesting question was found tonight:
Mybatis:parameter ' IDs ' not found. Available parameters are [TemplateID, param1, param2, ValueList]
Could not find this IDs this set variable,
<!--public list<employee> Getempsbyconditionforeach (list<integer> emp);-->
<select Id= "Getempsbyconditionforeach" resulttype= "Com.cn.zhu.bean.Employee"
>
select * from tbl_employee where ID in
<!--
Collection specifies that the parameters of the collection list type to be traversed are
specially handled in the map, and the map key is called the list
item to assign the currently traversed element to the specified variable
#{variable name} You can take out the value of a variable, which is the current traversal
separator The delimiter between each element
open traversal out all results stitching a start character
close: traverse out all result stitching an ending character Index
: Indexes are indexed when traversing the list, the item is the current value
traversing the map when index represents the map's key item is the map's value
-- >
<foreach collection= "IDs" item= "item_id" separator= "," open= "(" close= ")"
>
#{item_id} "
</foreach>
</select>
Console error
### Error querying database. Cause:org.apache.ibatis.binding.BindingException:Parameter ' IDs ' not found. Available parameters are [List, collection]
I can't find the value of IDs, so the problem is.
In fact
collection specifies the collection to traverse
The parameters of the list type are specially handled in the map, and the map key is called list
change to::
<!--public list<employee> Getempsbyconditionforeach (list<integer> emp);-->
<select Id= "Getempsbyconditionforeach" resulttype= "Com.cn.zhu.bean.Employee"
>
select * from tbl_employee where ID in
<!--
Collection specifies that the parameters of the collection list type to be traversed are
specially handled in the map, and the map key is called the list
item to assign the currently traversed element to the specified variable
#{variable name} You can take out the value of a variable, which is the current traversal
separator The delimiter between each element
open traversal out all results stitching a start character
close: traverse out all result stitching an ending character Index
: Indexes are indexed when traversing the list, the item is the current value
traversing the map when index represents the map's key item is the map's value
-- >
<foreach collection= "list" item= "item_id" separator= "," open= "(" close= ")"
>
#{item_id} "
</foreach>
</select>
to solve the collection= "list" Problem
Test class
@Test public
void Testdynamicsql () throws ioexception{
sqlsessionfactory sqlsessionfactory= Getsqlsessionfactory ();
Sqlsession opensession=sqlsessionfactory.opensession ();
try {
employeemapperdynamicsql mapper= opensession.getmapper (employeemapperdynamicsql.class);
list<employee> list11 = Mapper.getempsbyconditionforeach (Arrays.aslist (1,2,3,4));
for (Employee emp:list11) {
System.out.println (EMP);
}
catch (Exception e) {
//Todo:handle EXCEP tion
opensession.close ();
E.printstacktrace ();
}
So, why do you change that way?
collection specifies that the parameters of the collection list type to be traversed
will be specially handled in the map, the map key is called list
The fundamental reason is that they simply do not understand the collection in foreach should put something, the wrong understanding of the java.util.List, in fact, this type should be the same as our @param binding parameter name
<!--public void Addemps (@Param ("Emps") list<employee> emps); -->
if preceded by an alias, then collection will use alias Emps,,,, otherwise the default list