欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MyBatis動態(tài)SQL實(shí)現(xiàn)配置過程解析

 更新時間:2020年03月19日 15:01:00   作者:黃大姐の老公  
這篇文章主要介紹了MyBatis動態(tài)SQL實(shí)現(xiàn)配置過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

動態(tài)SQL

什么是動態(tài)SQL:

​ 動態(tài)SQL就是根據(jù)不同的條件生成不同的SQL語句

  • if
  • choose(when,otherwise)
  • trim(where,set)
  • foreach

1、搭建環(huán)境

建表

CREATE TABLE `bolg`(
  `id` VARCHAR(50) NOT NULL COMMENT '博客id',
  `title` VARCHAR(100) not null comment '博客標(biāo)題',
  `author` VARCHAR(30) not null comment '博客作者',
  `creat_time` datetime not null comment '創(chuàng)建時間',
  `views` int(30) not null comment '瀏覽量'
)ENGINE=InnoDB DEFAULT CHARSET=utf8

創(chuàng)建一個基礎(chǔ)工程

導(dǎo)包

編寫配置文件

編寫實(shí)體類

@Data
public class Blog {
  private int id;
  private String title;
  private String author;
  private Date creatTime;
  private int views;
}

編寫實(shí)體類對應(yīng)的Mapper接口和Mapper.xm

2、IF

<select id="queryBlogIF" parameterType="map" resultType="com.rui.pojo.Blog">
  select * from mybatis.bolg where 1=1
  <if test="title != null">
    and title = #{title}
  </if>
  <if test="author != null">
    and author = #{author}
  </if>
</select>
@Test
public void queryBlogIF(){
  SqlSession sqlSession = MyBatisUtils.getSqlSession();
  BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
  HashMap map = new HashMap();
  map.put("author","尹銳");
  List<Blog> blogs = mapper.queryBlogIF(map);
  for (Blog blog : blogs) {
    System.out.println(blog);
  }
  sqlSession.close();
}

3、choose(when,otherwise)

<select id="queryBlogChoose" parameterType="map" resultType="com.rui.pojo.Blog">
  select * from mybatis.bolg
  <where>
    <choose>
      <when test="title != null">
        title=#{title}
      </when>
      <when test="author!=null">
        and author = #{author}
      </when>
      <otherwise>
        and views = #{views}
      </otherwise>
    </choose>
  </where>
</select>

4、trim(where,set)

select * from mybatis.bolg
<where>
<if test="title != null">
  title = #{title}
</if>
<if test="author != null">
  and author = #{author}
</if>
</where>
<update id="updateBlog" parameterType="map">
  update mybatis.bolg
  <set>
    <if test="title != null">
      title = #{title},
    </if>
    <if test="author != null">
      author = #{author},
    </if>
  </set>
  where id = #{id}
</update>

所謂的動態(tài)SQL,本質(zhì)還是SQL語句,只是我們可以在SQL層面,去執(zhí)行一些邏輯代碼

5、Foreach

select * from user where 1=1 and 
 <foreach item="id" index="index" collection="ids"
   open="(" separator="or" close=")">
    #{id}
 </foreach>

(id=1 or id=2 or id=3)

<!--
select * from mybatis.bolg where 1=1 and (id=1 or id=2 or id=3)

我們現(xiàn)在傳遞一個萬能的map,這個map中可以存在一個map
-->
<select id="queryBlogForeach" parameterType="map" resultType="com.rui.pojo.Blog">
  select * from mybatis.bolg

  <where>
  <foreach collection="ids" item="id" open="(" close=")" separator="or">
    id = #{id}
  </foreach>
  </where>
</select>

動態(tài)SQL就是在拼接SQL語句,我們只要保證SQL的正確性,按照SQL的格式,去排列組合就可以了

建議:

先在MySQL中寫出完整的SQL,再對應(yīng)的去修改成為我們的動態(tài)SQL

SQL片段

有的時候,我們可能會將一些公共的部分抽取處理,方便復(fù)用

使用SQL標(biāo)簽抽取公共的部分

<sql id="if-title-author">
  <if test="title != null">
    title = #{title}
  </if>
  <if test="author != null">
    and author = #{author}
  </if>
</sql>

在需要使用的地方使用Include標(biāo)簽引用即可

<select id="queryBlogIF" parameterType="map" resultType="com.rui.pojo.Blog">
  select * from mybatis.bolg
  <where>
    <include refid="if-title-author"></include>
  </where>
</select>

注意事項(xiàng):

最好基于單表來定義SQL片段

不要存在where或者set標(biāo)簽,片段里盡量只有if就好了

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java list 比較詳解及實(shí)例

    java list 比較詳解及實(shí)例

    這篇文章主要介紹了java list 比較詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • 簡易版SpringBoot自定義模擬實(shí)現(xiàn)

    簡易版SpringBoot自定義模擬實(shí)現(xiàn)

    SpringBoot作為目前最流行的框架之一,極大地提高了開發(fā)效率和降低了學(xué)習(xí)成本,使得開發(fā)人員能夠更專注于業(yè)務(wù)邏輯的實(shí)現(xiàn),而無需過多關(guān)注底層框架的配置和集成,本文模擬實(shí)現(xiàn)簡易版SpringBoot
    2024-01-01
  • 淺談一下Spring的核心基礎(chǔ)IOC與DI

    淺談一下Spring的核心基礎(chǔ)IOC與DI

    這篇文章主要介紹了Spring的核心基礎(chǔ)IOC與DI的詳細(xì)用法,spring技術(shù)是現(xiàn)在企業(yè)開發(fā)中幾乎必備的技術(shù)選型,那么學(xué)好spring就很重要,本篇著重講解spring的核心機(jī)制,IOD與DI,一起來看看吧
    2023-03-03
  • Spring Security實(shí)現(xiàn)退出登錄和退出處理器

    Spring Security實(shí)現(xiàn)退出登錄和退出處理器

    本文主要介紹了Spring Security實(shí)現(xiàn)退出登錄和退出處理器,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • Java多線程 線程同步與死鎖

    Java多線程 線程同步與死鎖

    這篇文章主要介紹了 Java多線程 線程同步與死鎖的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • java中的switch case語句使用詳解

    java中的switch case語句使用詳解

    這篇文章主要介紹了java中的switch case語句使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Java中的System類、BigInteger類和BigDecimal類詳解

    Java中的System類、BigInteger類和BigDecimal類詳解

    這篇文章主要介紹了Java中的System類、BigInteger類和BigDecimal類詳解,arraycopy()方法,復(fù)制數(shù)組元素,比較適合底層調(diào)用,一般使用Arrays.copyOf()完成復(fù)制數(shù)組,需要的朋友可以參考下
    2023-09-09
  • Java嵌套for循環(huán)的幾種常見優(yōu)化方案

    Java嵌套for循環(huán)的幾種常見優(yōu)化方案

    這篇文章主要給大家介紹了關(guān)于Java嵌套for循環(huán)的幾種常見優(yōu)化,在Java中優(yōu)化嵌套for循環(huán)可以通過以下幾種方式來提高性能和效率,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-07-07
  • 詳解查看JAVA API及JAVA源碼的方法

    詳解查看JAVA API及JAVA源碼的方法

    這篇文章主要介紹了查看JAVA API及JAVA源碼的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • MyBatis-Plus如何解決主鍵自增問題

    MyBatis-Plus如何解決主鍵自增問題

    這篇文章主要介紹了MyBatis-Plus如何解決主鍵自增問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07

最新評論