使用注解的方式直接在語句中拼寫動態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); */
至此,小結結束!