Mybatis in 条件传参三种实现方法(直接$,List,[])

文章讨论了在MyBatis中处理IN条件的三种不同方式:1)使用字符串参数,简单但易受SQL注入攻击;2)通过List对象,复杂但安全;3)使用String数组,同样安全但需额外拼接。对于大型项目,建议为同一服务方法提供三种实现以适应不同需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一种方法:in 条件为拼接好的字符串

如果直接传入拼接好的where in 条件, 比如(‘111’,‘222’,‘333’),则需要使用${idlist}传参,即绝对引用,而不能使用#
, 如果使用#传参会被mybatis当成字符串再添加一层’'引号,导致错误.
优点:简单方便,高效,缺点:不能防止SQL注入

第二种方法:in 条件为List对象
in条件直接传入List对象,让mybatis再去拼接生成in条件,这个很麻烦,但是可以防止SQL注入

第三种方法:in 条件为String[] 数组

in条件直接传入[]数组对象,让mybatis再去拼接生成in条件,这个很麻烦,但是可以防止SQL注入

如果项目大,其实可以同时重载三种都实现,我一般都会这样,实现三种DAO接口,service层相同方法名,根据不同的模块不同的需求调用不同的实现层

Service:

    int deleteMenuByIdList(String idlist,int delcount,int lastsort);



    int deleteMenuByIdList(List<String> idlist, int delcount,int lastsort);



    int deleteMenuByIdList(String[] idlist, int delcount,int lastsort);

Dao:

    //用这种写法方便,idlist直接拼接好,xml中用 in ${idlist}接受参数



    int deleteMenuByIdList(@Param("idlist")String idlist, @Param("delcount")int delcount, @Param("lastsort")int lastsort);



    //用这种写法直接传List对象,xml中再写循环拼接,麻烦



    int deleteMenuByIdList2(@Param("idlist")List<String> idlist, @Param("delcount")int delcount, @Param("lastsort")int lastsort);



    //用这种写法直接传String[]数组,xml中再写循环拼接,麻烦



    int deleteMenuByIdList3(@Param("idlist")String[] idlist, @Param("delcount")int delcount, @Param("lastsort")int lastsort);

(2,3)的xml文件中不需要做修改,只需要修改一下id对应到DAO的方法名即可。

mappper.xml

1,



    <delete id="deleteMenuByIdList" >



      delete from s_menu  where menu_id in ${idlist};



      update s_menu set sort=sort-#{delcount} where sort >= #{lastsort} and menu_id not in ${idlist};



    </delete>



 



2,



    <delete id="deleteMenuByIdList2" >



        delete from s_menu where menu_id in



        <foreach collection="idlist" item="menu_id" separator="," open="(" close=")">



            #{menu_id}



        </foreach>



      ;update s_menu set sort=sort-#{delcount} where sort >= #{lastsort} and menu_id not in



       <foreach collection="idlist" item="menu_id" separator="," open="(" close=")">



           #{menu_id}



       </foreach>;



    </delete>



 



3,



    <delete id="deleteMenuByIdList3" >



        delete from s_menu where menu_id in



        <foreach collection="idlist" item="menu_id" separator="," open="(" close=")">



            #{menu_id}



        </foreach>



        ;update s_menu set sort=sort-#{delcount} where sort >= #{lastsort} and menu_id not in



        <foreach collection="idlist" item="menu_id" separator="," open="(" close=")">



            #{menu_id}



        </foreach>;



    </delete>
MyBatis中,如果要传递集合、列表、数组或Map等数据结构作为参数,我们可以使用以下方式进行配置和传参: 1. 传递集合或列表参数: 在传递集合或列表参数时,我们可以在SQL语句中使用foreach循环遍历每个参数。例如,我们有一个查询语句如下: ``` SELECT * FROM user WHERE id IN (1, 2, 3); ``` 如果我们想要通过集合参数传递id列表,我们的Mapper文件可以这样写: ``` <select id="getUserByIdList" resultType="User"> SELECT * FROM user WHERE id IN <foreach item="id" index="index" collection="idList" open="(" separator="," close=")"> #{id} </foreach> </select> ``` 其中,我们通过collection指定了传入的idList集合参数,在foreach标签中遍历idList,将每一个id都解析为#{id}参数。这样,就能够动态地构建查询条件,得到期望的查询结果。 2. 传递数组参数: 在传递数组参数时,我们可以通过Java中的数组语法直接传入参数值,例如: ``` <select id="getUserByIds" resultType="User"> SELECT * FROM user WHERE id IN <foreach item="id" index="index" collection="array" open="(" separator="," close=")"> #{id} </foreach> </select> ``` 在此例中,我们通过collection指定了传入的array数组参数,与遍历集合方法类似。 3. 传递Map参数: 如果要传递一个Map作为参数,我们可以为Map中的每个键值对设置一个key和value属性,以便在SQL中使用。例如: ``` <select id="getUserByMap" resultType="User"> SELECT * FROM user WHERE age = #{age} AND <if test="name!=null">name = #{name}</if> AND <if test="email!=null">email = #{email}</if> </select> ``` 在这个例子中,我们通过age、name、email三个键来传递参数值,同时使用if条件语句来根据不同的参数情况动态地构建SQL查询语句。 综上所述,不同的参数类型在MyBatis中的传递方式有所不同,但可以通过MyBatis内置的标签,如foreach、if等,来方便地处理动态SQL查询条件的构建。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值