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);
兩種變量形式 + 緩存控制
兩種變量形式
-
${}
替代變量(相當于占位符酷宵,進行字符串拼接) -
@{}
編譯變量(會編譯為?耀销,通過變量傳遞給jdbc)
緩存控制
-
caching
緩存服務(wù) -
cacheTag
緩存標簽(在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");