在MyBatis中collection属性的命名规则主要取决于传入参数的类型
命名规则
-
单参数为List/Set/数组时
- 默认使用
"list"
作为collection的值,例如传入List<String>
类型参数时,collection="list"。 - 若传入参数为数组(如
String[]
),则使用"array"
。
- 默认使用
-
多参数或复杂参数时
- 需要通过
@Param
注解自定义collection名称。例如:- 传入
Map<String, List<String>>
时,collection="key",其中"key"是Map的键名。 - 传入实体类且包含List属性时,collection="实体属性名",如实体类属性
List<String> ids
则collection="ids"。
- 传入
- 需要通过
- 简单总结:
参数为集合 List <T> t 时 :collection = "list" ,这里就默认为 list ;
参数为数组 Array[ ] a 时 :collection = "array" , 这里就默认写为 array ;
参数为 Map<key, value> map 时 :collection = "key" , 这个key即为自己封装map的key值 ;
参数为实体类对象,且含有属性List<T> t 时 :collection = “t” 这里的取值即为List的属性名;
多参数时必须要利用@Param注解作以区分