Browse Source

封装tablestore通过索引列查询数据列表

tanzhongran 3 years ago
parent
commit
5430fb2a29

+ 94 - 32
abi-cloud-qr-platform-common/src/main/java/com/abi/task/common/tablestore/TableStorePlusUtils.java

@@ -6,10 +6,12 @@ import com.abi.task.common.api.exception.BusinessException;
 import com.abi.task.common.api.exception.ErrorCodeEnum;
 import com.abi.task.common.tablestore.common.TableStore;
 import com.abi.task.common.tablestore.common.TableStoreEntity;
+import com.abi.task.common.tablestore.common.TableStoreReq;
+import com.abi.task.common.tablestore.common.TableStoreRes;
 import com.abi.task.common.utils.IStringUtil;
-import com.abi.task.common.utils.TableStoreUtils;
 import com.alicloud.openservices.tablestore.model.Column;
 import com.alicloud.openservices.tablestore.model.ColumnValue;
+import com.alicloud.openservices.tablestore.model.PrimaryKeyColumn;
 import com.alicloud.openservices.tablestore.model.Row;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
@@ -146,7 +148,7 @@ public class TableStorePlusUtils {
      * @param <T>
      * @return
      */
-    public <T extends TableStoreEntity> T getRow(Class<T> clz, String primaryKeyVal) {
+    public <T extends TableStoreEntity> T selectOne(Class<T> clz, String primaryKeyVal) {
         T entity = null;
 
         try {
@@ -166,6 +168,72 @@ public class TableStorePlusUtils {
 
             //调用tablestore查询到数据
             Row row = tableStoreUtils.getRow(primaryKeyName, primaryKeyVal, tableName);
+            //转为对象
+            entity = toEntity(clz,row);
+
+        } catch (Exception e) {
+            log.info("获取table store异常", e);
+            throw new BusinessException("获取table store异常");
+        }
+
+        return entity;
+    }
+
+    /**
+     * 读取多行数据(重载方法)
+     * @param req
+     * @param <T>
+     * @return
+     */
+    public <T extends TableStoreEntity> List<T> selectList(TableStoreReq req) {
+        if(req.getClz()==null){
+            throw new BusinessException("印射对象为空");
+        }
+
+        return selectList(req.getClz(),req);
+    }
+
+    /**
+     * 读取多行数据
+     * @param clz
+     * @param req
+     * @param <T>
+     * @return
+     */
+    public <T extends TableStoreEntity> List<T> selectList(Class<T> clz, TableStoreReq req) {
+        TableStoreRes tableStoreRes = tableStoreUtils.listRows(req);
+        List<T> rows = toEntityList(clz, tableStoreRes.getRowList());
+        return rows;
+    }
+
+    /**
+     * row列表转对象列表
+     * @param clz
+     * @param rowList
+     * @param <T>
+     * @return
+     */
+    public <T extends TableStoreEntity> List<T> toEntityList(Class<T> clz,List<Row> rowList){
+        List<T> resultList = new ArrayList<>();
+        for(Row row:rowList){
+            resultList.add(toEntity(clz,row));
+        }
+        return resultList;
+    }
+
+    /**
+     * row转对象
+     * @param clz
+     * @param row
+     * @param <T>
+     * @return
+     */
+    private <T extends TableStoreEntity> T toEntity(Class<T> clz, Row row){
+        //转为对象
+        T entity = null;
+
+        try{
+            entity = clz.newInstance();
             //获取字段的值
             Column[] columns = row.getColumns();
             //转化为map
@@ -173,9 +241,10 @@ public class TableStorePlusUtils {
             for (Column column : columns) {
                 rowMap.put(column.getName(), column.getValue().getValue());
             }
+            //map放入主键值
+            PrimaryKeyColumn primaryKeyColumn = row.getPrimaryKey().getPrimaryKeyColumn(0);
+            rowMap.put(primaryKeyColumn.getName(), primaryKeyColumn.getValue().asString());
 
-            //转为对象
-            entity = clz.newInstance();
             //循环塞值
             Field[] fields = entity.getClass().getDeclaredFields();
             for (Field field : fields) {
@@ -184,44 +253,37 @@ public class TableStorePlusUtils {
                 Class<?> fieldType = field.getType();
                 //拿到tablestore的值
                 Object columnVal = rowMap.get(underlineFieldName);
+
                 //转化完成的值
                 Object fieldValue = null;
                 //获取对象的set方法
                 Method method = clz.getMethod("set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1), fieldType);
-                //赛主键
-                if (underlineFieldName.equals(primaryKeyName)) {
-                    fieldValue = primaryKeyVal;
-                } else {
-                    //塞其他值
-                    if (columnVal != null) {
-                        if (fieldType == String.class) {
-                            fieldValue = (String) columnVal;
-                        } else if (fieldType == Integer.class) {
-                            fieldValue = Integer.valueOf(String.valueOf(columnVal));
-                        } else if (fieldType == Long.class) {
-                            fieldValue = (Long) columnVal;
-                        } else if (fieldType == Double.class) {
-                            fieldValue = (Double) columnVal;
-                        } else if (fieldType == Boolean.class) {
-                            fieldValue = (Boolean) columnVal;
-                        } else if (fieldType == Date.class) {
-                            fieldValue = DateUtil.parse((String) columnVal, "yyyy-MM-dd HH:mm:ss");
-                        } else if (fieldType == LocalDateTime.class) {
-                            fieldValue = DateUtil.parseLocalDateTime((String) columnVal, "yyyy-MM-dd HH:mm:ss");
-                        } else {
-                            log.info("不识别的字段类型 fieldName=" + fieldName);
-                            continue;
-                        }
+                if (columnVal != null) {
+                    if (fieldType == String.class) {
+                        fieldValue = (String) columnVal;
+                    } else if (fieldType == Integer.class) {
+                        fieldValue = Integer.valueOf(String.valueOf(columnVal));
+                    } else if (fieldType == Long.class) {
+                        fieldValue = (Long) columnVal;
+                    } else if (fieldType == Double.class) {
+                        fieldValue = (Double) columnVal;
+                    } else if (fieldType == Boolean.class) {
+                        fieldValue = (Boolean) columnVal;
+                    } else if (fieldType == Date.class) {
+                        fieldValue = DateUtil.parse((String) columnVal, "yyyy-MM-dd HH:mm:ss");
+                    } else if (fieldType == LocalDateTime.class) {
+                        fieldValue = DateUtil.parseLocalDateTime((String) columnVal, "yyyy-MM-dd HH:mm:ss");
+                    } else {
+                        log.info("不识别的字段类型 fieldName=" + fieldName);
+                        continue;
                     }
                 }
 
                 //反射调用set
                 method.invoke(entity, fieldValue);
             }
-
-        } catch (Exception e) {
-            log.info("获取table store异常", e);
-            throw new BusinessException("获取table store异常");
+        }catch (Exception e){
+            log.info("转换异常",e);
         }
 
         return entity;

+ 145 - 26
abi-cloud-qr-platform-common/src/main/java/com/abi/task/common/utils/TableStoreUtils.java

@@ -1,8 +1,11 @@
-package com.abi.task.common.utils;
+package com.abi.task.common.tablestore;
 
 import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.date.DateUtil;
 import com.abi.task.common.api.exception.BusinessException;
 import com.abi.task.common.api.exception.ErrorCodeEnum;
+import com.abi.task.common.tablestore.common.TableStoreReq;
+import com.abi.task.common.tablestore.common.TableStoreRes;
 import com.alicloud.openservices.tablestore.SyncClient;
 import com.alicloud.openservices.tablestore.model.BatchWriteRowRequest;
 import com.alicloud.openservices.tablestore.model.BatchWriteRowResponse;
@@ -29,10 +32,22 @@ import com.alicloud.openservices.tablestore.model.SingleRowQueryCriteria;
 import com.alicloud.openservices.tablestore.model.TableMeta;
 import com.alicloud.openservices.tablestore.model.TableOptions;
 import com.alicloud.openservices.tablestore.model.UpdateRowRequest;
+import com.alicloud.openservices.tablestore.model.search.SearchQuery;
+import com.alicloud.openservices.tablestore.model.search.SearchRequest;
+import com.alicloud.openservices.tablestore.model.search.SearchResponse;
+import com.alicloud.openservices.tablestore.model.search.query.RangeQuery;
+import com.alicloud.openservices.tablestore.model.search.query.TermsQuery;
+import com.alicloud.openservices.tablestore.model.search.sort.FieldSort;
+import com.alicloud.openservices.tablestore.model.search.sort.Sort;
+import com.alicloud.openservices.tablestore.model.search.sort.SortOrder;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.Date;
 import java.util.List;
 
 /**
@@ -152,30 +167,6 @@ public class TableStoreUtils {
         }
     }
 
-    /**
-     * 构造RowPutChange
-     *
-     * @param primaryKeyName
-     * @param pkValue
-     * @param tableName
-     * @param columns
-     * @return
-     */
-    private RowPutChange buildRowPutChange(String primaryKeyName, String pkValue, String tableName, List<Column> columns) {
-        //构造主键。
-        PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
-        //设置主键名称和主键值
-        primaryKeyBuilder.addPrimaryKeyColumn(primaryKeyName, PrimaryKeyValue.fromString(pkValue));
-        PrimaryKey primaryKey = primaryKeyBuilder.build();
-        //设置数据表名称。
-        RowPutChange rowPutChange = new RowPutChange(tableName, primaryKey);
-
-        //添加属性列
-        rowPutChange.addColumns(columns);
-
-        return rowPutChange;
-    }
-
     /**
      * 单行读取一行数据
      *
@@ -202,7 +193,6 @@ public class TableStoreUtils {
      * @return 一行某一列的数据
      */
     public Row getColumnRow(String primaryKeyName, String pkValue, String tableName, String columnName) {
-
         SingleRowQueryCriteria criteria = queryCriteria(primaryKeyName, pkValue, tableName);
 
         //设置读取某些列。
@@ -265,6 +255,112 @@ public class TableStoreUtils {
         client().deleteRow(new DeleteRowRequest(rowDeleteChange));
     }
 
+    /**
+     * 通过聚集索引分页查询
+     * @param req
+     * @return
+     */
+    public TableStoreRes listRows(TableStoreReq req){
+        //0-校验入参合法性
+        if(StringUtils.isBlank(req.getTableName())){
+            throw new BusinessException("TableName为空");
+        }
+        if(StringUtils.isBlank(req.getIndexName())){
+            throw new BusinessException("IndexName为空");
+        }
+        if(StringUtils.isBlank(req.getFieldName())){
+            throw new BusinessException("FieldName为空");
+        }
+        if(req.getFieldValue()==null
+            &&(req.getLessThan()==null || req.getGreaterThan()==null)){
+            throw new BusinessException("查询条件为空");
+        }
+
+        //1-构建查询条件
+        SearchQuery searchQuery = new SearchQuery();
+
+        //精确查询
+        if(req.getFieldValue()!=null){
+            TermsQuery termsQuery = new TermsQuery();
+            //设置要匹配的字段。
+            termsQuery.setFieldName(req.getFieldName());
+            //设置要匹配的值。
+            termsQuery.addTerm(toColumnValue(req.getFieldValue()));
+            searchQuery.setQuery(termsQuery);
+        }
+
+        //范围匹配
+        if(req.getLessThan()!=null && req.getGreaterThan()!=null){
+            RangeQuery rangeQuery = new RangeQuery();
+            //设置要匹配的字段。
+            rangeQuery.setFieldName(req.getFieldName());
+            //设置该字段的范围条件为大于小于
+            rangeQuery.greaterThan(toColumnValue(req.getGreaterThan()));
+            rangeQuery.lessThan(toColumnValue(req.getLessThan()));
+            searchQuery.setQuery(rangeQuery);
+        }
+
+        //设置按照  列 逆序排序。
+        if(req.getSortByFieldDesc()){
+            FieldSort fieldSort = new FieldSort(req.getFieldName());
+            fieldSort.setOrder(SortOrder.DESC);
+            searchQuery.setSort(new Sort(Arrays.asList((Sort.Sorter)fieldSort)));
+        }
+
+        //设置分页
+        int offset = (req.getPageNo()-1)*req.getPageCount();
+        searchQuery.setLimit(req.getPageCount());
+        searchQuery.setOffset(offset);
+
+        //设置返回匹配的总行数
+        searchQuery.setGetTotalCount(req.getGetTotalCount());
+
+        SearchRequest searchRequest = new SearchRequest(req.getTableName(), req.getIndexName(), searchQuery);
+        //通过设置columnsToGet参数可以指定返回的列或返回所有列,如果不设置此参数,则默认只返回主键列。
+        SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
+        //设置为返回所有列。
+        columnsToGet.setReturnAll(true);
+        searchRequest.setColumnsToGet(columnsToGet);
+        //table store的出参
+        SearchResponse resp = client().search(searchRequest);
+
+        //我们自己的出参
+        TableStoreRes res = new TableStoreRes();
+        res.setTotalCount(resp.getTotalCount());
+        res.setRowList(resp.getRows());
+        return res;
+    }
+
+
+
+
+
+    //私有方法-----------------------------------------
+
+    private ColumnValue toColumnValue(Object fieldValue){
+        ColumnValue columnValue = null;
+        if (fieldValue instanceof String) {
+            columnValue = ColumnValue.fromString(String.valueOf(fieldValue));
+        } else if (fieldValue instanceof Integer) {
+            columnValue = ColumnValue.fromLong(Long.valueOf((Integer) fieldValue));
+        } else if (fieldValue instanceof Long) {
+            columnValue = ColumnValue.fromLong((Long) fieldValue);
+        } else if (fieldValue instanceof Double) {
+            columnValue = ColumnValue.fromDouble((Double) fieldValue);
+        } else if (fieldValue instanceof Boolean) {
+            columnValue = ColumnValue.fromBoolean((Boolean) fieldValue);
+        } else if (fieldValue instanceof Date) {
+            columnValue = ColumnValue.fromString(DateUtil.format((Date) fieldValue, "yyyy-MM-dd HH:mm:ss"));
+        } else if (fieldValue instanceof LocalDateTime) {
+            columnValue = ColumnValue.fromString(DateUtil.format((LocalDateTime) fieldValue, "yyyy-MM-dd HH:mm:ss"));
+        } else {
+            throw new BusinessException("不识别的字段类型fieldValue");
+        }
+
+        return columnValue;
+    }
+
+
     private SingleRowQueryCriteria queryCriteria(String primaryKeyName, String pkValue, String tableName) {
         //构造主键。
         PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
@@ -287,5 +383,28 @@ public class TableStoreUtils {
         return new RowUpdateChange(tableName, primaryKey);
     }
 
+    /**
+     * 构造RowPutChange
+     *
+     * @param primaryKeyName
+     * @param pkValue
+     * @param tableName
+     * @param columns
+     * @return
+     */
+    private RowPutChange buildRowPutChange(String primaryKeyName, String pkValue, String tableName, List<Column> columns) {
+        //构造主键。
+        PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
+        //设置主键名称和主键值
+        primaryKeyBuilder.addPrimaryKeyColumn(primaryKeyName, PrimaryKeyValue.fromString(pkValue));
+        PrimaryKey primaryKey = primaryKeyBuilder.build();
+        //设置数据表名称。
+        RowPutChange rowPutChange = new RowPutChange(tableName, primaryKey);
+
+        //添加属性列
+        rowPutChange.addColumns(columns);
+
+        return rowPutChange;
+    }
 
 }

+ 16 - 0
abi-cloud-qr-platform-common/src/main/java/com/abi/task/common/tablestore/common/TableStoreIndex.java

@@ -0,0 +1,16 @@
+package com.abi.task.common.tablestore.common;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * tableStore索引field注解
+ * @author AndyTan
+ */
+@Target(ElementType.FIELD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface TableStoreIndex {
+    String indexName();
+}

+ 164 - 0
abi-cloud-qr-platform-common/src/main/java/com/abi/task/common/tablestore/common/TableStoreReq.java

@@ -0,0 +1,164 @@
+package com.abi.task.common.tablestore.common;
+
+import com.abi.task.common.api.exception.BusinessException;
+import com.abi.task.common.utils.IStringUtil;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.poi.ss.formula.functions.T;
+
+import java.lang.reflect.Field;
+
+/**
+ * 查询table store的入参
+ * @author AndyTan
+ */
+@Data
+@Slf4j
+public class TableStoreReq {
+
+    /**
+     * 表名
+     */
+    String tableName;
+
+    /**
+     * 索引名
+     */
+    String indexName;
+
+    /**
+     * 匹配的字段名
+     */
+    String fieldName;
+
+    /**
+     * 匹配的字段值
+     */
+    Object fieldValue;
+
+    /**
+     * 大于xxx
+     */
+    Object greaterThan;
+
+    /**
+     * 小于xxx
+     */
+    Object lessThan;
+
+    /**
+     * 每页条数
+     */
+    Integer pageCount = 10;
+
+    /**
+     * 第几页
+     */
+    Integer pageNo = 1;
+
+    /**
+     * 需要返回总数量
+     */
+    Boolean getTotalCount = false;
+
+    /**
+     * 是否逆序
+     */
+    Boolean sortByFieldDesc = false;
+
+    /**
+     * 印射对象类
+     */
+    Class clz;
+
+    /**
+     * 无参构造函数
+     */
+    public TableStoreReq(){}
+
+    /**
+     * 构造函数
+     */
+    public <T extends TableStoreEntity> TableStoreReq(Class<T> clz, String fieldName){
+        Field field = null;
+        try{
+            field = clz.getDeclaredField(fieldName);
+        }catch (Exception e){
+            log.info("找不到字段",e);
+        }
+
+        //表名
+        TableStore tableStore = clz.getAnnotation(TableStore.class);
+        String tableName = tableStore.tableName();
+        //索引
+        TableStoreIndex tableStoreIndex = field.getAnnotation(TableStoreIndex.class);
+        String indexName = tableStoreIndex.indexName();
+        //字段名转下划线
+        fieldName = IStringUtil.camelToUnderline(fieldName);
+
+        this.tableName = tableName;
+        this.indexName = indexName;
+        this.fieldName = fieldName;
+        this.clz = clz;
+    }
+
+    /**
+     * 构造函数
+     * @param tableName 表名
+     * @param indexName 索引名
+     * @param fieldName 匹配的字段名
+     */
+    public TableStoreReq(String tableName,String indexName,String fieldName){
+        this.tableName = tableName;
+        this.indexName = indexName;
+        this.fieldName = fieldName;
+    }
+
+    /**
+     * 设置范围
+     * @param greaterThan
+     * @param lessThan
+     * @return
+     */
+    public TableStoreReq range(Object greaterThan,Object lessThan){
+        this.setGreaterThan(greaterThan);
+        this.setLessThan(lessThan);
+        return this;
+    }
+
+    /**
+     * 设置等于
+     * @param fieldValue
+     * @return
+     */
+    public TableStoreReq equal(Object fieldValue){
+        this.setFieldValue(fieldValue);
+        return this;
+    }
+
+    public TableStoreReq page(Integer pageNo,Integer pageCount){
+        this.pageNo = pageNo;
+        this.pageCount = pageCount;
+        return this;
+    }
+
+    /**
+     * 是否逆序
+     * @return
+     */
+    public TableStoreReq isDesc(){
+        this.setSortByFieldDesc(true);
+        return this;
+    }
+
+    /**
+     * 是否需要总数
+     * @return
+     */
+    public TableStoreReq isNeedTotalCount(){
+        this.setGetTotalCount(true);
+        return this;
+    }
+
+
+}

+ 27 - 0
abi-cloud-qr-platform-common/src/main/java/com/abi/task/common/tablestore/common/TableStoreRes.java

@@ -0,0 +1,27 @@
+package com.abi.task.common.tablestore.common;
+
+import com.alicloud.openservices.tablestore.model.Row;
+import lombok.Data;
+import lombok.ToString;
+
+import java.util.List;
+
+/**
+ * 查询table store的入参
+ * @author AndyTan
+ */
+@Data
+@ToString
+public class TableStoreRes {
+
+    /**
+     * 总行数
+     */
+    private Long totalCount;
+
+    /**
+     * 行列表
+     */
+    private List<Row> rowList;
+
+}

+ 3 - 2
abi-cloud-qr-platform-common/src/main/java/com/abi/task/common/tablestore/entity/DemoEntity.java

@@ -2,6 +2,7 @@ package com.abi.task.common.tablestore.entity;
 
 import com.abi.task.common.tablestore.common.TableStore;
 import com.abi.task.common.tablestore.common.TableStoreEntity;
+import com.abi.task.common.tablestore.common.TableStoreIndex;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 
@@ -14,12 +15,12 @@ public class DemoEntity implements TableStoreEntity {
 
     private String qrCode;
 
-    private String actName;
-
     private Double payAmount;
 
+    @TableStoreIndex(indexName = "index_current_time")
     private LocalDateTime currentTime;
 
+    @TableStoreIndex(indexName = "index_user_id")
     private Long userId;
 
     private Integer activeCount;

File diff suppressed because it is too large
+ 369 - 0
abi-cloud-qr-platform-server/hs_err_pid31936.log


File diff suppressed because it is too large
+ 10293 - 0
abi-cloud-qr-platform-server/replay_pid31936.log


+ 3 - 0
abi-cloud-qr-platform-server/src/main/java/com/abi/qms/platform/dao/tablestore/entity/QrCode.java

@@ -2,6 +2,7 @@ package com.abi.qms.platform.dao.tablestore.entity;
 
 import com.abi.task.common.tablestore.common.TableStore;
 import com.abi.task.common.tablestore.common.TableStoreEntity;
+import com.abi.task.common.tablestore.common.TableStoreIndex;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 
@@ -25,6 +26,7 @@ public class QrCode implements TableStoreEntity, Serializable {
     /**
      * 码包id
      */
+    @TableStoreIndex(indexName = "index_package_id")
     private Long packageId;
 
     /**
@@ -40,6 +42,7 @@ public class QrCode implements TableStoreEntity, Serializable {
     /**
      * 已生成码包位置
      */
+    @TableStoreIndex(indexName = "index_code_index")
     private Long codeIndex;
 
     /**

+ 1 - 1
abi-cloud-qr-platform-server/src/main/java/com/abi/qms/platform/service/impl/QrBoxMappingServiceImpl.java

@@ -159,7 +159,7 @@ public class QrBoxMappingServiceImpl implements QrBoxMappingService {
      * @return
      */
     private Long getCodeIndex(String index) {
-        QrCode qrCode = tableStorePlusUtils.getRow(QrCode.class, index);
+        QrCode qrCode = tableStorePlusUtils.selectOne(QrCode.class, index);
         return qrCode.getCodeIndex();
     }
 

+ 38 - 3
abi-cloud-qr-platform-server/src/test/java/com/abi/qms/platform/TableStorePlusTest.java

@@ -2,6 +2,9 @@ package com.abi.qms.platform;
 
 import com.abi.qms.platform.dao.tablestore.entity.QrCode;
 import com.abi.task.common.tablestore.TableStorePlusUtils;
+import com.abi.task.common.tablestore.common.TableStoreEntity;
+import com.abi.task.common.tablestore.common.TableStoreReq;
+import com.abi.task.common.tablestore.common.TableStoreRes;
 import com.abi.task.common.tablestore.entity.DemoEntity;
 import lombok.extern.slf4j.Slf4j;
 import org.junit.jupiter.api.Test;
@@ -10,6 +13,7 @@ import org.springframework.boot.test.context.SpringBootTest;
 
 import java.time.LocalDateTime;
 import java.util.Arrays;
+import java.util.List;
 
 @SpringBootTest
 @Slf4j
@@ -22,7 +26,6 @@ public class TableStorePlusTest {
     public void testAddRow() {
         DemoEntity entity = new DemoEntity();
         entity.setQrCode("10293827182717");
-        entity.setActName("活动1");
         entity.setPayAmount(990.12);
         entity.setCurrentTime(LocalDateTime.now());
         entity.setUserId(10001L);
@@ -31,7 +34,6 @@ public class TableStorePlusTest {
 
         DemoEntity entity2 = new DemoEntity();
         entity2.setQrCode("10293827182716");
-        entity2.setActName("活动2");
         entity2.setPayAmount(990.12);
         entity2.setCurrentTime(LocalDateTime.now());
         entity2.setUserId(10001L);
@@ -43,15 +45,48 @@ public class TableStorePlusTest {
 
     @Test
     public void testGetRow() {
-        QrCode entity = tableStorePlusUtils.getRow(QrCode.class, "1397521498780426242");
+        QrCode entity = tableStorePlusUtils.selectOne(QrCode.class, "1397521498780426242");
         Long codeIndex = entity.getCodeIndex();
         log.info("======" + codeIndex);
     }
 
+    //按索引范围匹配
+    @Test
+    public void queryRowsRange(){
+        //构造入参
+        TableStoreReq req = new TableStoreReq(QrCode.class,"codeIndex")
+                .range(1,100)
+                .page(1,15)
+                .isDesc();
+        //调用查询
+        List<QrCode> qrCodeList = tableStorePlusUtils.selectList(req);
+        //打印
+        for(QrCode qrCode:qrCodeList){
+            log.info(qrCode.toString());
+        }
+    }
+
+    //按索引精确匹配
+    @Test
+    public void queryRowsEqual(){
+        TableStoreReq req = new TableStoreReq(QrCode.class,"packageId")
+                .equal("1")
+                .page(1,3);
+
+        List<QrCode> qrCodeList = tableStorePlusUtils.selectList(req);
+        //打印
+        for(QrCode qrCode:qrCodeList){
+            log.info(qrCode.toString());
+        }
+    }
+
+
+
     @Test
     public void testDeleteRow() {
         tableStorePlusUtils.deleteRow(DemoEntity.class, "10293827182717");
     }
 
 
+
 }

+ 25 - 1
abi-cloud-qr-platform-server/src/test/java/com/abi/qms/platform/TableStoreTest.java

@@ -1,7 +1,9 @@
 package com.abi.qms.platform;
 
 import cn.hutool.json.JSONUtil;
-import com.abi.task.common.utils.TableStoreUtils;
+import com.abi.task.common.tablestore.TableStoreUtils;
+import com.abi.task.common.tablestore.common.TableStoreReq;
+import com.abi.task.common.tablestore.common.TableStoreRes;
 import com.alicloud.openservices.tablestore.model.Column;
 import com.alicloud.openservices.tablestore.model.ColumnValue;
 import com.alicloud.openservices.tablestore.model.DeleteTableRequest;
@@ -95,4 +97,26 @@ public class TableStoreTest {
         tableStoreUtils.deleteRow(PRIMARYKEYNAME,"00002",TABLENAME);
     }
 
+    //按索引范围匹配
+    @Test
+    public void queryRowsRange(){
+        TableStoreReq req = new TableStoreReq("qr_code","index_code_index","code_index")
+                .range(1,9)
+                .isDesc();
+
+        TableStoreRes tableStoreRes = tableStoreUtils.listRows(req);
+        log.info(tableStoreRes.toString());
+    }
+
+    //按索引精确匹配
+    @Test
+    public void queryRowsEqual(){
+        TableStoreReq req = new TableStoreReq("qr_code","index_code_index","code_index")
+                .equal("1")
+                .isNeedTotalCount();
+
+        TableStoreRes tableStoreRes = tableStoreUtils.listRows(req);
+        log.info(tableStoreRes.toString());
+    }
+
 }

+ 1 - 0
abi-cloud-qr-platform-server/src/test/java/com/abi/qms/platform/TableStoreXTest.java

@@ -1,5 +1,6 @@
 package com.abi.qms.platform;
 
+import com.abi.task.common.tablestore.TableStoreUtils;
 import com.alicloud.openservices.tablestore.SyncClient;
 import com.alicloud.openservices.tablestore.TableStoreException;
 import com.alicloud.openservices.tablestore.model.*;