Ofbiz架构讲解与讨论
http://wenku.baidu.com/view/b631f311f18583d049645903
1. 创建记录
Map planetValues = UtilMisc.toMap("planetId", "JUPITER", " planetName", "Jupiter");
GenericValue planetGV = delegator.makeValue("Planet", planetValues); planetGV.create();
UtilMisc是集合类操作的工具类,ofbiz优化了map,list,set。 GenericValue 对象是对应数据库中一条记录。
以上代码等价于
INSERT INTO Planet (planetId, planetName) VALUES ("JUPITER", "Jupiter").
2. 更新记录
Map planetValues = UtilMisc.toMap("planetId", "JUPITER");
GenericValue planetGV = delegator.findByPrimaryKey("Planet",planetValues); planetGV.put("planetName", "Joopiter"); planetGV.store();
以上代码等价于
UPDATE Planet SET planetName = "Joopiter" WHERE planetId = "JUPITER"
3. 删除记录
Map planetValues = UtilMisc.toMap("planetId", "JUPITER");
GenericValue planetGV = delegator.findByPrimaryKey("Planet", planetValues); planetGV.remove();
以上代码等价于
DELETE FROM Planet WHERE planetId = "JUPITER"
4. 查询记录
condition = new EntityExpr("planetId", EntityOperator.EQUALS, "EARTH"); data = delegator.findByCondition("Planet", condition, null, null);
以上代码等价于
SELECT * FROM Planet WHERE planetId = "EARTH" 其中
EntityOperator 为ofbiz逻辑操作符号类。
EQUALS NOT_EQUAL LESS_THAN GREATER_THAN
LESS_THAN_EQUAL_TO (less than or equal to) GREATER_THAN_EQUAL_TO IN NOT_IN BETWEEN LIKE NOT_LIKE NOT
findByCondition(entityName,condition,filedList,orderList)方法的四个参数。
entityName:查询的实体名
condition :查询条件
filedList :实体属性列表
orderList : 属性排序列表
fields = UtilMisc.toList("planetId", "planetName");
data = delegator.findByCondition("Planet", condition, fields, null);
以上代码等价于
SELECT plantetId,name FROM Planet
fields = UtilMisc.toList("planetId DESC"); //fields = UtilMisc.toList("-planetId ");
以上代码等价于
SELECT * FROM Planet ORDER BY planetId DESC
模糊查询
condition = new EntityExpr("planetName", EntityOperator.LIKE, "%AR%"); data = delegator.findByCondition("Planet", condition, null, null);
以上代码等价于
SELECT * FROM Planet WHERE planetName LIKE "%ar%"
复合条件查询
conditionList = UtilMisc.toList
(new EntityExpr("planetName", EntityOperator.LIKE, "%ar%"), new EntityExpr("planetName", EntityOperator.LIKE, "%m%")); condition1 = new EntityConditionList(conditionList,
EntityOperator.OR); condition2 = new EntityConditionList
(UtilMisc.toList(new EntityExpr("planetName", EntityOperator.LIKE, "%s%"), new EntityExpr("planetName", EntityOperator.LIKE, "%P%")), EntityOperator.OR);
combined = new EntityConditionList(UtilMisc.toList(condition1, condition2), EntityOperator.AND); data = delegator.findByCondition("Planet", combined, null, null);
以上代码等价于 SELECT * FROM
Planet
WHERE
(planetName LIKE "%ar%" OR planetName LIKE "%t%") AND
(planetName LIKE "%s%" OR planetName LIKE "%P%")
Accessing the Entities and View Entities
http://blog.csdn.net/ztk3939339/article/details/8781483
无条件遍历
使用如下代码即可:
findList(EntityName, null, null, null, null,false);
条件遍历
需要先了解如下概念:
条件的表达
EntityWhereString - SQL语句描述,OFBiz的开发者强烈不建议使用这个,因为破坏了OFBiz的封装性
EntityExpr - 使用OFBiz封装好的操作符来描述一个条件,推荐使用,简单易懂。EntityExpr = 左值 + operator + 右值。我们使用EntityCondition.makeCondition()来生成EntityExpr
EntityOperator - 定义了条件中的操作符
EQUALS,NOT_EQUAL,LESS_THAN,GREATER_THAN,LESS_THAN_EQUAL_TO (less than or equal to),GREATER_THAN_EQUAL_TO - 用于数值的比较
condition = EntityCondition.makeCondition("planetId", EntityOperator.NOT_EQUAL,"EARTH");
IN,NOT_IN - 规定遍历的结果的某个属性值需要在/不在某个集合中
condition = EntityCondition.makeCondition("planetId", EntityOperator.IN, UtilMisc.toList("EARTH", "PLUTO"));
LIKE,NOT_LIKE - 用于字符串匹配
condition = new EntityExpr("planetName", EntityOperator.LIKE, "%AR%");
这里的意思是找出名字里面还有AR的所有planet
% - any number of characters
_ - any one character
EntityConditionList - 由多个EntityExpr连接来。 EntityConditionList = list_of(condition1,condition2,...) + EntityJoinOperator
conditionList = UtilMisc.toList(EntityCondition.makeCondition("planetName", EntityOperator.LIKE, "%ar%"),
EntityCondition.makeCondition("planetName", EntityOperator.LIKE, "%t%"));
conditions = EntityCondition.makeCondition(conditionList, EntityOperator.AND);
EntityJoinOperator - 两个值AND和OR。其实它是继承EntityOperator得来的,所以使用EntityOperator.AND和EntityOperator.OR也行
public List<GenericValue> findList(String entityName, EntityCondition entityCondition, Set<String> fieldsToSelect, List<String> orderBy, EntityFindOptions findOptions, boolean useCache) - 返回查找的列表
entityName
entityCondition - 查找的条件,可以为EntityExpr,也可以为EntityConditionList,因为他们都是继承于EntityCondition的
fieldsToSelect - 结果中需要显示field,即选择要显示的列
orderBy - 结果的排序,排序的规则是:orders the retrieved records by those fields. If there is more than one field in the list, ordering is performed by the first field, then the second, and so on。默认是升序排列。如果要降序排列,可以这样:
order = UtilMisc.toList("planetId DESC");
注意:此处planetId和DESC之间的空格有且只能有一个,多了少了都会出错,其他地方有多少个空格都无所谓
或者
order = UtilMisc.toList("-planetId");
注意:-和planetId之间不能有空格,否则出错,其他地方空格随意
findOptions - 不常用,设为null
useCache - 不常用,设为false
单一条件查找
import org.ofbiz.base.util.*;
import org.ofbiz.entity.*;
import org.ofbiz.entity.condition.*;
condition = EntityCondition.makeCondition("planetId", EntityOperator.IN,UtilMisc.toList("EARTH", "PLUTO"));
data = delegator.findList("Planet", condition, null, null, null,false);
多条件查找
import org.ofbiz.base.util.*;
import org.ofbiz.entity.*;
import org.ofbiz.entity.condition.*;
conditionList = UtilMisc.toList(EntityCondition.makeCondition("planetName", EntityOperator.LIKE, "%ar%"),
EntityCondition.makeCondition("planetName", EntityOperator.LIKE, "%t%"));
conditions = EntityCondition.makeCondition(conditionList, EntityOperator.AND);
data = delegator.findList("Planet", conditions, null, null, null,false);
查找结果排序
import org.ofbiz.base.util.*;
order = UtilMisc.toList("planetId DESC");
data = delegator.findList("Planet", null, null, order, null, false);
选择要结果中Field
import org.ofbiz.base.util.*;
fields = UtilMisc.toSet("planetId", "planetName");
data = delegator.findList("Planet", null, fields, null, null, false);
分页
int listSize = 0;
int lowIndex = 0;
int highIndex = 0;
int viewSize = 10;
lowIndex = (Integer.parseInt(pagenumber) - 1) * viewSize + 1;
highIndex = Integer.parseInt(pagenumber) * viewSize;
List<GenericValue> list = null;
String entityName = "SandbagUserTotalScoreProgramView";
TransactionUtil.begin();
EntityListIterator pli = null;
Set<String> fieldToSet = FastSet.newInstance();
List<String> orderBy = FastList.newInstance();
orderBy.add("-score");
EntityFindOptions findOpts = new EntityFindOptions();
findOpts.setResultSetType(EntityFindOptions.TYPE_SCROLL_INSENSITIVE);
findOpts.setMaxRows(15);
List<EntityCondition> mainCondList = FastList.newInstance();
if (UtilValidate.isNotEmpty(traineeId)) {
mainCondList.add(EntityCondition.makeCondition("traineeId", EntityOperator.EQUALS, traineeId));
}
mainCondList.add(EntityCondition.makeCondition("fromType", EntityOperator.EQUALS, "program"));
EntityCondition mainCond = EntityCondition.makeCondition(mainCondList, EntityOperator.AND);
pli = delegator.find(entityName, mainCond, null, fieldToSet, orderBy, findOpts);
if (lowIndex > 0 && viewSize > 0) {
list = pli.getPartialList(lowIndex, viewSize);
} else {
list = pli.getCompleteList();
}
listSize = pli.getResultsSizeAfterPartialList();
if (highIndex > listSize) {
highIndex = listSize;
}
TransactionUtil.commit();
success = "1";
resultObj.put("rows", viewSize);
resultObj.put("total", listSize);
resultObj.put("pagecount", listSize % viewSize > 0 ? listSize / viewSize + 1 : listSize / viewSize);
resultObj.put("data", parseViewDatList(list));
resultObj.put("success", success);