美文网首页
动态 SQL

动态 SQL

作者: 路上捡只猫 | 来源:发表于2018-11-15 20:21 被阅读0次
  1. 根据不同的条件需要执行不同的 SQL 命令.称为动态 SQL
  2. MyBatis中动态SQL在mapper.xml中添加逻辑判断等.
  3. If使用
<   id="selByAccinAccout" resultType="log"> select * from log where 1=1
<!-- OGNL 表达式,直接写 key 或对象的属性.不需要添加任
何特字符号 -->
<if test="accin!=null and accin!=''">
        and accin=#{accin}
</if>
<if test="accout!=null and accout!=''">
        and accout=#{accout}
     </if>
   </select>
  1. <where>
    4.1 当编写 where 标签时,如果内容中第一个是 and 去掉第一个 and
    4.2 如果<where>中有内容会生成 where 关键字,如果没有内容不 生成 where 关键
    4.3 使用示例
    4.3.1 比直接使用<if>少写 where 1=1
<select id="selByAccinAccout" resultType="log"> select * from log
<where>
<if test="accin!=null and accin!=''"> and accin=#{accin}
</if>
<if test="accout!=null and accout!=''">
        and accout=#{accout}
     </if>
   </where>
</select>
  1. <choose><when><otherwise>
    5.1 只有有一个成立,其他都不执行. 5.2 代码示例
    5.2.1 如果 accin 和 accout 都不是 null 或不是””生成的 sql 中只 有 where accin=?
<select id="selByAccinAccout" resultType="log"> select * from log
<where>
<choose>
<when test="accin!=null and accin!=''">
 and accin=#{accin}
</when>
<when test="accout!=null and accout!=''">
           and accout=#{accout}
        </when>
     </choose>
   </where>
</select>
  1. <set>用在修改SQL中set从句
    6.1 作用:去掉最后一个逗号
    6.2 作用:如果<set>里面有内容生成 set 关键字,没有就不生成 6.3 示例
    6.3.1 id=#{id} 目的防止<set>中没有内容,mybatis 不生成 set 关 键字,如果修改中没有 set 从句 SQL 语法错误.
<update id="upd" parameterType="log" > update log
   <set>
     id=#{id},
<if test="accIn!=null and accIn!=''"> accin=#{accIn},
</if>
<if test="accOut!=null and accOut!=''">
accout=#{accOut},
</if> </set>
   where id=#{id}
</update>
  1. Trim
    7.1 prefix 在前面添加内容
    7.2 prefixOverrides 去掉前面内容
    7.3 suffix 在后面添加内容
    7.4 suffixOverrieds 去掉后面内容
    7.5 执行顺序去掉内容后添加内容
    7.6 代码示例
<update id="upd" parameterType="log"> update log
<trim prefix="set" suffixOverrides=","> a=a,
</trim>
where id=100
</update>
  1. <bind>
    8.1 作用:给参数重新赋值
    8.2 场景:
    8.2.1 模糊查询
    8.2.2 在原内容前或后添加内容
    8.3 示例
<   id="selByLog" parameterType="log" resultType="log">
<bind name="accin" value="'%'+accin+'%'"/>
#{money}
</select>
  1. <foreach>标签
    9.1 循环参数内容,还具备在内容的前后添加内容,还具备添加分 隔符功能.
    9.2 适用场景:in 查询中.批量新增中(mybatis 中 foreach 效率比较 低)
    9.2.1 如果希望批量新增,SQL 命令
insert into log VALUES (default,1,2,3),(default,2,3,4),(default,3,4,5)
9.2.2 openSession()必须指定
9.2.2.1 底层 JDBC 的 PreparedStatement.addBatch();
factory.openSession(ExecutorType.BATCH);

9.3 示例
9.3.1 collectino=”” 要遍历的集合
9.3.2 item 迭代变量, #{迭代变量名}获取内容
9.3.3 open 循环后左侧添加的内容
9.3.4 close 循环后右侧添加的内容
9.3.5 separator 每次循环时,元素之间的分隔符

<select id="selIn" parameterType="list" resultType="log">
     select * from log where id in
<foreach collection="list" item="abc" open="(" close=")" separator=",">
#{abc}
     </foreach>
   </select>
  1. <sql> 和<include>
    10.1 某些 SQL 片段如果希望复用,可以使用<sql>定义这个片段
<sql id="mysql"> id,accin,accout,money
</sql>

10.2 在<select>或<delete>或<update>或<insert>中使用<include> 引用

<select id="">
select 
<include refid="mysql"></include>
 from log
</select>

相关文章

  • MyBatis学习:动态sql

    1.动态sql 动态sql是mybatis中的一个核心,什么是动态sql?动态sql即对sql语句进行灵活操作,通...

  • 第十三章 使用动态SQL(一)

    第十三章 使用动态SQL(一) 动态SQL简介 动态SQL是指在运行时准备并执行的SQL语句。在动态SQL中,准备...

  • 第八章 动态SQL

    动态SQL中的元素介绍 动态SQL有什么作用 MyBatis提供了对SQL语句动态组装的功能 动态SQL中的元素 ...

  • 关于Mybatis的一些问题讨论

    Mybatis动态sql是做什么的?都有哪些动态sql?简述一下动态sql的执行原理 动态sql的用途 Mybat...

  • MyBatis5-动态 SQL

    动态 SQL 什么是动态 SQL 就是动态的对 SQL 进行组装 拼接. : 可以自动去...

  • geoserver动态颜色参数样式、动态sql配置实现

    geoserver动态颜色参数样式、动态sql配置实现 动态颜色参数样式、动态sql 访问方式: http://l...

  • 强大的动态SQL

    1 动态SQL# 那么,问题来了: 什么是动态SQL? 动态SQL有什么作用? 传统的使用JDBC的方法,相信大家...

  • MyBatis:动态 SQL

    1. 动态 SQL 简而言之,动态 SQL 就是在 Mapper 中使用分支、循环等逻辑。常见的动态 SQL 元素...

  • 无标题文章

    ### 一、简答题 #### 1、Mybatis动态sql是做什么的?都有哪些动态sql?简述一下动态sql的执行...

  • 五,MyBatis动态SQL

    一,含义:动态SQL是指根据参数数据动态组织SQL的技术 二,动态SQL的应用场景:比如淘宝搜索的时候,可以动态的...

网友评论

      本文标题:动态 SQL

      本文链接:https://www.haomeiwen.com/subject/earufqtx.html