美文网首页
Mybaits动态sql

Mybaits动态sql

作者: c_gentle | 来源:发表于2021-06-21 09:38 被阅读0次

    一、传入的参数为集合或数组,在xml里面进行遍历时

    1.传入参数格式

    private List<String> zxValue = new ArrayList<>();
    private List<String> bxgdList = Arrays.asList("1", "2");
    private List<String> qzsbList = Arrays.asList("3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14");
    private List<String> wzList = Arrays.asList("15", "16");
    private List<String> zxList = Arrays.asList("17", "18", "19", "20");
    

    2.xml里面进行遍历时的写法

     SELECT * FROM PS_LAWCASE C
            join
            PS_LAWCASE_PERSON P
            ON C.ID=P.LAWCASE_ID
            join PS_SPECIAL S
            on C.ID= S.LAWCASE_ID
            <where>
            <if test="psQueryVo.zxValue!=null and psQueryVo.zxValue.size>0">
            and S.type in
                <foreach collection="psQueryVo.zxValue" item="type" index="index"
                         open="(" close=")" separator=",">
                    #{type,}
                </foreach>
            </if>
                <if test="psQueryVo.title!=null and psQueryVo.title !=''">
                    <bind name="title" value="'%'+psQueryVo.title+'%'"/>
                    AND c.title like #{psQueryVo.title}
                </if>
    

    需要注意的是在判断属性是否为空时不是比较的""值而是比较的size

    二、当在xml里面需要对传入的值进行like以及使用%时,可以使用bind进行绑定

       <if test="psQueryVo.name!=null and psQueryVo.name !=''">
                    <bind name="name" value="'%'+psQueryVo.name+'%'"/>
                    AND p.name like #{psQueryVo.name}
                </if>
    

    相关文章

      网友评论

          本文标题:Mybaits动态sql

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