MyBatis (when mapping files using foreach tag error, the issue of property collection)

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.apache.ibatis.binding.BindingException: Parameter 'ids' not found. Available parameters are [collection, list]
### Cause: org.apache.ibatis.binding.BindingException: Parameter 'ids' not found. Available parameters are [collection, list]

When learning mybatis using foreach loop through the collection, like the above error

Interface code:

    public List<Employee> getEmpsbyCondtionForeach(List<Integer> ids);

xml Code:

    <! - more id id in query information === (, 2, 3) -> 
    < the SELECT id = "getEmpsbyCondtionForeach" resultType = "emp" > 
        the SELECT * from tbl_employee in the WHERE id 
        <-! Collection: specifies a set of traverse: 
                List type specific treatment parameters encapsulated in the map, map the key is List 
             Item: assign an element of the current iteration specified variables 
             separator: separator between each element 
             open: traversing a All results stitching a character begins 
             close: traversing all the result of splicing the end of a character 
             index: index. The list is traversed when the index is an index, item is the current value 
                         is the key map indicates the traversing map index, item values that map 

             # {} variable name can be extracted variable value, which is the current traversing the element 
         -> 
        < foreach Collection="id" item="item_id" separator=","
                open="(" close=")">
            #{item_id}
        </foreach>
    </select>

solution:

Modify mapper.java:

    public List<Employee> getEmpsbyCondtionForeach(@Param("ids") List<Integer> ids);

Or modifications mapper.xml:

    <! - more id id in query information === (, 2, 3) -> 
    < the SELECT id = "getEmpsbyCondtionForeach" resultType = "emp" > 
        the SELECT * from tbl_employee in the WHERE id 
        <-! Collection: specifies a set of traverse: 
                List type specific treatment parameters encapsulated in the map, map the key is List 
             Item: assign an element of the current iteration specified variables 
             separator: separator between each element 
             open: traversing a All results stitching a character begins 
             close: traversing all the result of splicing the end of a character 
             index: index. The list is traversed when the index is an index, item is the current value 
                         is the key map indicates the traversing map index, item values that map 

             # {} variable name can be extracted variable value, which is the current traversing the element 
         -> 
        < foreach Collection="list" item="item_id" separator=","
                open="(" close=")">
            #{item_id}
        </foreach>
    </select>

the reason:

Properties collection foreach tag, under different circumstances, the value of this attribute is not the same, there are the following three cases:

1, if the incoming is a single parameter and a parameter type is List time, Collection attribute value list.

2, if the incoming single parameter and time parameter type is an array of the array, the property value array Collection.

3, if the incoming parameter is more, we need to package them into a Map, of course, can also be packaged as a single parameter Map, in fact, if you're in the incoming parameters, which also put it in MyBatis packaged into a Map, the map is the key parameter names, so this time the collection property value is passed in a List or array key target in its own package of map inside.

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/xjs1874704478/p/11917351.html