<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Mybatis 九種數據庫 sql 實操方式,九種都會常用

          共 3325字,需瀏覽 7分鐘

           ·

          2021-04-09 14:31


          背景

          現在越來越流行基于 SpringBoot 開發(fā) web 應用,其中利用 mybatis 作為數據庫 CRUD 操作已成為主流,樓主以 mysql為例,總結了九大類使用 mybatis 操作數據庫 sql 小技巧分享給大家。

          1. 分頁查詢
          2. 預置 sql 查詢字段
          3. 一對多級聯查詢
          4. 一對一級聯查詢
          5. foreach 搭配 in 查詢
          6. 利用if 標簽拼裝動態(tài) where 條件
          7. 利用 chooseotherwise組合標簽拼裝查詢條件
          8. 動態(tài)綁定查詢參數:_parameter
          9. 利用 set 配合 if 標簽,動態(tài)設置數據庫字段更新值

          01 分頁查詢

          利用 limit 設置每頁 offset 索引和每頁 limit 大小。

          select?*?from?sys_user?u
          LEFT?JOIN?sys_user_site?s?ON?u.user_id?=?s.user_id
          LEFT?JOIN?sys_dept?d?ON?d.dept_id?=?s.dept_id
          LEFT?JOIN?sys_emailinfo?e?ON?u.user_id?=?e.userid?AND?e.MAIN_FLAG?=?'Y'
          <where>
          ?<include?refid="userCondition"/>
          </where>
          limit?#{offset},?#{limit}

          02 預置 sql 查詢字段

          <sql?id="columns">
          ??id,title,content,original_img,is_user_edit,province_id,status,porder
          </sql>

          查詢 select 語句引用 columns:

          <select?id="selectById"?resultMap="RM_MsShortcutPanel">
          ?seelct
          ?<include?refid="columns"/>
          ?from?cms_self_panel
          ?where
          ?id?=?#{_parameter}
          </select>

          03 一對多級聯查詢

          利用 mybatiscollection 標簽,可以在每次查詢文章主體同時通過 queryparaminstancelist 級聯查詢出關聯表數據。

          <resultMap?id="BaseResultMap"?type="com.unicom.portal.pcm.entity.ArticleEntity">
          ?<id?column="id"?jdbcType="BIGINT"?property="id"/>?
          ?<collection?property="paramList"?column="id"?select="queryparaminstancelist"/>
          </resultMap>

          queryparaminstancelistsql 語句

          <select?id="queryparaminstancelist"?resultMap="ParamInstanceResultMap">
          ?select?*?from?`cms_article_flow_param_instance`?where?article_id=#{id}?
          </select>

          04 一對一級聯查詢

          利用 mybatisassociation 標簽,一對一查詢關聯表數據。

          <resultMap?id="BaseResultMap"?type="com.unicom.portal.pcm.entity.ArticleEntity">
          ?<association?property="articleCount"?javaType="com.unicom.portal.pcm.entity.MsArticleCount"/>
          </resultMap>

          查詢sql語句:1cd4ec488d83e83012e55ec1e1a9ed12.webp

          MsArticlecount 實體對象的屬性值可以從 上面的 select 后的 sql 字段進行匹配映射獲取。

          05 foreach 搭配 in 查詢

          利用 foreach 遍歷 array 集合的參數,拼成 in 查詢條件

          <foreach?collection="array"?index="index"?item="item"?open="("?separator=","?close=")">
          ?#{item}
          </foreach>

          06 利用 if 標簽拼裝動態(tài) where 條件

          select?r.*,?(select?d.org_name?from?sys_dept?d?where?d.dept_id?=?r.dept_id)?deptName?from?sys_role?r
          <where>
          r.wid?=?#{wid}
          <if?test="roleName?!=?null?and?roleName.trim()?!=?''">
          and?r.`role_name`?like?concat('%',#{roleName},'%')
          </if>
          <if?test="status?!=?null?and?status.trim()?!=?''">
          and?r.`status`?=?#{status}
          </if>
          </where>

          07 利用 choose 和 otherwise 組合標簽拼裝查詢條件

          <choose>
          ?<when?test="sidx?!=?null?and?sidx.trim()?!=?''">
          ?order?by?r.${sidx}?${order}
          ?</when>
          ?<otherwise>
          ?order?by?r.role_id?asc
          ?</otherwise>
          </choose>

          08 隱形綁定參數:_parameter

          _parameter 參數的含義

          Mapper、associationcollection 指定只有一個參數時進行查詢時,可以使用 _parameter,它就代表了這個參數。

          另外,當使用 Mapper指定方法使用 @Param 的話,會使用指定的參數值代替。

          SELECT?id,?grp_no?grpNo,?province_id?provinceId,?status?FROM?tj_group_province
          <where>?
          ?...
          ?<if?test="_parameter!=null">
          ?and?grp_no?=?#{_parameter}
          ?</if>
          </where>

          09 利用 set 配合 if 標簽,動態(tài)設置數據庫字段更新值

          <update?id="updateById">
          ?UPDATE?cms_label
          ?<set>
          ???<if?test="labelGroupId?!=?null">
          ?????label_group_id?=?#{labelGroupId},
          ???</if>
          ???dept_id?=?#{deptId},
          ???<if?test="recommend?!=?null">
          ?????is_recommend?=?#{recommend},
          ???</if>
          ?</set>
          ?WHERE?label_id?=?#{labelId}?
          </update




          七種正確使用 Redis分布式鎖的姿勢,你學廢了嗎?

          2021-04-06

          df1d3d6e5b77b0576cb10ef2b75c9fa6.webp

          用動圖講解 MySQL 索引底層B+樹,清晰明了多了

          2021-04-02

          31c55c70dc32e61f46000a9be4852b71.webp

          Jenkins Pipeline動態(tài)使用Git分支名稱的技巧,可以觸類旁通的那種

          2021-03-29

          f55324d763586887c30320586d38750b.webp

          瀏覽 38
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  一级a啪啪啪| 超碰在线观看网站 | 538在线精品 | 99 热在线观看 | 亚洲天堂资源 |