|
@@ -3,6 +3,7 @@ package com.abi.task.common.tablestore;
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
import cn.hutool.core.date.DateUtil;
|
|
import cn.hutool.core.date.DateUtil;
|
|
import com.abi.task.common.api.exception.BusinessException;
|
|
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.TableStore;
|
|
import com.abi.task.common.tablestore.common.TableStoreEntity;
|
|
import com.abi.task.common.tablestore.common.TableStoreEntity;
|
|
import com.abi.task.common.utils.IStringUtil;
|
|
import com.abi.task.common.utils.IStringUtil;
|
|
@@ -18,7 +19,12 @@ import org.springframework.stereotype.Component;
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.Method;
|
|
import java.lang.reflect.Method;
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
-import java.util.*;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Arrays;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 进一步封装的tablestore工具类
|
|
* 进一步封装的tablestore工具类
|
|
@@ -32,19 +38,21 @@ public class TableStorePlusUtils {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 存储单条 重构方法
|
|
* 存储单条 重构方法
|
|
|
|
+ *
|
|
* @param entity
|
|
* @param entity
|
|
*/
|
|
*/
|
|
- public void putRow(TableStoreEntity entity){
|
|
|
|
|
|
+ public void putRow(TableStoreEntity entity) {
|
|
putRow(Arrays.asList(entity));
|
|
putRow(Arrays.asList(entity));
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 存储多行数据(如果主键key相同,则覆盖)
|
|
* 存储多行数据(如果主键key相同,则覆盖)
|
|
|
|
+ *
|
|
* @param entityList
|
|
* @param entityList
|
|
*/
|
|
*/
|
|
- public void putRow(List<TableStoreEntity> entityList){
|
|
|
|
- if(CollectionUtil.isEmpty(entityList)){
|
|
|
|
- throw new BusinessException("入参列表为空");
|
|
|
|
|
|
+ public void putRow(List<TableStoreEntity> entityList) {
|
|
|
|
+ if (CollectionUtil.isEmpty(entityList)) {
|
|
|
|
+ throw new BusinessException(ErrorCodeEnum.ERROR_PARAM.getCode(), "入参列表为空");
|
|
}
|
|
}
|
|
try {
|
|
try {
|
|
String primaryKeyName = null;
|
|
String primaryKeyName = null;
|
|
@@ -52,18 +60,18 @@ public class TableStorePlusUtils {
|
|
List<String> pkValueList = new ArrayList<>();
|
|
List<String> pkValueList = new ArrayList<>();
|
|
List<List<Column>> columnsList = new ArrayList<>();
|
|
List<List<Column>> columnsList = new ArrayList<>();
|
|
|
|
|
|
- for(TableStoreEntity entity:entityList){
|
|
|
|
|
|
+ for (TableStoreEntity entity : entityList) {
|
|
List<Column> columns = new ArrayList<>();
|
|
List<Column> columns = new ArrayList<>();
|
|
//表名
|
|
//表名
|
|
TableStore tableStore = entity.getClass().getAnnotation(TableStore.class);
|
|
TableStore tableStore = entity.getClass().getAnnotation(TableStore.class);
|
|
tableName = tableStore.tableName();
|
|
tableName = tableStore.tableName();
|
|
if (StringUtils.isBlank(tableName)) {
|
|
if (StringUtils.isBlank(tableName)) {
|
|
- throw new BusinessException("存储表名为空");
|
|
|
|
|
|
+ throw new BusinessException(ErrorCodeEnum.ERROR_PARAM.getCode(), "存储表名为空");
|
|
}
|
|
}
|
|
//主键
|
|
//主键
|
|
primaryKeyName = tableStore.primaryKeyName();
|
|
primaryKeyName = tableStore.primaryKeyName();
|
|
if (StringUtils.isBlank(primaryKeyName)) {
|
|
if (StringUtils.isBlank(primaryKeyName)) {
|
|
- throw new BusinessException("存储主键为空");
|
|
|
|
|
|
+ throw new BusinessException(ErrorCodeEnum.ERROR_PARAM.getCode(), "存储主键为空");
|
|
}
|
|
}
|
|
//主键的驼峰转下划线
|
|
//主键的驼峰转下划线
|
|
primaryKeyName = IStringUtil.camelToUnderline(primaryKeyName);
|
|
primaryKeyName = IStringUtil.camelToUnderline(primaryKeyName);
|
|
@@ -77,7 +85,7 @@ public class TableStorePlusUtils {
|
|
String underlineFieldName = IStringUtil.camelToUnderline(fieldName);
|
|
String underlineFieldName = IStringUtil.camelToUnderline(fieldName);
|
|
//获取method
|
|
//获取method
|
|
Method method = entity.getClass().getMethod("get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1));
|
|
Method method = entity.getClass().getMethod("get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1));
|
|
- if (method == null){
|
|
|
|
|
|
+ if (method == null) {
|
|
throw new BusinessException("get方法不存在");
|
|
throw new BusinessException("get方法不存在");
|
|
}
|
|
}
|
|
//反射调用拿到结果
|
|
//反射调用拿到结果
|
|
@@ -87,7 +95,7 @@ public class TableStorePlusUtils {
|
|
primaryKeyVal = (String) fieldValue;
|
|
primaryKeyVal = (String) fieldValue;
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
- if(fieldValue==null){
|
|
|
|
|
|
+ if (fieldValue == null) {
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
//放入table的column
|
|
//放入table的column
|
|
@@ -118,29 +126,30 @@ public class TableStorePlusUtils {
|
|
}
|
|
}
|
|
|
|
|
|
//真实去调用
|
|
//真实去调用
|
|
- if(pkValueList.size()>1){
|
|
|
|
|
|
+ if (pkValueList.size() > 1) {
|
|
tableStoreUtils.batchPutRow(primaryKeyName, pkValueList, tableName, columnsList);
|
|
tableStoreUtils.batchPutRow(primaryKeyName, pkValueList, tableName, columnsList);
|
|
- }else{
|
|
|
|
|
|
+ } else {
|
|
tableStoreUtils.putRow(primaryKeyName, pkValueList.get(0), tableName, columnsList.get(0));
|
|
tableStoreUtils.putRow(primaryKeyName, pkValueList.get(0), tableName, columnsList.get(0));
|
|
}
|
|
}
|
|
|
|
|
|
- }catch (Exception e){
|
|
|
|
- log.info("setRow error",e);
|
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.info("setRow error", e);
|
|
throw new BusinessException("存储table异常");
|
|
throw new BusinessException("存储table异常");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 读取一行数据
|
|
* 读取一行数据
|
|
|
|
+ *
|
|
* @param clz
|
|
* @param clz
|
|
* @param primaryKeyVal
|
|
* @param primaryKeyVal
|
|
* @param <T>
|
|
* @param <T>
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public <T extends TableStoreEntity> T getRow(Class<T> clz,String primaryKeyVal){
|
|
|
|
|
|
+ public <T extends TableStoreEntity> T getRow(Class<T> clz, String primaryKeyVal) {
|
|
T entity = null;
|
|
T entity = null;
|
|
|
|
|
|
- try{
|
|
|
|
|
|
+ try {
|
|
//表名
|
|
//表名
|
|
TableStore tableStore = clz.getAnnotation(TableStore.class);
|
|
TableStore tableStore = clz.getAnnotation(TableStore.class);
|
|
String tableName = tableStore.tableName();
|
|
String tableName = tableStore.tableName();
|
|
@@ -160,9 +169,9 @@ public class TableStorePlusUtils {
|
|
//获取字段的值
|
|
//获取字段的值
|
|
Column[] columns = row.getColumns();
|
|
Column[] columns = row.getColumns();
|
|
//转化为map
|
|
//转化为map
|
|
- Map<String,Object> rowMap = new HashMap<>();
|
|
|
|
- for(Column column:columns){
|
|
|
|
- rowMap.put(column.getName(),column.getValue().getValue());
|
|
|
|
|
|
+ Map<String, Object> rowMap = new HashMap<>();
|
|
|
|
+ for (Column column : columns) {
|
|
|
|
+ rowMap.put(column.getName(), column.getValue().getValue());
|
|
}
|
|
}
|
|
|
|
|
|
//转为对象
|
|
//转为对象
|
|
@@ -178,27 +187,27 @@ public class TableStorePlusUtils {
|
|
//转化完成的值
|
|
//转化完成的值
|
|
Object fieldValue = null;
|
|
Object fieldValue = null;
|
|
//获取对象的set方法
|
|
//获取对象的set方法
|
|
- Method method = clz.getMethod("set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1),fieldType);
|
|
|
|
|
|
+ Method method = clz.getMethod("set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1), fieldType);
|
|
//赛主键
|
|
//赛主键
|
|
if (underlineFieldName.equals(primaryKeyName)) {
|
|
if (underlineFieldName.equals(primaryKeyName)) {
|
|
fieldValue = primaryKeyVal;
|
|
fieldValue = primaryKeyVal;
|
|
- }else{
|
|
|
|
|
|
+ } else {
|
|
//塞其他值
|
|
//塞其他值
|
|
- if(columnVal!=null){
|
|
|
|
|
|
+ if (columnVal != null) {
|
|
if (fieldType == String.class) {
|
|
if (fieldType == String.class) {
|
|
- fieldValue = (String)columnVal;
|
|
|
|
|
|
+ fieldValue = (String) columnVal;
|
|
} else if (fieldType == Integer.class) {
|
|
} else if (fieldType == Integer.class) {
|
|
fieldValue = Integer.valueOf(String.valueOf(columnVal));
|
|
fieldValue = Integer.valueOf(String.valueOf(columnVal));
|
|
} else if (fieldType == Long.class) {
|
|
} else if (fieldType == Long.class) {
|
|
- fieldValue = (Long)columnVal;
|
|
|
|
|
|
+ fieldValue = (Long) columnVal;
|
|
} else if (fieldType == Double.class) {
|
|
} else if (fieldType == Double.class) {
|
|
- fieldValue = (Double)columnVal;
|
|
|
|
|
|
+ fieldValue = (Double) columnVal;
|
|
} else if (fieldType == Boolean.class) {
|
|
} else if (fieldType == Boolean.class) {
|
|
- fieldValue = (Boolean)columnVal;
|
|
|
|
|
|
+ fieldValue = (Boolean) columnVal;
|
|
} else if (fieldType == Date.class) {
|
|
} else if (fieldType == Date.class) {
|
|
- fieldValue = DateUtil.parse((String)columnVal,"yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
+ fieldValue = DateUtil.parse((String) columnVal, "yyyy-MM-dd HH:mm:ss");
|
|
} else if (fieldType == LocalDateTime.class) {
|
|
} else if (fieldType == LocalDateTime.class) {
|
|
- fieldValue = DateUtil.parseLocalDateTime((String)columnVal,"yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
+ fieldValue = DateUtil.parseLocalDateTime((String) columnVal, "yyyy-MM-dd HH:mm:ss");
|
|
} else {
|
|
} else {
|
|
log.info("不识别的字段类型 fieldName=" + fieldName);
|
|
log.info("不识别的字段类型 fieldName=" + fieldName);
|
|
continue;
|
|
continue;
|
|
@@ -207,11 +216,11 @@ public class TableStorePlusUtils {
|
|
}
|
|
}
|
|
|
|
|
|
//反射调用set
|
|
//反射调用set
|
|
- method.invoke(entity,fieldValue);
|
|
|
|
|
|
+ method.invoke(entity, fieldValue);
|
|
}
|
|
}
|
|
|
|
|
|
- }catch (Exception e){
|
|
|
|
- log.info("获取table store异常",e);
|
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.info("获取table store异常", e);
|
|
throw new BusinessException("获取table store异常");
|
|
throw new BusinessException("获取table store异常");
|
|
}
|
|
}
|
|
|
|
|
|
@@ -220,31 +229,32 @@ public class TableStorePlusUtils {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 删除行数据
|
|
* 删除行数据
|
|
|
|
+ *
|
|
* @param clz
|
|
* @param clz
|
|
* @param primaryKeyVal
|
|
* @param primaryKeyVal
|
|
* @param <T>
|
|
* @param <T>
|
|
*/
|
|
*/
|
|
- public <T extends TableStoreEntity> void deleteRow(Class<T> clz,String primaryKeyVal){
|
|
|
|
|
|
+ public <T extends TableStoreEntity> void deleteRow(Class<T> clz, String primaryKeyVal) {
|
|
try {
|
|
try {
|
|
//表名
|
|
//表名
|
|
TableStore tableStore = clz.getAnnotation(TableStore.class);
|
|
TableStore tableStore = clz.getAnnotation(TableStore.class);
|
|
String tableName = tableStore.tableName();
|
|
String tableName = tableStore.tableName();
|
|
if (StringUtils.isBlank(tableName)) {
|
|
if (StringUtils.isBlank(tableName)) {
|
|
- throw new BusinessException("存储表名为空");
|
|
|
|
|
|
+ throw new BusinessException(ErrorCodeEnum.ERROR_PARAM.getCode(), "存储表名为空");
|
|
}
|
|
}
|
|
//主键
|
|
//主键
|
|
String primaryKeyName = tableStore.primaryKeyName();
|
|
String primaryKeyName = tableStore.primaryKeyName();
|
|
if (StringUtils.isBlank(primaryKeyName)) {
|
|
if (StringUtils.isBlank(primaryKeyName)) {
|
|
- throw new BusinessException("存储主键为空");
|
|
|
|
|
|
+ throw new BusinessException(ErrorCodeEnum.ERROR_PARAM.getCode(), "存储主键为空");
|
|
}
|
|
}
|
|
//主键的驼峰转下划线
|
|
//主键的驼峰转下划线
|
|
primaryKeyName = IStringUtil.camelToUnderline(primaryKeyName);
|
|
primaryKeyName = IStringUtil.camelToUnderline(primaryKeyName);
|
|
|
|
|
|
//调用删除
|
|
//调用删除
|
|
- tableStoreUtils.deleteRow(primaryKeyName,primaryKeyVal,tableName);
|
|
|
|
|
|
+ tableStoreUtils.deleteRow(primaryKeyName, primaryKeyVal, tableName);
|
|
|
|
|
|
- }catch (Exception e){
|
|
|
|
- log.info("删除table store异常",e);
|
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.info("删除table store异常", e);
|
|
throw new BusinessException("删除table store异常");
|
|
throw new BusinessException("删除table store异常");
|
|
}
|
|
}
|
|
|
|
|