學習Mybatis xml 常用關鍵語法


1.<where>和<if>

<!-- 根據條件查詢用戶 -->
<select id="queryUserByWhere" parameterType="user" resultType="user">
    SELECT id, username, birthday, sex, address FROM `user`
    WHERE 1=1
    <if test="sex != null and sex != ''">
        AND sex = #{sex}
    </if>
    <if test="username != null and username != ''">
        AND username LIKE
        '%${username}%'
    </if>
</select>

if 內部判斷的是傳遞過來的實體參數中的字段,判斷參數字段內容,如果為ture 執行if 內部命令

 

2.<include refid> 個人認為為標簽索引

    <sql id="Base_Column_List" >
            collegeID, collegeName
    </sql> 
     
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
           select 
            <include refid="Base_Column_List" />
            from t_notification_template
           where id = #{id,jdbcType=BIGINT}
     </select>

內部select查詢中可以使用索引標簽

好處是將公共部分提出來,方便后續開發使用。

4.<foreach>   參數為list時,可使用

<select id="countByUserList" resultType="_int" parameterType="list">
select count(*) from users
  <where>
    id in
    <foreach item="item" collection="list" separator="," open="(" close=")" index="">
      #{item.id, jdbcType=NUMERIC}
    </foreach>
  </where>
</select>

 


免責聲明!

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



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