Преглед на файлове

继续优化封装的tablestore方法

tanzhongran преди 4 години
родител
ревизия
d9cb07266a

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

@@ -13,8 +13,10 @@ 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 com.alicloud.openservices.tablestore.model.search.SearchResponse;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.poi.ss.formula.functions.T;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
@@ -185,7 +187,7 @@ public class TableStorePlusUtils {
      * @param <T>
      * @return
      */
-    public <T extends TableStoreEntity> List<T> selectList(TableStoreReq req) {
+    public <T extends TableStoreEntity> TableStoreRes<T> selectList(TableStoreReq<T> req) {
         if(req.getClz()==null){
             throw new BusinessException("印射对象为空");
         }
@@ -200,10 +202,18 @@ public class TableStorePlusUtils {
      * @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;
+    public <T extends TableStoreEntity> TableStoreRes<T> selectList(Class<T> clz, TableStoreReq req) {
+        //拿到tablestore出参
+        SearchResponse resp = tableStoreUtils.listRows(req);
+        //构造我们的出参
+        TableStoreRes<T> res = new TableStoreRes<T>();
+        res.setRowList(resp.getRows());
+        res.setTotalCount(resp.getTotalCount());
+        //把rows转entity
+        List<T> entityList = toEntityList(clz, res.getRowList());
+        res.setEntityList(entityList);
+
+        return res;
     }
 
     /**

+ 3 - 9
abi-cloud-qr-platform-common/src/main/java/com/abi/task/common/tablestore/TableStoreUtils.java

@@ -46,9 +46,7 @@ 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;
+import java.util.*;
 
 /**
  * @author: fangxinjian
@@ -260,7 +258,7 @@ public class TableStoreUtils {
      * @param req
      * @return
      */
-    public TableStoreRes listRows(TableStoreReq req){
+    public SearchResponse listRows(TableStoreReq req){
         //0-校验入参合法性
         if(StringUtils.isBlank(req.getTableName())){
             throw new BusinessException("TableName为空");
@@ -324,11 +322,7 @@ public class TableStoreUtils {
         //table store的出参
         SearchResponse resp = client().search(searchRequest);
 
-        //我们自己的出参
-        TableStoreRes res = new TableStoreRes();
-        res.setTotalCount(resp.getTotalCount());
-        res.setRowList(resp.getRows());
-        return res;
+        return resp;
     }
 
 

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

@@ -5,6 +5,7 @@ import com.abi.task.common.utils.IStringUtil;
 import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.poi.ss.formula.functions.T;
+import org.checkerframework.checker.units.qual.K;
 
 import java.lang.reflect.Field;
 
@@ -14,81 +15,78 @@ import java.lang.reflect.Field;
  */
 @Data
 @Slf4j
-public class TableStoreReq {
+public class TableStoreReq<T extends TableStoreEntity> {
 
     /**
      * 表名
      */
-    String tableName;
+    private String tableName;
 
     /**
      * 索引名
      */
-    String indexName;
+    private String indexName;
 
     /**
      * 匹配的字段名
      */
-    String fieldName;
+    private String fieldName;
 
     /**
      * 匹配的字段值
      */
-    Object fieldValue;
+    private Object fieldValue;
 
     /**
      * 大于xxx
      */
-    Object greaterThan;
+    private Object greaterThan;
 
     /**
      * 小于xxx
      */
-    Object lessThan;
+    private Object lessThan;
 
     /**
      * 每页条数
      */
-    Integer pageCount = 10;
+    private Integer pageCount = 10;
 
     /**
      * 第几页
      */
-    Integer pageNo = 1;
+    private Integer pageNo = 1;
 
     /**
      * 需要返回总数量
      */
-    Boolean getTotalCount = false;
+    private Boolean getTotalCount = false;
 
     /**
      * 是否逆序
      */
-    Boolean sortByFieldDesc = false;
+    private Boolean sortByFieldDesc = false;
 
     /**
      * 印射对象类
      */
-    Class clz;
+    private Class clz;
 
     /**
-     * 无参构造函数
+     * 静态创建函数
      */
-    public TableStoreReq(){}
+    public static <T extends TableStoreEntity> TableStoreReq<T> build(Class<T> clzParam, String fieldName){
+        TableStoreReq<T> req = new TableStoreReq<T>();
 
-    /**
-     * 构造函数
-     */
-    public <T extends TableStoreEntity> TableStoreReq(Class<T> clz, String fieldName){
         Field field = null;
         try{
-            field = clz.getDeclaredField(fieldName);
+            field = clzParam.getDeclaredField(fieldName);
         }catch (Exception e){
             log.info("找不到字段",e);
         }
 
         //表名
-        TableStore tableStore = clz.getAnnotation(TableStore.class);
+        TableStore tableStore = clzParam.getAnnotation(TableStore.class);
         String tableName = tableStore.tableName();
         //索引
         TableStoreIndex tableStoreIndex = field.getAnnotation(TableStoreIndex.class);
@@ -96,10 +94,12 @@ public class TableStoreReq {
         //字段名转下划线
         fieldName = IStringUtil.camelToUnderline(fieldName);
 
-        this.tableName = tableName;
-        this.indexName = indexName;
-        this.fieldName = fieldName;
-        this.clz = clz;
+        req.tableName = tableName;
+        req.indexName = indexName;
+        req.fieldName = fieldName;
+        req.clz = clzParam;
+
+        return req;
     }
 
     /**
@@ -108,10 +108,12 @@ public class TableStoreReq {
      * @param indexName 索引名
      * @param fieldName 匹配的字段名
      */
-    public TableStoreReq(String tableName,String indexName,String fieldName){
-        this.tableName = tableName;
-        this.indexName = indexName;
-        this.fieldName = fieldName;
+    public static <T extends TableStoreEntity> TableStoreReq<T> build(String tableName,String indexName,String fieldName){
+        TableStoreReq<T> req = new TableStoreReq<T>();
+        req.tableName = tableName;
+        req.indexName = indexName;
+        req.fieldName = fieldName;
+        return req;
     }
 
     /**
@@ -120,7 +122,7 @@ public class TableStoreReq {
      * @param lessThan
      * @return
      */
-    public TableStoreReq range(Object greaterThan,Object lessThan){
+    public TableStoreReq<T> range(Object greaterThan,Object lessThan){
         this.setGreaterThan(greaterThan);
         this.setLessThan(lessThan);
         return this;
@@ -131,12 +133,12 @@ public class TableStoreReq {
      * @param fieldValue
      * @return
      */
-    public TableStoreReq equal(Object fieldValue){
+    public TableStoreReq<T> equal(Object fieldValue){
         this.setFieldValue(fieldValue);
         return this;
     }
 
-    public TableStoreReq page(Integer pageNo,Integer pageCount){
+    public TableStoreReq<T> page(Integer pageNo,Integer pageCount){
         this.pageNo = pageNo;
         this.pageCount = pageCount;
         return this;
@@ -146,7 +148,7 @@ public class TableStoreReq {
      * 是否逆序
      * @return
      */
-    public TableStoreReq isDesc(){
+    public TableStoreReq<T> isDesc(){
         this.setSortByFieldDesc(true);
         return this;
     }
@@ -155,7 +157,7 @@ public class TableStoreReq {
      * 是否需要总数
      * @return
      */
-    public TableStoreReq isNeedTotalCount(){
+    public TableStoreReq<T> isNeedTotalCount(){
         this.setGetTotalCount(true);
         return this;
     }

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

@@ -12,7 +12,7 @@ import java.util.List;
  */
 @Data
 @ToString
-public class TableStoreRes {
+public class TableStoreRes<T extends TableStoreEntity> {
 
     /**
      * 总行数
@@ -24,4 +24,11 @@ public class TableStoreRes {
      */
     private List<Row> rowList;
 
+    /**
+     * 对象列表
+     */
+    private List<T> entityList;
+
+
+
 }

+ 14 - 6
abi-cloud-qr-platform-server/src/test/java/com/abi/qms/platform/TableStorePlusTest.java

@@ -53,13 +53,15 @@ public class TableStorePlusTest {
     //按索引范围匹配
     @Test
     public void queryRowsRange(){
-        //构造入参
-        TableStoreReq req = new TableStoreReq(QrCode.class,"codeIndex")
+        TableStoreReq<QrCode> req = TableStoreReq.build(QrCode.class,"codeIndex")
                 .range(1,100)
                 .page(1,15)
                 .isDesc();
         //调用查询
-        List<QrCode> qrCodeList = tableStorePlusUtils.selectList(req);
+        TableStoreRes<QrCode> res = tableStorePlusUtils.selectList(req);
+
+        List<QrCode> qrCodeList = res.getEntityList();
+
         //打印
         for(QrCode qrCode:qrCodeList){
             log.info(qrCode.toString());
@@ -69,15 +71,21 @@ public class TableStorePlusTest {
     //按索引精确匹配
     @Test
     public void queryRowsEqual(){
-        TableStoreReq req = new TableStoreReq(QrCode.class,"packageId")
+        TableStoreReq<QrCode> req = TableStoreReq.build(QrCode.class,"packageId")
                 .equal("1")
-                .page(1,3);
+                .page(1,3)
+                .isNeedTotalCount();
+
+        TableStoreRes<QrCode> res = tableStorePlusUtils.selectList(req);
+
+        List<QrCode> qrCodeList = res.getEntityList();
 
-        List<QrCode> qrCodeList = tableStorePlusUtils.selectList(req);
         //打印
         for(QrCode qrCode:qrCodeList){
             log.info(qrCode.toString());
         }
+        log.info("total count----------->");
+        log.info(String.valueOf(res.getTotalCount()));
     }
 
 

+ 0 - 21
abi-cloud-qr-platform-server/src/test/java/com/abi/qms/platform/TableStoreTest.java

@@ -97,26 +97,5 @@ 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());
-    }
 
 }