记录折腾的那点事
在折腾的道路上永不止步

基于Springboot,Mybatis九种数据库sql实操方式,你get到了吗?

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

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

1、分页查询

基于Springboot,Mybatis九种数据库sql实操方式,你get到了吗?

以mysql为例,利用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}

2、预置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>

3、一对多级联查询

利用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的select sql语句

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

4、一对一级联查询

利用mybatis的association标签,一对一查询关联表数据。

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

查询sql语句:

基于Springboot,Mybatis九种数据库sql实操方式,你get到了吗?

MsArticlecount实体对象的字段值可以从下面select中的字段值获取。

5、in查询

利用foreach遍历array集合的参数,拼成in查询条件

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

6、利用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>

7、利用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>

8、动态绑定查询参数:_parameter

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

9、利用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>

赞(1)
未经允许不得转载:ghMa » 基于Springboot,Mybatis九种数据库sql实操方式,你get到了吗?
分享到: 更多 (0)

评论 1

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    除了预置sql查询字段,其他我竟然都没用过

    tt4年前 (2020-06-03)回复