Setting-Up Our Playground
由于教程是以OFBiz Release 4.0为基准的,而我们的项目是决定用OFBiz 10.04.02的,存在一定的区别
在试运行test.bsh的时候,我遇到了如下问题:
ERROR rendering error page [/error/error.jsp], but here is the error text: org.ofbiz.widget.screen.ScreenRenderException: Error rendering screen [component://learning/widget/learning/LearningScreens.xml#ProcessEntityAccessBSF]: org.ofbiz.base.util.GeneralException: Error running BSH script at location [component://learning/webapp/learning/WEB-INF/actions/entityaccess/processEntityAccessBSF.bsh] (Error running BSH script at [component://learning/webapp/learning/WEB-INF/actions/entityaccess/processEntityAccessBSF.bsh], line [18]: Sourced file: component://learning/webapp/learning/WEB-INF/actions/entityaccess/processEntityAccessBSF.bsh : Method Invocation this.interpreter.eval : at Line: 18 : in file: /bsh/commands/source.bsh : this .interpreter .eval ( new InputStreamReader ( url .openStream ( ) ) , this .caller .namespace , "URL: " + url .toString ( ) ) Called from method: source : at Line: 20 : in file: component://learning/webapp/learning/WEB-INF/actions/entityaccess/processEntityAccessBSF.bsh : source ( location ) Target exception: Sourced file: URL: file:/E:/apache-ofbiz-10.04.02/hot-deploy/learning/webapp/learning/WEB-INF/actions/entityaccess/test.bsh : Error in method invocation: Method findAll( java.lang.String ) not found in class'org.ofbiz.entity.GenericDelegator' : at Line: 4 : in file: URL: file:/E:/apache-ofbiz-10.04.02/hot-deploy/learning/webapp/learning/WEB-INF/actions/entityaccess/test.bsh : delegator .findAll ( "PostalAddress" ) (Sourced file: component://learning/webapp/learning/WEB-INF/actions/entityaccess/processEntityAccessBSF.bsh : Method Invocation this.interpreter.eval)) (Error running BSH script at location [component://learning/webapp/learning/WEB-INF/actions/entityaccess/processEntityAccessBSF.bsh] (Error running BSH script at [component://learning/webapp/learning/WEB-INF/actions/entityaccess/processEntityAccessBSF.bsh], line [18]: Sourced file: component://learning/webapp/learning/WEB-INF/actions/entityaccess/processEntityAccessBSF.bsh : Method Invocation this.interpreter.eval : at Line: 18 : in file: /bsh/commands/source.bsh : this .interpreter .eval ( new InputStreamReader ( url .openStream ( ) ) , this .caller .namespace , "URL: " + url .toString ( ) ) Called from method: source : at Line: 20 : in file: component://learning/webapp/learning/WEB-INF/actions/entityaccess/processEntityAccessBSF.bsh : source ( location ) Target exception: Sourced file: URL: file:/E:/apache-ofbiz-10.04.02/hot-deploy/learning/webapp/learning/WEB-INF/actions/entityaccess/test.bsh : Error in method invocation: Method findAll( java.lang.String ) not found in class'org.ofbiz.entity.GenericDelegator' : at Line: 4 : in file: URL: file:/E:/apache-ofbiz-10.04.02/hot-deploy/learning/webapp/learning/WEB-INF/actions/entityaccess/test.bsh : delegator .findAll ( "PostalAddress" ) (Sourced file: component://learning/webapp/learning/WEB-INF/actions/entityaccess/processEntityAccessBSF.bsh : Method Invocation this.interpreter.eval)))
表示眼睛花了,从后往前看,有一句值得注意:
Error in method invocation: Method findAll( java.lang.String ) not found in class'org.ofbiz.entity.GenericDelegator'
我就想会不会是10.04.02版本的delegator没有提供findAll这个方法呢
![](https://img-my.csdn.net/uploads/201304/10/1365561554_7740.png)
果然没有,4.0倒是有
![](https://img-my.csdn.net/uploads/201304/10/1365561557_2437.png)
findAll的代码如下:
![](https://img-my.csdn.net/uploads/201304/10/1365561562_7357.png)
所以仿照这个,我将test.bsh改为
![](https://img-my.csdn.net/uploads/201304/10/1365561565_7878.png)
运行,成功!
这是应急的方法,后面文档中提到:
For those keeping up to date with the latest development trunk, two important lookup methods have changed.
- The method findByPrimaryKey has been replaced with the new findOne method
- the findAll method has been replaced with findList .
具体的使用方法详见后面查看数据的部分
Creating a Database Record
OFBiz使用Javolution的FastMap和FastList,来代替原有的Map和List,效率更高,更方便使用
关于Javolution的API可以查阅网址:http://javolution.org/target/site/apidocs/index.html
示例代码:
import org.ofbiz.entity.*;
import org.ofbiz.base.util.*;
Map planetValues = UtilMisc.toMap("planetId", "JUPITER", "planetName", "Jupiter");
GenericValue planetGV = delegator.makeValue("Planet", planetValues);
planetGV.create();
planetValues = UtilMisc.toMap("planetId", "EARTH", "planetName", "Earth");
planetGV = delegator.makeValue("Planet", planetValues);
planetGV.create();
planetValues = UtilMisc.toMap("planetId", "MARS", "planetName", "Mars");
planetGV = delegator.makeValue("Planet", planetValues);
planetGV.create();
- UitlMisc.toMap - Create a map from passed nameX, valueX parameters
- delegator.makeValue - Creates a GenericValue object that represents a data record in memory
- planetGV.create() - 写入到数据库中
![](https://img-my.csdn.net/uploads/201304/10/1365561578_5942.png)
Updating Database Records
import org.ofbiz.entity.*;
import org.ofbiz.base.util.*;
Map planetValues = UtilMisc.toMap("planetId", "JUPITER");
GenericValue planetGV = delegator.findByPrimaryKey("Planet", planetValues);
planetGV.put("planetName", "Joopiter");
planetGV.store();
- findByPrimaryKey
java.lang.String - Entity的名字
java.util.Map - 查找的条件 - planetGV.put() -修改
- planetGV.store() - 存入数据库
![](https://img-my.csdn.net/uploads/201304/10/1365561583_3137.png)
注意,虽然目前OFBiz 10.04.02的GenericDelegator仍然有findByPrimaryKey方法,以后很可能用findOne来代替
Deleting Database Records
代码如下:
import org.ofbiz.entity.*;
import org.ofbiz.base.util.*;
Map planetValues = UtilMisc.toMap("planetId", "JUPITER");
GenericValue planetGV = delegator.findByPrimaryKey("Planet", planetValues);
planetGV.remove();
Retrieving Database Records
Retrieving Database Records
OFBiz Release 4.0到OFBiz 10.04.02的一些变动
- findAll(EntityName) —— findList(EntityName, null, null, null, null,false);
- findByCondition(entityName, entityCondition, fieldsToSelect, orderBy) —— findList(entityName, entityCondition, fieldsToSelect, orderBy, entityFindOptions, useCache)
- new EntityExpr(lhs, operator, rhs) —— EntityCondition.makeCondition(lhs, operator, rhs)
- new EntityConditionList(conditionList, entityJoinOperator) —— EntityCondition.makeCondition(java.util.List<T> conditionList, entityJoinOperator)
无条件遍历
使用如下代码即可: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 - 用于字符串匹配
这里的意思是找出名字里面还有AR的所有planetcondition = new EntityExpr("planetName", EntityOperator.LIKE, "%AR%");
- % - 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之间的空格有且只能有一个,多了少了都会出错,其他地方有多少个空格都无所谓
或者
注意:-和planetId之间不能有空格,否则出错,其他地方空格随意order = UtilMisc.toList("-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);