溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何理解weed3-4.1開始注解sql的使用

發(fā)布時間:2021-10-11 13:44:08 來源:億速云 閱讀:116 作者:柒染 欄目:大數(shù)據(jù)

如何理解weed3-4.1開始注解sql的使用,針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

Weed3 一個微型ORM框架(只有0.1Mb哦)

源碼:https://github.com/noear/weed3 源碼:https://gitee.com/noear/weed3

使用約定***
  • 1.項目開啟編譯參數(shù):-parameters

先來一個demo
//申明mapper
public interface DbMapper1{
    @Sql("select * from appx where app_id = @{app_id} limit 1")
    AppxModel appx_get(int app_id) throws Exception;
}

//使用mapper
DbMapper1 dm = db.mapper(DbMapper1.class);
AppxModel m = dm.appx_get(1);
然后復(fù)雜一點點demo1
  • 1.申明一個mapper

//加了替換符
//加了緩存控制
public interface DbMapper1{
    @Sql(value = "select * from ${tb} where app_id = @{app_id} limit 1", 
         caching = "test", 
         cacheTag = "app_${app_id}")
    AppxModel appx_get(String tb, int app_id) throws Exception;
}
  • 2.使用它

DbContext db = new DbContext(...);

DbMapper1 dm = db.mapper(DbMapper1.class);
AppxModel m = dm.appx_get("appx",1);
兩種變量形式 + 緩存控制

兩種變量形式

  • ${} 替代變量(相當(dāng)于占位符,進行字符串拼接)

  • @{} 編譯變量(會編譯為?,通過變量傳遞給jdbc)

緩存控制

  • caching 緩存服務(wù)

  • cacheTag 緩存標(biāo)簽(在key之上,建立的虛擬tag;為便于清理)

  • usingCache 緩存使用時間

  • cacheClear 緩存清理(通過cacheTag形式清理)

再來一個demo2

更新之后,清掉緩存:app_${app_id}

public interface DbMapper2{
    @Sql(value = "update appx set name=@{name} where app_id = @{app_id}", 
         caching = "test", 
         cacheClear = "app_${app_id}")
    void appx_set(int app_id, String name) throws Exception;
}
再來一個demo3

使用查詢結(jié)果構(gòu)建cahce tag:app_type${type}

public interface DbMapper3{
    @Sql(value = "select * from appx where app_id = @{app_id} limit 1", 
         caching = "test", 
         cacheTag = "app_${app_id},app_type${type}")
    AppxModel appx_get(int app_id) throws Exception;
}
補充:構(gòu)建一個緩存服務(wù)
//隨便寫在哪里

//1.初始化一個ICacheServiceEx
//2.通過nameSet("test") 注冊到緩存庫
//3.之后就可以被 @sql的 caching 使用(xml sql 的 caching 同樣如此)
//
new LocalCache("test",60).nameSet("test");

關(guān)于如何理解weed3-4.1開始注解sql的使用問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI