如何将传入list参数看做表
遇到一个业务,需要将一个list传入与原有的表进行关联,实现方式如下
通过以下语句
select 1 column1,2 column2 ,3 column3 from dual
union
select 4 column1,5 column2 ,6 column3 from dual
union
select 7 column1,8 column2 ,9 column3 from dual
得到的结果如下:
同理我们可以用mybatis中的foreach标签实现将list循环为一条条记录,用union连接起来即可
<select id="list" resultType="com.xxx.xxx.xxx.xxx.xxxx.xxxx">
SELECT table1.uid ,tabel3.list_uid ,tabel3.list_name
from table1
left join
(
select table2.list_uid , table2.list_name ,table2.list_sex from
(
<foreach collection="list" item="obj" open=" ( " close=" ) " separator="union">
select #{obj.uid} list_uid,#{obj.name} list_name,#{obj.sex} list_sex from dual
</foreach>
) table2
) table3
on table1.uid = table3.list_uid
</select>