Mybatis将传入list参数看做表,与数据表做关联查询

如何将传入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>

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis 中,如果你想要使用一个 List 参数作为临时来进行关联查询,你可以按照以下步骤进行操作: 1. 首先,在你的 Mapper 接口中定义一个方法,该方法接收一个 List 参数,用于传递临时的数据。 ```java public interface YourMapper { List<YourResultType> yourMethod(List<YourDataType> dataList); } ``` 2. 在你的 Mapper XML 文件中,编写 SQL 语句来进行关联查询。在这个 SQL 语句中,你可以使用临时的数据来进行查询操作。可以使用 `foreach` 标签来遍历传入List 参数,并将数据插到临时中。 ```xml <select id="yourMethod" resultType="YourResultType"> CREATE TEMPORARY TABLE temp_table (id INT); <foreach collection="dataList" item="item" separator=";"> INSERT INTO temp_table (id) VALUES (#{item.id}); </foreach> -- 在这里编写你的关联查询语句,使用 temp_table 来进行关联查询 SELECT ... FROM ... WHERE ... </select> ``` 在这个例子中,我们使用 `temp_table` 作为临时名,并将 List 中的数据插到该中。然后,在查询语句中,可以使用该临时来进行关联查询。 3. 最后,你可以在你的代码中调用该方法,并传入 List 参数进行查询操作。 ```java List<YourDataType> dataList = new ArrayList<>(); // 添加数据到 dataList List<YourResultType> result = yourMapper.yourMethod(dataList); ``` 这样,你就可以使用 MyBatis 进行关联查询,并且使用 List 参数作为临时的数据了。记得根据你的实际需求修改代码和 SQL 语句。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值