JFinal Online Manual
/ 復(fù)合主鍵
復(fù)合主鍵
JFinal ActiveRecord 從 2.0 版本開(kāi)始,采用極簡(jiǎn)設(shè)計(jì)支持復(fù)合主鍵,對(duì)于 Model 來(lái)說(shuō)需要 在映射時(shí)指定復(fù)合主鍵名稱,以下是具體例子:
ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
// 多數(shù)據(jù)源的配置僅僅是如下第二個(gè)參數(shù)指定一次復(fù)合主鍵名稱
arp.addMapping("user_role", "userId, roleId", UserRole.class);
//同時(shí)指定復(fù)合主鍵值即可查找記錄 UserRole.dao.findById(123, 456);
//同時(shí)指定復(fù)合主鍵值即可刪除記錄 UserRole.dao.deleteById(123, 456);
// 多數(shù)據(jù)源的配置僅僅是如下第二個(gè)參數(shù)指定一次復(fù)合主鍵名稱
arp.addMapping("user_role", "userId, roleId", UserRole.class);
//同時(shí)指定復(fù)合主鍵值即可查找記錄 UserRole.dao.findById(123, 456);
//同時(shí)指定復(fù)合主鍵值即可刪除記錄 UserRole.dao.deleteById(123, 456);
如上代碼所示,對(duì)于 Model 來(lái)說(shuō),只需要在添加 Model 映射時(shí)指定復(fù)合主鍵名稱即可開(kāi) 始使用復(fù)合主鍵,在后續(xù)的操作中 JFinal 會(huì)對(duì)復(fù)合主鍵支持的個(gè)數(shù)進(jìn)行檢測(cè),當(dāng)復(fù)合主鍵數(shù)量 不正確時(shí)會(huì)報(bào)異常,尤其是復(fù)合主鍵數(shù)量不夠時(shí)能夠確保數(shù)據(jù)安全。復(fù)合主鍵不限定只能有兩 個(gè),可以是數(shù)據(jù)庫(kù)支持下的任意多個(gè)。
對(duì)于 Db + Record 模式來(lái)說(shuō),復(fù)合主鍵的使用不需要配置,直接用即可:
Db.findById("user_role", "roleId, userId", 123, 456);
Db.deleteById("user_role", "roleId, userId", 123, 456);
Db.deleteById("user_role", "roleId, userId", 123, 456);