最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Tk.mybatis零sql語(yǔ)句實(shí)現(xiàn)動(dòng)態(tài)sql查詢的方法(4種)

 更新時(shí)間:2021年12月01日 17:13:37   作者:tyler.shi  
有時(shí)候,查詢數(shù)據(jù)需要根據(jù)條件使用動(dòng)態(tài)查詢,這時(shí)候需要使用動(dòng)態(tài)sql,本文主要介紹了Tk.mybatis零sql語(yǔ)句實(shí)現(xiàn)動(dòng)態(tài)sql查詢的方法,感興趣的可以了解一下

有時(shí)候,查詢數(shù)據(jù)需要根據(jù)條件使用動(dòng)態(tài)查詢,這時(shí)候需要使用動(dòng)態(tài)sql,通常我們會(huì)自己寫動(dòng)態(tài)sql來(lái)實(shí)現(xiàn),比如:

<select id="findStudentByCondition" resultType="com.example.service.entity.Student">
    SELECT id, name, score FROM tbl_student
    <where>
      <if test="score !=null and score > 0">
        score > #{score}
      </if>
      <if test="name !=null and name != ''">
         <bind name="pattern" value=" '%' + name + '%' "/>
        AND name LIKE #{pattern}
      </if>
    </where>
    ORDER BY score DESc
  </select>

??????? 這個(gè)sql是查詢成績(jī)大于90并且名字包含“i”的學(xué)生信息。但是有時(shí)候又加了一個(gè)條件,又要去改sql,改入?yún)?,有沒(méi)有一種方式可以將寫動(dòng)態(tài)sql像寫代碼一樣實(shí)現(xiàn)呢?如果你有這個(gè)想法,推薦你了解一下Tk.mybatis。

????? 關(guān)于Tk.mybatis的介紹以及如何配置,本文暫不介紹,不了解的請(qǐng)自行百度,本文只介紹如何基于tk.mybatis實(shí)現(xiàn)不寫sql語(yǔ)句也能實(shí)現(xiàn)動(dòng)態(tài)sql查詢。

實(shí)現(xiàn)方式:

1. 使用Example實(shí)現(xiàn)

2. 使用Example.createCriteria

3. 使用Example.builder實(shí)現(xiàn)

4. 使用WeekendSqls實(shí)現(xiàn)

方式一:使用Example實(shí)現(xiàn)

/**
   * 第一種:使用example查詢
   */
  @Test
  public void testSelectByExample() {
    Example example = new Example(Student.class);
    // 設(shè)置查詢列
    example.selectProperties("id","name","score");
    // 動(dòng)態(tài)sql
    example.and()
        .andGreaterThan("score",90)
        .andLike("name", "%i%");
    // 去重
    example.setDistinct(true);
    // 排序
    example.orderBy("score").desc();
    List<Student> students = studentMapper.selectByExample(example);
    System.out.println(students);
  }

方式二:使用example.createCriteria實(shí)現(xiàn)

/**
   * 第二種:使用example.createCriteria查詢
   */
  @Test
  public void testSelectByExampleCriteria() {
    Example example = new Example(Student.class);
    // 設(shè)置查詢列
    example.selectProperties("id","name","score");
    // 動(dòng)態(tài)查詢
    example.createCriteria()
        .andGreaterThan("score",90)
        .andLike("name", "%i%");
    // 去重
    example.setDistinct(true);
    // 排序
    example.orderBy("score").desc();
    List<Student> students = studentMapper.selectByExample(example);
    System.out.println(students);
  }

方式三:使用Example.builder實(shí)現(xiàn)

/**
   * 第三種:使用Example.builder實(shí)現(xiàn)
   */
  @Test
  public void testSelectByExampleBuilder() {
    Example example = Example.builder(Student.class)
        // 查詢列
        .select("id","name","score")
        // 動(dòng)態(tài)sql
        .where(Sqls.custom()
            .andGreaterThan("score",90)
            .andLike("name","%i%"))
        // 去重
        .distinct()
        // 排序
        .orderByDesc("score")
        .build();
    List<Student> students = studentMapper.selectByExample(example);
    System.out.println(students);
  }

方式四:使用weekendSqls實(shí)現(xiàn)

/**
   * 第四種:使用weekendSqls實(shí)現(xiàn)
   */
  @Test
  public void testSelectByWeekendSqls() {
    WeekendSqls<Student> sqls = WeekendSqls.custom();
    sqls = sqls
        .andGreaterThan(Student::getScore,90)
        .andLike(Student::getName,"%i%");
    List<Student> sysRoles = studentMapper.selectByExample(Example.builder(Student.class)
        .select("id","name","score")
        .where(sqls)
        .distinct()
        .orderByDesc("score")
        .build());
    System.out.println(sysRoles);
 
  }

?到此這篇關(guān)于Tk.mybatis零sql語(yǔ)句實(shí)現(xiàn)動(dòng)態(tài)sql查詢的方法(4種)的文章就介紹到這了,更多相關(guān)Tk.mybatis 動(dòng)態(tài)sql查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 關(guān)于dubbo的超時(shí)處理及重試原則

    關(guān)于dubbo的超時(shí)處理及重試原則

    這篇文章主要介紹了關(guān)于dubbo的超時(shí)處理及重試原則,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Java開(kāi)發(fā)利器之Guava?Cache的使用教程

    Java開(kāi)發(fā)利器之Guava?Cache的使用教程

    緩存技術(shù)被認(rèn)為是減輕服務(wù)器負(fù)載、降低網(wǎng)絡(luò)擁塞、增強(qiáng)Web可擴(kuò)展性的有效途徑之一。今天咱們就來(lái)聊聊Guava?Cache本地緩存,感興趣的可以了解一下
    2022-09-09
  • java連接ElasticSearch集群操作

    java連接ElasticSearch集群操作

    這篇文章主要介紹了java連接ElasticSearch集群操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09
  • java SpringSecurity使用詳解

    java SpringSecurity使用詳解

    這篇文章主要介紹了java中Spring Security的實(shí)例詳解的相關(guān)資料,spring security是一個(gè)多方面的安全認(rèn)證框架,提供了基于JavaEE規(guī)范的完整的安全認(rèn)證解決方案,需要的朋友可以參考下
    2021-08-08
  • SpringMVC 文件上傳配置,多文件上傳,使用的MultipartFile的實(shí)例

    SpringMVC 文件上傳配置,多文件上傳,使用的MultipartFile的實(shí)例

    本篇文章主要介紹了SpringMVC 文件上傳配置,詳解介紹了如何使用SpringMVC進(jìn)行表單上的文件上傳以及多個(gè)文件同時(shí)上傳的步驟,有興趣的可以了解一下。
    2016-12-12
  • Java中redisTemplate注入失敗NullPointerException異常問(wèn)題解決

    Java中redisTemplate注入失敗NullPointerException異常問(wèn)題解決

    這篇文章主要介紹了Java中redisTemplate注入失敗NullPointerException異常問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2023-08-08
  • Spring中循環(huán)依賴的解決方法詳析

    Spring中循環(huán)依賴的解決方法詳析

    這篇文章主要給大家介紹了關(guān)于Spring中循環(huán)依賴的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • 詳解如何在Spring中為@Value注解設(shè)置默認(rèn)值

    詳解如何在Spring中為@Value注解設(shè)置默認(rèn)值

    在Spring開(kāi)發(fā)中,我們經(jīng)常會(huì)遇到需要從配置文件中讀取屬性的情況,@Value注解是Spring提供的一種便捷方式,能夠讓我們輕松地將配置文件中的屬性注入到Spring Bean中,
    2024-10-10
  • Java基于狀態(tài)模式實(shí)現(xiàn)的文檔編輯模式切換功能實(shí)例

    Java基于狀態(tài)模式實(shí)現(xiàn)的文檔編輯模式切換功能實(shí)例

    這篇文章主要介紹了Java基于狀態(tài)模式實(shí)現(xiàn)的文檔編輯模式切換功能,結(jié)合實(shí)例形式詳細(xì)分析了狀態(tài)模式的概念、原理及java使用狀態(tài)模式實(shí)現(xiàn)文檔編輯模式切換操作相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下
    2018-05-05
  • Java弱鍵集合WeakHashMap及ConcurrentCache原理詳解

    Java弱鍵集合WeakHashMap及ConcurrentCache原理詳解

    這篇文章主要介紹了Java弱鍵集合WeakHashMap及ConcurrentCache原理詳解,基于哈希表的Map接口實(shí)現(xiàn),支持null鍵和值,但是WeakHashMap具有弱鍵,可用來(lái)實(shí)現(xiàn)緩存存儲(chǔ),在進(jìn)行GC的時(shí)候會(huì)自動(dòng)回收鍵值對(duì),需要的朋友可以參考下
    2023-09-09

最新評(píng)論

元江| 石景山区| 塘沽区| 莲花县| 温州市| 石景山区| 曲靖市| 白城市| 诸城市| 永宁县| 固阳县| 永康市| 济宁市| 乡城县| 黄平县| 壶关县| 巴楚县| 景宁| 乾安县| 榆社县| 伽师县| 汕尾市| 嘉鱼县| 塔河县| 沙田区| 博乐市| 张家界市| 石台县| 门源| 益阳市| 沙河市| 华容县| 吉木乃县| 松原市| 夏河县| 广南县| 肇庆市| 门头沟区| 云和县| 江北区| 乌拉特中旗|