Be alaways javaing...

Loving Java
posts - 43, comments - 5, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

OfBiz中的数据操作

Posted on 2008-09-08 17:59 追风舞者 阅读(1221) 评论(0)  编辑  收藏 所属分类: OfBiz
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/



只有注册用户登录后才能发表评论。


网站导航: