Mybatis 九種數據庫 sql 實操方式,九種都會常用
背景
現在越來越流行基于 SpringBoot 開發(fā) web 應用,其中利用 mybatis 作為數據庫 CRUD 操作已成為主流,樓主以 mysql為例,總結了九大類使用 mybatis 操作數據庫 sql 小技巧分享給大家。
- 分頁查詢
- 預置
sql查詢字段 - 一對多級聯查詢
- 一對一級聯查詢
foreach搭配in查詢- 利用
if標簽拼裝動態(tài)where條件 - 利用
choose和otherwise組合標簽拼裝查詢條件 - 動態(tài)綁定查詢參數:
_parameter - 利用
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 一對多級聯查詢
利用 mybatis 的 collection 標簽,可以在每次查詢文章主體同時通過 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>
queryparaminstancelist 的 sql 語句
<select?id="queryparaminstancelist"?resultMap="ParamInstanceResultMap">
?select?*?from?`cms_article_flow_param_instance`?where?article_id=#{id}?
</select>
04 一對一級聯查詢
利用 mybatis 的 association 標簽,一對一查詢關聯表數據。
<resultMap?id="BaseResultMap"?type="com.unicom.portal.pcm.entity.ArticleEntity">
?<association?property="articleCount"?javaType="com.unicom.portal.pcm.entity.MsArticleCount"/>
</resultMap>
查詢sql語句:
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、association、collection指定只有一個參數時進行查詢時,可以使用_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
Jenkins Pipeline動態(tài)使用Git分支名稱的技巧,可以觸類旁通的那種
評論
圖片
表情
