Mybatis之通用mapper使用注解的方式写动态sql-小结

使用注解的方式直接在语句中拼写动态SQL语句

注意事项:

  @Select("<script> SELECT DISTINCT project_id FROM `project_partner` WHERE belong=1 AND project_id " +
            "IN " +
            "<foreach item='item' index='index' collection='ids' open='(' close=')' separator=',' >" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    List<Integer> findAllId(@Param("ids") List<Integer> item);

    /**
     * 等价于上者
     * 小结:
     * 如果加上 @Param 注解,(可以不指定parameterType),并且注解值等同于 collection 中的值
     * 如果不加上 @Param 注解,需要指定 collection 的类型,是 list、map、array 的三种类型中的一种
     * 并且使用动态sql的话需要加上 <script> 的闭合标签,并且如果其中出现 < 或 > 符号,需要进行转义,否则会出错:
     *                  Caused by: org.xml.sax.SAXParseException: 元素内容必须由格式正确的字符数据或标记组成
     * @Select(    "<script> SELECT DISTINCT project_id FROM `project_partner` WHERE belong=1 AND project_id " +
     *             "IN " +
     *             "<foreach item='item' index='index' collection='list' open='(' close=')' separator=',' >" +
     *             "#{item}" +
     *             "</foreach>" +
     *             "</script>")
     *     List<Integer> findAllId(List<Integer> item);
     */

 

至此,小结结束!

posted @ 2020-01-04 10:56  koooin  阅读(4172)  评论(0编辑  收藏  举报