亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747


數(shù)據(jù)庫(kù)ORM框架:Mybatis九種sql實(shí)操方式,你get到了嗎?

 

現(xiàn)在越來(lái)越流行基于Springboot開(kāi)發(fā)web應(yīng)用,其中利用MyBatis作為數(shù)據(jù)庫(kù)CRUD操作已成為主流,樓主總結(jié)了九大類使用mybatis操作數(shù)據(jù)庫(kù)sql小技巧分享給大家。

  1. 分頁(yè)查詢
  2. 預(yù)置sql查詢字段
  3. 一對(duì)多級(jí)聯(lián)查詢
  4. 一對(duì)一級(jí)聯(lián)查詢
  5. in查詢
  6. 利用if標(biāo)簽拼裝動(dòng)態(tài)where條件
  7. 利用choose和otherwise組合標(biāo)簽拼裝查詢條件
  8. 動(dòng)態(tài)綁定查詢參數(shù):_parameter
  9. 利用set配合if標(biāo)簽,動(dòng)態(tài)設(shè)置數(shù)據(jù)庫(kù)字段更新值

1、分頁(yè)查詢

數(shù)據(jù)庫(kù)ORM框架:Mybatis九種sql實(shí)操方式,你get到了嗎?

 

以MySQL為例,利用limit設(shè)置每頁(yè)offset索引和每頁(yè)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、預(yù)置sql查詢字段

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

查詢select語(yǔ)句引用columns:

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

3、一對(duì)多級(jí)聯(lián)查詢

利用mybatis的collection標(biāo)簽,可以在每次查詢文章主體同時(shí)通過(guò)queryparaminstancelist級(jí)聯(lián)查詢出關(guān)聯(lián)表數(shù)據(jù)。

<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語(yǔ)句

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

4、一對(duì)一級(jí)聯(lián)查詢

利用mybatis的association標(biāo)簽,一對(duì)一查詢關(guān)聯(lián)表數(shù)據(jù)。

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

查詢sql語(yǔ)句:

數(shù)據(jù)庫(kù)ORM框架:Mybatis九種sql實(shí)操方式,你get到了嗎?

 

MsArticlecount實(shí)體對(duì)象的字段值可以從下面select中的字段值獲取。

5、in查詢

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

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

6、利用if標(biāo)簽拼裝動(dòng)態(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>

7、利用choose和otherwise組合標(biāo)簽拼裝查詢條件

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

8、動(dòng)態(tài)綁定查詢參數(shù):_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標(biāo)簽,動(dòng)態(tài)設(shè)置數(shù)據(jù)庫(kù)字段更新值

<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>

 

分享到:
標(biāo)簽:框架 數(shù)據(jù)庫(kù) ORM
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定