1、簡單SQL使用
//從***數據表獲取統計數據 @Select("select count(*) from issues where issue_type = #{type}") String getIssueCount(String type);
2、動態SQL使用
//獲取時間段內issue詳細信息(可根據項目名、開發者名、問題類型獲取) @Select({"<script>", "select id,committername,type,count,projectname,file,line,message,creationdate,updatedate from issue", "where creationdate BETWEEN #{startDate} AND #{endDate}", "<if test='committer != null and committer.length > 0'>", "AND committername IN", "<foreach item='item' index='index' collection='committer' open='(' close=')' separator=','>", "#{item}", "</foreach>", "</if>", "<if test='type != null and type.length > 0'>", "AND type IN", "<foreach item='item' index='index' collection='type' open='(' close=')' separator=','>", "#{item}", "</foreach>", "</if>", "<if test='project != null and project.length > 0'>", "AND projectname IN", "<foreach item='item' index='index' collection='project' open='(' close=')' separator=','>", "#{item}", "</foreach>", "</if>", "</script>"}) List<IssueModel> getDetailIssue(@Param("startDate") String startDate, @Param("endDate")String endDate, @Param("committer") String[] committer, @Param("type") String[] type, @Param("project") String[] project);
知識點:
(1)注解寫動態SQL,用<script>標簽包圍,然后像xml語法一樣書寫。
(2)SQL的拼接可以使用+號,也可以使用逗號。我這里使用的是逗號,要使用+號可以把<script>前后的大括號去掉。
(2)實現IN查詢中 > 符號需要轉義為 > ,其中foreach的collection直接寫成@param中的值即可。
(3)這是一種使用注解完全替代XML的方法,稍微復雜的SQL語句推薦使用XML方式。