美文网首页
mybatis 解析传入数组或list 实例

mybatis 解析传入数组或list 实例

作者: clannadyang | 来源:发表于2022-11-24 12:15 被阅读0次

1. mybatis 传入Array

<!-- int delByIds(@Param("ids") String[] idArr);  -->
<delete id="delByIds" >
        delete from sys_config where id in
        <foreach item="iditem" collection="ids" open="(" separator="," close=")">
            #{iditem}
        </foreach>
</delete>

<!--  *****************************************************************   -->
<!--  public int delByIds(String[] idArr);  -->
<delete id="delByIds" >
        delete from sys_config where id in
        <foreach item="iditem" collection="array" open="(" separator="," close=")">
            #{iditem}
        </foreach>
</delete>

2. mybatis 传入list

<!-- int delByIds(@Param("ids") List<String> idArr);  -->
<delete id="delByIds">
        delete from sys_config where id in
        <foreach item="iditem" collection="ids" open="(" separator="," close=")">
            #{iditem}
        </foreach>
</delete>

<!--  *****************************************************************   -->
<!-- int delByIds(List<String> idArr);  -->
<delete id="delByIds">
        delete from sys_config where id in
        <foreach item="iditem" collection="list" open="(" separator="," close=")">
            #{iditem}
        </foreach>
</delete>

2. mabatis 传入list<JSON>或list<Map>,数组同理

传入数据:[{id:"1",state:"1"},{id:"2",state:"1"},{id:"3",state:"2"}]
字段判空:<if test="item !=null and item.id !=null and item.id !='' "></if>
注意:这里的foreach 分割符为 or,open和close为 ( )
<foreach item="item" collection="confObjList" open="(" separator="or" close=")">

<!-- int delByIds(@Param("confObjList") List<JSONObject> confObjList);  -->
<!-- int delByIds(@Param("confObjList") List<Map<String,Object>> confObjList);  -->
<delete id="delByIds">
        delete from sys_config where  
        <foreach item="item" collection="confObjList" open="(" separator="or" close=")">
                      id = #{item.id} and status = #{item.state}
        </foreach>
</delete>
<!--        ↓↓↓ 最终的sql ↓↓↓ 
delete from sys_config where (id='1' and status='1')
    or (id='2' and status='1')
    or (id='3' and status='2')
-->

4. mybatis 传入二维数组或双层list 使用下标获取数组数据

list<String[]>或list<ArrayList<String>>或list<ArrayList<JSONObject>>,数组同理
传入数据:[ ["1","aaa"], ["2","bbb"], ["3"] ]
注意 这里用 ${} 替换 #{} ,用$需要自己拼接单引号,这儿有可能存在sql注入问题
===> 数组用.length,List用.size()

<!-- int delByIds(@Param("confObjList") List<String[]> confObjList);  -->
<delete id="delByIds">
        delete from sys_config where  
        <foreach collection="confObjList" item="items" open="(" separator="or" close=")">
              <if test="items!=null and items.length == 1">
                        id='${items[0]}'
              </if>
            <if test="items !=null and items.length == 2">
               ( id='${items[0]}' and name='${items[1]}')
            </if>
        </foreach>
</delete>

<!-- int delByIds(@Param("confObjList") List<Map<String,Object>> confObjList);  -->
<delete id="delByIds">
        delete from sys_config where  
        <foreach collection="confObjList" item="items" open="(" separator="or" close=")">
              <if test="items!=null and items.size() == 1">
                        id='${items[0]}'
              </if>
            <if test="items !=null and items.size()== 2">
               ( id='${items[0]}' and name='${items[1]}')
            </if>
        </foreach>
</delete>
<!--        ↓↓↓ 最终的sql ↓↓↓ 
delete from sys_config where (id='1' and name='1')
    or (id='2' and name='1')
    or (id='3' )
-->

3. mybatis 传入二维数组或双层list

list<String[]>或list<ArrayList<String>>或list<ArrayList<JSONObject>>,数组同理
传入数据:[ ["1","aaa"], ["2","bbb"] ]
注意 这里用 ${} 替换 #{} , ===> 数组用.length,List用.size()

<!-- int delByIds(@Param("confObjList") List<String[]> confObjList);  -->
<delete id="delByIds">
        delete from sys_config where  
        <foreach collection="confObjList" item="item1" open="(" separator="or" close=")">
             <foreach collection="item1" item="item2" open="(" separator="and" close=")">
                name =#{item2}
            </foreach>
        </foreach>
</delete>

<!--        ↓↓↓ 最终的sql ↓↓↓ 
delete from sys_config where (name='1' and name='aaa')
    or (name='2' and name='bbb')
    -->

相关文章

网友评论

      本文标题:mybatis 解析传入数组或list 实例

      本文链接:https://www.haomeiwen.com/subject/ecdzxdtx.html