zoukankan      html  css  js  c++  java
  • MyBatis 动态SQL!

    12、动态SQL

    什么是动态SQL:动态SQL就是指根据不同的条件生成不同的SQL语句

    利用动态 SQL,可以彻底摆脱这种痛苦。

    如果你之前用过 JSTL 或任何基于类 XML 语言的文本处理器,你对动态 SQL 元素可能会感觉似曾相识。在 MyBatis 之前的版本中,需要花时间了解大量的元素。借助功能强大的基于 OGNL 的表达式,MyBatis 3 替换了之前的大部分元素,大大精简了元素种类,现在要学习的元素种类比原来的一半还要少。
    
    if
    choose (when, otherwise)
    trim (where, set)
    foreach
    

    搭建环境(20-12-23)

    CREATE TABLE `blog`(
    `id` VARCHAR(50) NOT NULL COMMENT '博客id',
    `title` VARCHAR(100) NOT NULL COMMENT '博客标题',
    `author` VARCHAR(30) NOT NULL COMMENT '博客作者',
    `create_time` DATETIME NOT NULL COMMENT '创建时间',
    `views` INT(30) NOT NULL COMMENT '浏览量'
    )ENGINE=INNODB DEFAULT CHARSET=utf8
    

    创建一个基础工程

    1. 导包(Lombok可选)

    2. 编写配置文件

    3. 编写实体类

      public class Blog {
          private int id;
          private String title;
          private String author;
          private Date createTime;
          private int views;
      }
      
    4. 编写实体类对应的Mapper接口 和 Mapper.xml文件

      public interface BlogMapper {
      
          //插入数据
          int addBlog(Blog blog);
      }
      
      <mapper namespace="com.kuang.dao.BlogMapper">
          <insert id="addBlog" parameterType="blog">
              insert into mybatis.blog (id, title, author, create_time, views)
              values (#{id},#{title},#{author},#{createTime},#{views})
          </insert>
      </mapper>
      

    IF

    <select id="queryBlogIF" parameterType="map" resultType="blog">
        select * from mybatis.blog where 1=1
        <if test="title != null">
           and title = #{title}
        </if>
        <if test="author != null">
            and author = #{author}
        </if>
    </select>
    

    choose (when, otherwise)

    <select id="queryBlogChoose" parameterType="map" resultType="blog">
        select * from mybatis.blog
        <where>
            <choose>
                <when test="title != null">
                    title = #{title}
                </when>
                <when test="author != null">
                    and author=#{author}
                </when>
                <otherwise>
                    and views = #{views}
                </otherwise>
            </choose>
        </where>
    </select>
    

    trim (where, set)

    <select id="queryBlogIF" parameterType="map" resultType="blog">
        select * from mybatis.blog
        #         where 1=1
        <where>
            <if test="title != null">
                title = #{title}
            </if>
            <if test="author != null">
                and author = #{author}
            </if>
        </where>
    </select>
    
    <update id="updateBlog" parameterType="map">
        update mybatis.blog
        <set>
            <if test="title!=null">
                title = #{title},
            </if>
            <if test="author!=null">
                author = #{author}
            </if>
        </set>
        where id=#{id}
    </update>
    

    所谓的动态SQL,本质还是SQL语句,只是我们可以在SQL层面,去执行一个逻辑代码

    if

    where , set , choose , when

    本人学习Java的一片私人空间……
  • 相关阅读:
    特NB的本地语音识别方案(转)
    海思MPP(转)
    单片机实现PT2262解码示例代码(转)
    海思HI35XX之----视频处理单元各通道间的关系(转)
    海思AI芯片(Hi3519A/3559A)方案学习(三)Ubuntu18.0.4上编译Hi3519AV100 uboot和kernel(转)
    Hi3519V101开发环境搭建(二)(转)
    Git 原理
    海思3531添加移远EC20 4g模块(转)
    将移远通信的EC20驱动移植到NUC972上(转)
    Shell 正则表达式
  • 原文地址:https://www.cnblogs.com/none-space/p/14181777.html
Copyright © 2011-2022 走看看