Three implementation methods of Mybatis in conditional parameter passing (direct $, List, [])

The first method: the in condition is a concatenated string

If you directly pass in the concatenated where in conditions, such as ('111', '222', '333'), you need to use ${idlist} to pass parameters, that is, absolute references, instead of #, if you use # to pass
parameters It will be treated as a string by mybatis and add a layer of '' quotes, resulting in an error.
Advantages: Simple, convenient, efficient, Disadvantages: Cannot prevent SQL injection


The second method: the in condition is a List object.  
The in condition is directly passed into the List object, and then mybatis is spliced ​​to generate the in condition. This is very troublesome, but it can prevent SQL injection


The third method: the in condition is a String[] array

The in condition is passed directly to the [] array object, and mybatis is then spliced ​​to generate the in condition. This is very troublesome, but it can prevent SQL injection

If the project is large, you can actually overload all three implementations at the same time. I usually do this. I implement three DAO interfaces, the service layer has the same method name, and different implementation layers are called according to the different requirements of different modules.

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);

There is no need to modify the xml file of (2, 3), just modify the id corresponding to the method name of 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>

Guess you like

Origin blog.csdn.net/wh445306/article/details/111056331