您的位置 首页 java

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

欢迎关注我的头条号:Wooola,专注于Java、Golang、微服务架构,致力于每天分享原创文章、快乐编码和开源技术。
 

前言

现在越来越流行基于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、分页查询

以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语句:

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>
 

文章来源:智云一二三科技

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

文章地址:https://www.zhihuclub.com/197389.shtml

关于作者: 智云科技

热门文章

网站地图