mybatis動態SQL中的sql片段


https://www.cnblogs.com/qiankun-site/p/5762352.html

在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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM