Mybatis 传输List的实现代码
程序员文章站
2024-02-26 11:40:04
1. 当查询的参数只有一个时
findbyids(list ids)
1.1 如果参数的类型是list, 则在使用时,c...
1. 当查询的参数只有一个时
findbyids(list<long> ids)
1.1 如果参数的类型是list, 则在使用时,collection属性要必须指定为 list
xml代码
<select id="findbyidsmap" resultmap="baseresultmap"> select <include refid="base_column_list" /> from jria where id in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </select> <select id="findbyidsmap" resultmap="baseresultmap"> select <include refid="base_column_list" /> from jria where id in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </select> findbyids(long[] ids)
1.2 如果参数的类型是array,则在使用时,collection属性要必须指定为 array
xml代码
<select id="findbyidsmap" resultmap="baseresultmap"> select <include refid="base_column_list" /> from tabs where id in <foreach item="item" index="index" collection="array" open="(" separator="," close=")"> #{item} </foreach> </select> <select id="findbyidsmap" resultmap="baseresultmap"> select <include refid="base_column_list" /> from tabs where id in <foreach item="item" index="index" collection="array" open="(" separator="," close=")"> #{item} </foreach> </select>
2. 当查询的参数有多个时,例如 findbyids(string name, long[] ids)
这种情况需要特别注意,在传参数时,一定要改用map方式, 这样在collection属性可以指定名称
下面是一个示例
map<string, object> params = new hashmap<string, object>(2); params.put("name", name); params.put("ids", ids); mapper.findbyidsmap(params);
xml代码
<select id="findbyidsmap" resultmap="baseresultmap"> select <include refid="base_column_list" /> from tabs where id in <foreach item="item" index="index" collection="ids" open="(" separator="," close=")"> #{item} </foreach> </select>
总结
以上所述是小编给大家介绍的mybtis 传输list的实现代码,希望对大家有所帮助