1.OfBiz中利用delegator訪問數據庫
1)創建GenericDelegator對象
1.1 service中
public static Map createHelloPerson(DispatchContext dctx, Map context) {
GenericDelegator delegator = dctx.getDelegator();
...
}
1.2手工建立GenericDelegator對象
GenericDelegator delegator = GenericDelegator.getGenericDelegator("default");
2)Insert a record
通過調用delegator對象的getNextSeqId(), makeValue(String entityName, Map fields)和create()方法。
例如:
String helloPersonId = delegator.getNextSeqId("HelloPerson");
GenericValue helloPerson = delegator.makeValue("HelloPerson",
UtilMisc.toMap("helloPersonId", helloPersonId)); // create a GenericValue from ID we just got
helloPerson.setNonPKFields(context); // move non-primary key fields from input parameters to GenericValue
delegator.create(helloPerson); // store the generic value, ie persists it
3)Remove a record
delegator.removeByAnd(String entityName, Map fields);
4)Strore a record
例如:
Map pk = UtilMisc.toMap("attribute1", attribute1Value, "attribute2", attribute2Value);//pk中存儲了查詢條件
GenericValue obj = delegator.findByPrimaryKey("ClassName", pk);
obj.setNonPKFields(context);//contex為Map類型,存儲了要更新的字段
obj.store();
5)Store records
List resultList = delegator.findAll(String entityName, List orderBy);
List toStore = new ArrayList();
toStore.addAll(resultList);
delegator.storeAll(toStore);
6)Look for record/records
findByAnd,findByCondition、findByLike、findByOr、findByPrimaryKey、
findListIteratorByCondition、 findall、findAllByPrimaryKeys
2) 依據數值對象進行訪問
在現有的數值對象(GenericValue類型)上可以進行下列操作:
根據關系查找關聯信息getRelated,包括getRelated、getRelatedByAnd、getRelatedDummyPK、getRelatedMulti、getRelatedOrderBy。
刷新本數值對象refresh
保存本數值對象store,主要用于修改后的保存
刪除數值對象remove,包括刪除本數值對象remove和刪除某個關聯的數值對象removeRelated
在現有數值對象上的操作是通過調用
更加具體的信息可參考:http://www.opentaps.org/javadocs/release-1.0.1/framework/api/
1)創建GenericDelegator對象
1.1 service中
public static Map createHelloPerson(DispatchContext dctx, Map context) {
GenericDelegator delegator = dctx.getDelegator();
...
}
1.2手工建立GenericDelegator對象
GenericDelegator delegator = GenericDelegator.getGenericDelegator("default");
2)Insert a record
通過調用delegator對象的getNextSeqId(), makeValue(String entityName, Map fields)和create()方法。
例如:
String helloPersonId = delegator.getNextSeqId("HelloPerson");
GenericValue helloPerson = delegator.makeValue("HelloPerson",
UtilMisc.toMap("helloPersonId", helloPersonId)); // create a GenericValue from ID we just got
helloPerson.setNonPKFields(context); // move non-primary key fields from input parameters to GenericValue
delegator.create(helloPerson); // store the generic value, ie persists it
3)Remove a record
delegator.removeByAnd(String entityName, Map fields);
4)Strore a record
例如:
Map pk = UtilMisc.toMap("attribute1", attribute1Value, "attribute2", attribute2Value);//pk中存儲了查詢條件
GenericValue obj = delegator.findByPrimaryKey("ClassName", pk);
obj.setNonPKFields(context);//contex為Map類型,存儲了要更新的字段
obj.store();
5)Store records
List resultList = delegator.findAll(String entityName, List orderBy);
List toStore = new ArrayList();
toStore.addAll(resultList);
delegator.storeAll(toStore);
6)Look for record/records
findByAnd,findByCondition、findByLike、findByOr、findByPrimaryKey、
findListIteratorByCondition、 findall、findAllByPrimaryKeys
2) 依據數值對象進行訪問
在現有的數值對象(GenericValue類型)上可以進行下列操作:
根據關系查找關聯信息getRelated,包括getRelated、getRelatedByAnd、getRelatedDummyPK、getRelatedMulti、getRelatedOrderBy。
刷新本數值對象refresh
保存本數值對象store,主要用于修改后的保存
刪除數值對象remove,包括刪除本數值對象remove和刪除某個關聯的數值對象removeRelated
在現有數值對象上的操作是通過調用
更加具體的信息可參考:http://www.opentaps.org/javadocs/release-1.0.1/framework/api/