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

Tk.mybatis零sql語句實現(xiàn)動態(tài)sql查詢的方法(4種)

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

有時候,查詢數(shù)據(jù)需要根據(jù)條件使用動態(tài)查詢,這時候需要使用動態(tài)sql,通常我們會自己寫動態(tài)sql來實現(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>

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

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

實現(xiàn)方式:

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

2. 使用Example.createCriteria

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

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

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

/**
   * 第一種:使用example查詢
   */
  @Test
  public void testSelectByExample() {
    Example example = new Example(Student.class);
    // 設(shè)置查詢列
    example.selectProperties("id","name","score");
    // 動態(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實現(xiàn)

/**
   * 第二種:使用example.createCriteria查詢
   */
  @Test
  public void testSelectByExampleCriteria() {
    Example example = new Example(Student.class);
    // 設(shè)置查詢列
    example.selectProperties("id","name","score");
    // 動態(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實現(xiàn)

/**
   * 第三種:使用Example.builder實現(xiàn)
   */
  @Test
  public void testSelectByExampleBuilder() {
    Example example = Example.builder(Student.class)
        // 查詢列
        .select("id","name","score")
        // 動態(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實現(xiàn)

/**
   * 第四種:使用weekendSqls實現(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語句實現(xiàn)動態(tài)sql查詢的方法(4種)的文章就介紹到這了,更多相關(guān)Tk.mybatis 動態(tài)sql查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

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

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

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

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

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

    java連接ElasticSearch集群操作

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

    java SpringSecurity使用詳解

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

    SpringMVC 文件上傳配置,多文件上傳,使用的MultipartFile的實例

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

    Java中redisTemplate注入失敗NullPointerException異常問題解決

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

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

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

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

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

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

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

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

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

最新評論

阿坝县| 武强县| 赤壁市| 瑞昌市| 延川县| 平遥县| 平顺县| 文安县| 海门市| 阜新市| 崇明县| 崇文区| 监利县| 三台县| 江门市| 沂南县| 福鼎市| 菏泽市| 金溪县| 庄河市| 陇川县| 万载县| 灵石县| 天峻县| 铜川市| 南江县| 富裕县| 雅江县| 洮南市| 嘉峪关市| 晋城| 甘洛县| 原平市| 广元市| 迁西县| 防城港市| 辽阳市| 大足县| 视频| 长丰县| 平凉市|