ofbiz--操作数据库

时间:2021-10-12 13:06:27

与数据库的操作都是通过Delegator进行的。

查询

  • 查询单条数据
Delegator delegator = (Delegator) request.getAttribute("delegator");
GenericValue terminalTypeRecord = EntityQuery.use(delegator).from("VehicleTerminatorType").where(UtilMisc.toMap("id", typeId)).queryOne();
  • 查询多条数据
Delegator delegator = (Delegator) request.getAttribute("delegator");
List<GenericValue> terminalTypeRecord = EntityQuery.use(delegator).from("VehicleTerminatorType").where(UtilMisc.toMap("id", typeId)).queryList();
  • 构造复杂一点的查询条件
List<EntityCondition> where = FastList.newInstance();     
where.add(EntityCondition.makeCondition("name", EntityOperator.LIKE, "%" + "abc"));
EntityListIterator eli = null;
EntityFindOptions efo = new EntityFindOptions(true, EntityFindOptions.TYPE_SCROLL_SENSITIVE, EntityFindOptions.CONCUR_READ_ONLY, false);
eli = delegator.find("user", EntityCondition.makeCondition(where), null, null, null, efo);
List<GenericValue> result = eli.getCompleteList();

增加

Delegator delegator = (Delegator) request.getAttribute("delegator");
Map<String, Object> dataMap = FastMap.newInstance();
dataMap.put("name", terminalType);
dataMap.put("enableSms", sms);
dataMap.put("enableGprs", gprs);
GenericValue terminalTypeInfo = delegator.makeValidValue("VehicleTerminatorType", dataMap);
delegator.createSetNextSeqId(terminalTypeInfo); // 插入自增长的id

删除

  • 删除单条记录
delegator.removeByCondition("VehicleType", EntityCondition.makeCondition(UtilMisc.toMap("id", typeId)));
  • 删除列表
List<EntityCondition> conditionList = FastList.newInstance();
conditionList.add(EntityCondition.makeCondition("id", EntityOperator.IN, lkList));
delegator.removeByCondition("VehicleType", EntityCondition.makeCondition(conditionList));

修改

GenericValue terminalTypeRecord = EntityQuery.use(delegator).from("VehicleTerminatorType").where(UtilMisc.toMap("id", typeId)).queryOne();
terminalTypeRecord.put("name", terminalType);
terminalTypeRecord.put("enableSms", sms);
terminalTypeRecord.put("enableGprs", gprs);
delegator.store(terminalTypeRecord);