3.3. Neo4j数据分页接口方法
首先,我们先设计一个通用的执行Cypher检索语句的接口方法,将检索结果(主要指Node、Relationship和Path对象)封转进Propertyies列表中。
/**
* 执行Cypher检索语句,将检索结果封装进Properties列表中。
* @param query cypher检索语句
* @param params cypher检索语句参数集合
* @return
*/
public List<Properties> executeQuery(String query, Map<String,Object> params) {
List<Properties> propertiesList = new ArrayList<Properties>();
if(StringUtils.isBlank(query)) {
return propertiesList;
}
ExecutionEngine executionEngine = new ExecutionEngine(this.getGraphDatabaseService());
ExecutionResult result = null;
if(params == null || params.size() == 0) {
result = executionEngine.execute(query);
} else {
result = executionEngine.execute(query, params);
}
for (Map<String, Object> row : result ) {
Properties properties = new Properties();
for ( Entry<String, Object> column : row.entrySet()){
properties.put(column.getKey(), column.getValue());
}
propertiesList.add(properties);
}
return propertiesList;
}
下面以无条件分页检索Node信息为例,讲述下接口方法的设计思路。具体代码如下:
/**
* 分页检索Node信息。
* @param pageModel 分页模型对象,不能为空。
* @param orders 排序属性字段。
* @return
*/
public GraphPageModel queryNodes(GraphPageModel pageModel, GOrderBy ... orders) {
if(pageModel == null) {
pageModel = new GraphPageModel(10);
}
pageModel.getNodeList().clear();
pageModel.getRelationshipList().clear();
//计算总行数
String query = "START n=node(*) RETURN count(*) AS NODE_COUNT";
List<Properties> resultList = this.executeQuery(query);
if(resultList == null || resultList.size() == 0) {
return pageModel;
}
for(Properties properties : resultList) {
int nodeCount = Integer.valueOf(properties.get("NODE_COUNT").toString());
pageModel.setTotalCount(nodeCount);
}
//组织排序字段信息
String strGOrderBy = "";
if(orders != null && orders.length > 0) {
strGOrderBy = "ORDER BY";
for(GOrderBy order : orders) {
strGOrderBy += String.format(" n.%s %s,", order.getPropertyName(), order.getOrderType().toUpperCase());
}
strGOrderBy = strGOrderBy.substring(0, strGOrderBy.length() - 1);
}
int skipCount = (pageModel.getPageIndex() - 1) * pageModel.getPageSize();
int limitCount = pageModel.getPageSize();
query = String.format("START n=node(*) RETURN n AS NODE_ENTRY %s SKIP %s LIMIT %s", strGOrderBy, skipCount, limitCount);
List<Properties> list = this.executeQuery(query);
for(Properties properties : list) {
pageModel.getNodeList().add((Node)properties.get("NODE_ENTRY"));
}
return pageModel;
}
Neo4j 的详细介绍:请点这里
Neo4j 的下载地址:请点这里
推荐阅读:
Neo4J图数据库实践系列