Browse Source

进一步封装tableStore,使开发更关注业务

tanzhongran 4 years ago
parent
commit
20f34c337c

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

@@ -8,6 +8,7 @@ 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.Row;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -16,9 +17,7 @@ import org.springframework.stereotype.Component;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.time.LocalDateTime;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
+import java.util.*;
 
 /**
  * 进一步封装的tablestore工具类
@@ -33,7 +32,6 @@ public class TableStorePlusUtils {
     /**
      * 存储一行数据(如果主键key相同,则覆盖)
      * @param entity
-     * @throws IllegalAccessException
      */
     public void putRow(TableStoreEntity entity){
         try {
@@ -102,4 +100,124 @@ public class TableStorePlusUtils {
         }
     }
 
+    /**
+     * 读取一行数据
+     * @param clz
+     * @param primaryKeyVal
+     * @param <T>
+     * @return
+     */
+    public <T extends TableStoreEntity> T getRow(Class<T> clz,String primaryKeyVal){
+        T entity = null;
+
+        try{
+            //表名
+            TableStore tableStore = clz.getAnnotation(TableStore.class);
+            String tableName = tableStore.tableName();
+            if (StringUtils.isBlank(tableName)) {
+                throw new BusinessException("存储表名为空");
+            }
+            //主键
+            String primaryKeyName = tableStore.primaryKeyName();
+            if (StringUtils.isBlank(primaryKeyName)) {
+                throw new BusinessException("存储主键为空");
+            }
+            //主键的驼峰转下划线
+            primaryKeyName = IStringUtil.camelToUnderline(primaryKeyName);
+
+            //调用tablestore查询到数据
+            Row row = tableStoreUtils.getRow(primaryKeyName, primaryKeyVal, tableName);
+            //获取字段的值
+            Column[] columns = row.getColumns();
+            //转化为map
+            Map<String,Object> rowMap = new HashMap<>();
+            for(Column column:columns){
+                rowMap.put(column.getName(),column.getValue().getValue());
+            }
+
+            //转为对象
+            entity = clz.newInstance();
+            //循环塞值
+            Field[] fields = entity.getClass().getDeclaredFields();
+            for (Field field : fields) {
+                String fieldName = field.getName();
+                String underlineFieldName = IStringUtil.camelToUnderline(fieldName);
+                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;
+                        }
+                    }
+                }
+
+                //反射调用set
+                method.invoke(entity,fieldValue);
+            }
+
+        }catch (Exception e){
+            log.info("获取table store异常",e);
+            throw new BusinessException("获取table store异常");
+        }
+
+        return entity;
+    }
+
+    /**
+     * 删除行数据
+     * @param clz
+     * @param primaryKeyVal
+     * @param <T>
+     */
+    public <T extends TableStoreEntity> void deleteRow(Class<T> clz,String primaryKeyVal){
+        try {
+            //表名
+            TableStore tableStore = clz.getAnnotation(TableStore.class);
+            String tableName = tableStore.tableName();
+            if (StringUtils.isBlank(tableName)) {
+                throw new BusinessException("存储表名为空");
+            }
+            //主键
+            String primaryKeyName = tableStore.primaryKeyName();
+            if (StringUtils.isBlank(primaryKeyName)) {
+                throw new BusinessException("存储主键为空");
+            }
+            //主键的驼峰转下划线
+            primaryKeyName = IStringUtil.camelToUnderline(primaryKeyName);
+
+            //调用删除
+            tableStoreUtils.deleteRow(primaryKeyName,primaryKeyVal,tableName);
+
+        }catch (Exception e){
+            log.info("删除table store异常",e);
+            throw new BusinessException("删除table store异常");
+        }
+
+    }
+
 }

+ 10 - 0
abi-cloud-qr-platform-server/src/test/java/com/abi/qms/platform/TableStorePlusTest.java

@@ -2,6 +2,7 @@ package com.abi.qms.platform;
 import java.time.LocalDateTime;
 
 import com.abi.task.common.tablestore.TableStorePlusUtils;
+import com.abi.task.common.tablestore.common.TableStoreEntity;
 import com.abi.task.common.tablestore.entity.DemoEntity;
 import lombok.extern.slf4j.Slf4j;
 import org.junit.jupiter.api.Test;
@@ -27,9 +28,18 @@ public class TableStorePlusTest {
         entity.setIsDog(false);
 
         tableStorePlusUtils.putRow(entity);
+    }
 
+    @Test
+    public void testGetRow(){
+        DemoEntity entity = tableStorePlusUtils.getRow(DemoEntity.class, "10293827182717");
+        log.info(entity.toString());
     }
 
+    @Test
+    public void testDeleteRow(){
+        tableStorePlusUtils.deleteRow(DemoEntity.class, "10293827182717");
+    }
 
 
 }