zoukankan      html  css  js  c++  java
  • mybatis动态SQL中的sql片段

    在mybatis中通过使用SQL片段可以提高代码的重用性,如下情景:

      1、创建动态SQL

        <sql id="sql_count">select count(*)</sql>

      2、使用

        <select id="selectListCountByParam" parameterType="map" resultType="String">

          <include refid="sql_count"/> from table_name

        </select>

      3、解析

        在使用sql片段时使用include标签通过sql片段的id进行引用,sql片段的ID在当前空间必须为唯一的

        当然,sql片段中也可以写其他的内容,只要符合语法规范都是可以的。如下:
        <sql id="sql_where">
          <trim prefix="WHERE" prefixoverride="AND | OR">
            <if test="id != null">AND id=#{id}</if>
            <if test="name != null and name.length()>0">AND name=#{name}</if>
            <if test="gender != null and gender.length()>0">AND gender=#{gender}</if>
          </trim>
        </sql>


        <select id="updateByKey" parameterType="Map" resultType="List">
          select * from user
          <include refid="sql_where">
        </select>

  • 相关阅读:
    面试算法爱好者书籍/OJ推荐
    go语言——数据类型
    go编程规范
    Go语言常见语法错误
    go语言入门
    python——itertools
    linux sed使用
    python——optparse模块
    Linux基本配置
    文件包含
  • 原文地址:https://www.cnblogs.com/qiankun-site/p/5762352.html
Copyright © 2011-2022 走看看