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的一片私人空间……
  • 相关阅读:
    《算法竞赛入门经典》《算法竞赛入门经典——训练之南》:勘误、讨论及代码
    codeforces 340B Maximal Area Quadrilateral(叉积)
    codeforces 340C Tourist Problem(简单数学题)
    codeforces 340A The Wall(简单数学题)
    UVALive 4043 Ants(二分图完美匹配)
    UVA 11865 Stream My Contest(最小树形图)
    UVA 11354 Bond(最小瓶颈路+倍增)
    UVALive 5713 Qin Shi Huang's National Road System(次小生成树)
    UVALive 3661 Animal Run(最短路解最小割)
    卡尔曼滤波器
  • 原文地址:https://www.cnblogs.com/none-space/p/14181777.html
Copyright © 2011-2022 走看看