Fabric链码查询数据库工具类

该博客介绍了Fabric链码的查询工具类`QueryUtil`,提供了查询列表和分页查询的功能。`queryList`方法用于根据查询字符串获取列表结果,`pagedQuery`则实现了分页查询,返回包含数据和分页标记的页面结果。此外,还有`queryHistoryForId`方法用于查询特定ID的历史记录。
摘要由CSDN通过智能技术生成

# Fabric链码查询数据库工具类
[TOC]

## QueryUtil

```
package com.yl.crm.common.utils;

import com.alibaba.fastjson.JSON;
import com.yl.crm.common.result.ListResult;
import com.yl.crm.common.result.PageResult;
import com.yl.crm.demand.entity.Demand;
import lombok.extern.java.Log;
import org.apache.commons.collections4.IterableUtils;
import org.hyperledger.fabric.shim.ChaincodeException;
import org.hyperledger.fabric.shim.ChaincodeStub;
import org.hyperledger.fabric.shim.ledger.KeyModification;
import org.hyperledger.fabric.shim.ledger.KeyValue;
import org.hyperledger.fabric.shim.ledger.QueryResultsIterator;
import org.hyperledger.fabric.shim.ledger.QueryResultsIteratorWithMetadata;

import java.util.ArrayList;
import java.util.List;

/**
 * ListUtil
 *
 * @author wangxiaohu
 * @date 2022-11-10 11:27
 */
@Log
public class QueryUtil<T> {
    /**
     * 查询列表
     *
     * @param stub  stub
     * @param query query
     * @param type     type
     * @return ListResult<T>
     */
    public ListResult<T> queryList(ChaincodeStub stub, String query, Class<T> type) {
        log.info(String.format("列表查询 , 入参 = %s", query));
        ListResult<T> resultList = new ListResult<>();
        QueryResultsIterator<KeyValue> queryResult = stub.getQueryResult(query);
        List<T> results = new ArrayList<>();

        if (!IterableUtils.isEmpty(queryResult)) {
            for (KeyValue kv : queryResult) {
                results.add(JSON.parseObject(kv.getStringValue(), type));
            }
            resultList.setData(results);
        }

        log.info(String.format("列表查询,入参 = %s,返回结果=%s", query, JSON.toJSONString(resultList)));
        return resultList;
    }

    /**
     * 分页查询
     *
     * @param stub     stub
     * @param query    query
     * @param pageSize pageSize
     * @param bookmark bookmark
     * @return PageResult<T>
     */
    public PageResult<T> pagedQuery(ChaincodeStub stub, String query, int pageSize, String bookmark, , Class<T> type) {
        log.info(String.format("分页查询  , query = %s", query));
        QueryResultsIteratorWithMetadata<KeyValue> queryResult = stub.getQueryResultWithPagination(query,
                pageSize,
                bookmark);

        List<T> list = new ArrayList<>();

        if (!IterableUtils.isEmpty(queryResult)) {
            for (KeyValue kv : queryResult) {
                list.add(JSON.parseObject(kv.getValue(), type));
            }
        }

        PageResult<T> pageResult = new PageResult<T>()
                .setData(list)
                .setBookmark(queryResult.getMetadata().getBookmark());

        log.info(String.format("分页查询入参=%s,返回结果=%s", query, JSON.toJSONString(pageResult)));
        return pageResult;
    }

    /**
     * 查询账本历史记录
     *
     * @param stub stub
     * @param id   id
     * @return String
     */
    public String queryHistoryForId(ChaincodeStub stub, String id) {
        log.info(String.format("queryHistoryForId  , id = %s", id));

        try {
            QueryResultsIterator<KeyModification> history = stub.getHistoryForKey(id);
            List<KeyModification> list = new ArrayList<>();
            history.forEach(list::add);
            String result = JSON.toJSONString(list);
            log.info("getClientHistoryForId-->入参:" + id + ",查询结果:" + list);
            return result;
        } catch (Exception e) {
            throw new ChaincodeException("历史记录查询错误");
        }
    }
}

```

## ListResult
```
package com.yl.crm.common.result;

import lombok.Data;
import org.hyperledger.fabric.contract.annotation.DataType;
import org.hyperledger.fabric.contract.annotation.Property;

import java.util.List;

@DataType
@Data
public class ListResult<T> {

    @Property
    List<T> data;

}

```

## PageResult
```
package com.yl.crm.common.result;

import lombok.Data;
import lombok.experimental.Accessors;
import org.hyperledger.fabric.contract.annotation.DataType;
import org.hyperledger.fabric.contract.annotation.Property;

import java.util.List;

@DataType
@Data
@Accessors(chain = true)
public class PageResult<T> {

    @Property
    String bookmark;

    @Property
    List<T> data;

}

```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值