Mybatis 九种数据库 sql 实操方式,九种都会常用

日拱一兵

共 3325字,需浏览 7分钟

 · 2021-04-09


背景

现在越来越流行基于 SpringBoot 开发 web 应用,其中利用 mybatis 作为数据库 CRUD 操作已成为主流,楼主以 mysql为例,总结了九大类使用 mybatis 操作数据库 sql 小技巧分享给大家。

  1. 分页查询
  2. 预置 sql 查询字段
  3. 一对多级联查询
  4. 一对一级联查询
  5. foreach 搭配 in 查询
  6. 利用if 标签拼装动态 where 条件
  7. 利用 chooseotherwise组合标签拼装查询条件
  8. 动态绑定查询参数:_parameter
  9. 利用 set 配合 if 标签,动态设置数据库字段更新值

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 标签拼装动态 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 参数的含义

Mapperassociationcollection 指定只有一个参数时进行查询时,可以使用 _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 标签,动态设置数据库字段更新值

<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动态使用Git分支名称的技巧,可以触类旁通的那种

2021-03-29

f55324d763586887c30320586d38750b.webp

浏览 11
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报