mybatis list in與數(shù)組參數(shù)如何轉(zhuǎn)換

小樊
113
2024-07-05 01:06:19

在MyBatis中,可以使用IN關(guān)鍵字來(lái)將數(shù)組參數(shù)轉(zhuǎn)換為列表。例如,如果有一個(gè)ids數(shù)組作為參數(shù),可以使用以下方式將其轉(zhuǎn)換為MyBatis接受的IN列表:

  1. 在mapper.xml中使用foreach標(biāo)簽來(lái)循環(huán)遍歷數(shù)組參數(shù),并將其轉(zhuǎn)換為列表形式:
<select id="selectByIds" parameterType="java.util.List" resultType="YourResultType">
    SELECT * FROM your_table
    WHERE id IN
    <foreach collection="list" item="item" open="(" separator="," close=")">
        #{item}
    </foreach>
</select>
  1. 在Java代碼中將數(shù)組參數(shù)轉(zhuǎn)換為L(zhǎng)ist類型,并調(diào)用MyBatis的方法:
List<Integer> idsList = Arrays.asList(ids);
yourMapper.selectByIds(idsList);

這樣就可以將數(shù)組參數(shù)轉(zhuǎn)換為MyBatis接受的IN列表形式。

0