Ofbiz项目学习——阶段性小结——视图

2022-11-26,,,,

一、简要介绍

1、按照SQL的视图概念:在 SQL 中,视图是基于 SQL 语句的结果集的可视化的表。视图包含行和列,就像一个真实的表。视图中的字段就是来自一个或多个数据库中的真实的表中的字段。

2、SQL CREATE VIEW 语法

CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition

3、举例:视图 "Current Product List" 会从 Products 表列出所有正在使用的产品。这个视图使用下列 SQL 创建:

CREATE VIEW [Current Product List] AS
SELECT ProductID,ProductName
FROM Products
WHERE Discontinued=No
// 我们可以查询上面这个视图:
SELECT * FROM [Current Product List]
// Northwind 样本数据库的另一个视图会选取 Products 表中所有单位价格高于平均单位价格的产品
CREATE VIEW [Products Above Average Price] AS
SELECT ProductName,UnitPrice
FROM Products
WHERE UnitPrice>(SELECT AVG(UnitPrice) FROM Products)

二 、ofbiz——动态视图——Java代码实现

思路:定义成员实体(表),定义各个实体的别名(表字段别名),成员实体建立字段关系(表之间通过字段建立联系)

DynamicViewEntity dve = new DynamicViewEntity();
// 定义成员实体(表)
dve.addMemberEntity("tableOne", "tableOne");
dve.addMemberEntity("tableTwo", "tableTwo");
dve.addMemberEntity("tableTwo", "tableThree");

定义各个实体的别名(表字段别名)

// 定义表1的字段
dve.addAlias("tableOne", "id");
// tableOne表中的字段全部显示,并且都不设置前缀
dve.addAlias("tableOne", null);
// 定义表2的字段
dve.addAlias("tableTwo", "id");
dve.addAlias("tableTwo", "name"); 
// 定义表3的字段
dve.addAlias("tableThree", "id");
dve.addAlias("tableThree","productId" );
dve.addAlias("tableThree", "sceneId");
dve.addAlias("tableThree", "riskId");

注意:有些字段需要重命名,

dve.addAlias("tableThree", "name", "firstName", null, null, null, null);

上述源码:

最后建立关系:(外连接)

dve.addViewLink(“tableOne”,"tableThree",Boolean.TRUE,
ModelKeyMap.makeKeyMapList("id", "riskId"));

内连接  

dve.addViewLink(“tableTwo”,"tableThree",Boolean.False,
UtilMisc.toList(new ModelKeyMap("id", "productId")));

源码:

    public void addViewLink(String entityAlias, String relEntityAlias, Boolean relOptional, List<ModelKeyMap> modelKeyMaps) {
ModelViewLink modelViewLink = new ModelViewLink(entityAlias, relEntityAlias, relOptional, null, modelKeyMaps);
this.viewLinks.add(modelViewLink);
}

最后:组装要展示的列,执行查询

Set<String> set = new HashSet<>();
set.add("id");
set.add("riskId");
set.add("productId"); List<GenericValue> list = EntityQuery.use(delegator)
.select(set)
.from(dve)
.where(condList)
.cursorScrollInsensitive()
.queryList();

  

全部实现 代码:

/**
* 多表联合查询
* @param dctx
* @param context
* @return
*/
public static Map<String,Object> queryMoreTable( DispatchContext dctx, Map<String,Object> context ){ //取得实体引擎实例
GenericDelegator delegator = dctx.getDelegator(); // 使用动态视图查询批处理信息
// dynamic view entity
DynamicViewEntity dve = new DynamicViewEntity();
//当事人表
dve.addMemberEntity("Party", "Party");
dve.addAliasAll("Party", null); // Party表中的字段全部显示,并且都不设置前缀
//登陆信息表
dve.addMemberEntity("UL", "UserLogin");
dve.addAlias("UL", "userLoginId");
dve.addAlias("UL", "currentPassword");
dve.addAlias("UL", "ulEnabled", "enabled", null, null, null, null); // 重命名
// 左外连接
dve.addViewLink("Party", "UL", Boolean.TRUE, UtilMisc.toList(new ModelKeyMap("partyId", "partyId")));
//个人信息表
dve.addMemberEntity("Person", "Person");
dve.addAlias("Person", "name", "firstName", null, null, null, null); // 重命名
dve.addAlias("Person", "birthDate");
dve.addAlias("Person", "createdStamp");
// 内连接
dve.addViewLink("Party", "Person", Boolean.FALSE, UtilMisc.toList(new ModelKeyMap("partyId", "partyId"))); // 取得查询条件
EntityCondition conditions = EntityCondition.makeCondition("partyTypeId", EntityOperator.EQUALS, "PERSON"); EntityFindOptions opts = new EntityFindOptions();
opts.setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE); //该字段的设置, 在采用分页查询时才会用到, 因为结果集类型为这个值时,游标才可以向回滚动.
List<GenericValue> personInfos = FastList.newInstance();
int personCount = 0;
EntityListIterator personIterator = null; //实体集合迭代器, 用来操作数据的.和数据库中的结果集功能类似
try {
personIterator = delegator.findListIteratorByCondition(dve, conditions, null, null, null, opts); //查询出符合条件的记录的总个数
personIterator.last(); //让游标移动到最后
personCount = personIterator.currentIndex(); //取出此时的游标索引,就是记录的总个数
//把游标移动到最开始位置
personIterator.beforeFirst(); GenericValue onePerson = null;
while ( personIterator.hasNext() ) {
onePerson = personIterator.next();
personInfos.add(onePerson);
//因为这是个测试程序,为了防止数据溢出,我查询出20条件就不查询了,下面的是检查代码,如果记录已经有20条时,跳出循环.
//在正常使用时,我们一般会分页的方式来解决大数据量问题
if( personInfos.size() >= 20){
break;
}
} } catch (GenericEntityException e) {
Debug.logError(e, module);
//把指定的错误码对应的描述信息返回给服务调用者
return ReturnMapUtil.getErrorMap(DemoErrorMapping.BASE0009,e.getMessage());
} finally {
// 关闭迭代器(一定要记得关闭哦,不然就死定了)
if (UtilValidate.isNotEmpty(personIterator)) {
try {
personIterator.close();
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
}
}
//返回
Map<String, Object> result = FastMap.newInstance();
result.put("personInfos", personInfos);
result.put("personCount", personCount);
return result;
}

  

findListIteratorByCondition源码:
    public EntityListIterator findListIteratorByCondition(DynamicViewEntity dynamicViewEntity, EntityCondition whereEntityCondition, EntityCondition havingEntityCondition, Collection<String> fieldsToSelect, List<String> orderBy, 
      EntityFindOptions findOptions) throws GenericEntityException {
return findListIteratorByCondition(dynamicViewEntity, whereEntityCondition, havingEntityCondition, fieldsToSelect, orderBy, findOptions, false);
}

  

  

  

Ofbiz项目学习——阶段性小结——视图的相关教程结束。

《Ofbiz项目学习——阶段性小结——视图.doc》

下载本文的Word格式文档,以方便收藏与打印。