在MyBatis中collection属性的命名规则主要取决于传入参数的类型

在MyBatis中collection属性的命名规则主要取决于传入参数的类型

命名规则

  1. 单参数为List/Set/数组时

    • 默认使用"list"作为collection的值,例如传入List<String>类型参数时,collection="list"。 ‌
    • 若传入参数为数组(如String[]),则使用"array"。 ‌
    • 传入Map<String, List<String>>时,collection="key",其中"key"是Map的键名。 ‌
    • 传入实体类且包含List属性时,collection="实体属性名",如实体类属性List<String> ids则collection="ids"。 ‌
  2. 多参数或复杂参数时

    • 需要通过@Param注解自定义collection名称。
  • 简单总结:

参数为集合 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注解作以区分

posted @ 2025-10-05 14:08  休玛  阅读(5)  评论(0)    收藏  举报