• Springboot2注解使用Mybatis动态SQL


    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 &gt; 0'>",
                "AND committername IN",
                "<foreach item='item' index='index' collection='committer' open='(' close=')' separator=','>",
                "#{item}",
                "</foreach>",
                "</if>",
                "<if test='type != null and type.length &gt; 0'>",
                "AND type IN",
                "<foreach item='item' index='index' collection='type' open='(' close=')' separator=','>",
                "#{item}",
                "</foreach>",
                "</if>",
                "<if test='project != null and project.length &gt; 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查询中 > 符号需要转义为 &gt; ,其中foreach的collection直接写成@param中的值即可。

    (3)这是一种使用注解完全替代XML的方法,稍微复杂的SQL语句推荐使用XML方式。

    以上个人拙作,不喜勿喷,希望能给路上同仁带去点光。

    若不小心对你有启发,评论留下你的故事,点赞分享让更多人为你受益。

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    基于ARM的指纹识别门禁系统设计方案
    基于ARM9的指纹识别系统的设计和实现
    使用TI 的低功耗C5x DSP的指纹识别方框图和解决方
    基于ATmega162的指纹识别电子机械锁设计
    一种光学指纹识别系统的设计方案
    利用DSP高速处理能力对指纹识别的系统方案
    加性噪声--传递概率密度函数=噪声概率密度函数
    信号的方差与功率的关系
    Bayes factor
    频率学派贝叶斯学派估计的区别
  • 原文地址:https://www.cnblogs.com/porotin/p/10194160.html
Copyright © 2020-2023  润新知