在使用@select的时候,如何加入if判断条件
通常大项目都是用mapper.xml进行拼接,但是小项目可以使用mybatis-plus进行快速开发。
库表没有生成mapper.xml中的增删改如果在哪里专门进行操作,会比较耗时
而联表复杂的操作又不方便直接使用wrapper拼接

因此需要研究一下,分享出来

 @Select({
            "<script>",
            "select xxxxx from a_table a ",
            "left join b_table b on a.class_id = b.id   ",
            "left join c_table c on a.xx_id = c.id  ",
            "where 1=1 ",
            "<if test= \"gender!=null and gender!='' \">",
            "and u.gender like #{gender}",
            "</if>",
            "<if test= \"phoneNumber!=null and phoneNumber!='' \">",
            "and u.origin_user_id like #{phoneNumber}",
            "</if>",
            "<if test= \"username!=null and username!='' \">",
            "and u.nick_name like #{username}",
            "</if>",
            "<if test= \"classId!=null and classId!='' \">",
            "and a.class_id like #{classId}",
            "</if>",
            "order by create_time desc limit  #{start},#{size} ",
            "</script>"
    })

主要的难题是处理使用转移处理引号 \"
数据进行了脱敏

用in加集合的也分享出来

 @Select({
            "<script>",
            "select  book_id,count(*) as count  from book_like_table where book_id in ",
            "<foreach collection='bookIds' item='book_id' open='(' separator=',' close=')'>",
            "#{book_id}",
            "</foreach>",
            " GROUP BY book_id ",
            "</script>"
    })

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐