Mybatis查询之list作为参数查询

dao:

 Float findTotMaterial(Map<String,Object> map);

service:

//查询出list昨晚参数

List<String> youndIdLists = youComInfoMapper.findCycCount2(
                    producecode, farmIdList, dayAge);

Map<String, Object> paramMap2 = new HashMap<String, Object>();

paramMap2.put("list1", youndIdLists);


Float totMaterial = youComInfoMapper.findTotMaterial(paramMap2);


映射xm代码:

<select id="findTotMaterial" resultType="java.lang.Float" parameterType="java.util.Map">
    select round(s.totalMaterialNum*1000/(s.youngCount -s.deathCount),2) totMaterial from
        (select COALESCE(sum(ins.fMatNum),0) totalMaterialNum,
        COALESCE(sum(ys.iCount+ys.iGivCount),0)youngCount,
        COALESCE(sum(ins.iDeaCount),0)deathCount
        from inspection ins
        left JOIN young ys on ins.iYoungId=ys.ID
         where ys.ID in
         <foreach collection="list1" index="index" item="youngId" open="(" separator="," close=")">
            #{youngId}
        </foreach>
        )s
    </select>




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 语句。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值