利用實體bean對象批量數據傳輸處理
需求
現在有兩方數據庫表結構相同,一方A、另一個方B,現想從A處查詢出多個表的數據,傳輸到B地保存起來。
解決方案1
最簡單粗暴的方法就是,查詢出A處相關表的數據封裝到實體對象中,之后放到List集合中,再傳遞給B處,B處再遍歷集合,將數據保存到B處。但是此處的問題是想要再添加一個表的數據時,需要改查詢的代碼還需要改保存的代碼,非常麻煩,所以不建議使用。
方案2
- 新建一個需要准備哪些數據的實體類對象
- 待查詢的貓
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Cat {
private String id;
private String food;
private String weight;
private String height;
}
- 待查詢的狗
@Data
@AllArgsConstructor
public class Dog {
private String id;
private String food;
private String weight;
private String height;
}
- 待查詢的豬
@Data
@AllArgsConstructor
public class Pig {
private String id;
private String food;
private String weight;
private String height;
private String pid;
}
- 自定義傳輸實體對象,這里定義了需要查詢那些集合對象
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CustomDataTransferDTO{
/**
* ===============================================================
* 數據查詢結果
* ===============================================================
*/
/**
* 待查詢的貓信息
*/
private List<Cat> catList;
/**
* 待查詢的狗信息 通過注解來明確關聯關系
*/
@CustomAnnotation.connectTable(tablePath = "com.study.customdatatransfer.Pig")
private List<Dog> dogList;
/**
* 待查詢的豬信息
*/
@Ignore
private List<Pig> pigList;
- 新建參數關系類
- 公共參數關系類
/**
* 這里為共通參數信息設置
* @author jieya
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonParameterDTO {
/**
* ===============================================================
* 這里配置所有集合查詢的公共查詢條件
* ===============================================================
*/
/**
* 主鍵信息
*/
public String id;
}
- 自定義查詢參數
/**
* 自定義查詢條件及關聯表信息查詢實體對象
* @author Administrator
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TableAndParamsDTO {
/**
* 主表名 這里是查詢那個實體對象的數據,這里的table值一定要和CustomDataTransferDTO中的List的泛型對上
*/
@CustomAnnotation.Table
private String table;
/**
* ===============================================================
* 自定義參數
* ===============================================================
*/
/**
* 自定義查詢參數 search 標記這是一個查詢參數
*/
@CustomAnnotation.search
private String food;
/**
* connectSearchTerm(term = "id") 這個標記為這是連表查詢的副表,主表的id等於副表的pid
*/
@CustomAnnotation.connectSearchTerm(term = "id")
private String pid;
}
- 新建自定義處理主方法
/**
* 自定義數據處理主方法
*
* @author Administrator
*/
public class CustomDataMain {
private static final List<Cat> catList = new ArrayList<>();
private static final List<Dog> dogList = new ArrayList<>();
private static final List<Pig> pigList = new ArrayList<>();
private static List<TableAndParamsDTO> tableAndParamsList = new ArrayList();
private static CommonParameterDTO commonParameter = new CommonParameterDTO();
static {
catList.add(new Cat("1", "面包1", "10", "12"));
catList.add(new Cat("2", "面包2", "10", "12"));
catList.add(new Cat("3", "面包3", "10", "12"));
catList.add(new Cat("4", "面包4", "10", "12"));
dogList.add(new Dog("1", "米飯1", "10", "12"));
dogList.add(new Dog("2", "米飯2", "10", "12"));
dogList.add(new Dog("3", "米飯3", "10", "12"));
dogList.add(new Dog("4", "米飯4", "10", "12"));
pigList.add(new Pig("1", "麻辣燙1", "10", "12", "1"));
pigList.add(new Pig("2", "麻辣燙2", "10", "12", "2"));
pigList.add(new Pig("3", "麻辣燙3", "10", "12", "3"));
pigList.add(new Pig("4", "麻辣燙4", "10", "12", "4"));
}
public static void main(String[] args) throws Exception {
// 共通參數
commonParameter.setId("1");
//
TableAndParamsDTO tableAndParamsDTO = new TableAndParamsDTO();
tableAndParamsDTO.setTable("Pig");
tableAndParamsDTO.setFood("麻辣燙1");
tableAndParamsDTO.setPid("id");
tableAndParamsList.add(tableAndParamsDTO);
findCustomData(CustomDataTransferDTO.class);
}
public static Object findCustomData(Class<?> clazz) throws Exception {
// 實例化數據傳輸類
Object obj = clazz.newInstance();
// 首先得到pojo所定義的字段
Field[] fields = clazz.getDeclaredFields();
for (Field curField : fields) {
// 設置字段可訪問(必須,否則報錯)
curField.setAccessible(true);
// 如果
if (!curField.isAnnotationPresent(Ignore.class)) {
CustomAnnotation.connectTable annotation = curField.getAnnotation(CustomAnnotation.connectTable.class);
String sideTablePath = null;
if (annotation != null) {
sideTablePath = annotation.tablePath();
}
Class<?> curFieldType = curField.getType();
// 集合List元素
if (curFieldType.equals(List.class)) {
// 當前集合的泛型類型
Type genericType = curField.getGenericType();
if (null == genericType) {
continue;
}
if (genericType instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) genericType;
// 得到泛型里的class類型對象
Class<?> actualTypeArgument = (Class<?>) pt.getActualTypeArguments()[0];
// 獲取完整路徑信息
String tablePath = actualTypeArgument.getName();
// 獲取實體對象名稱
String tableName = actualTypeArgument.getSimpleName();
// 獲取該實體對象設置的自定義信息
TableAndParamsDTO tableAndParams = tableAndParamsList.stream().filter(o -> o.getTable().equals(tableName)).findAny().orElse(null);
// 拼接hql和執行獲取數據
obj = connectSqlAndExexute(obj, clazz, tablePath, tableAndParams, sideTablePath);
}
} else {
System.out.println(curField.getName() + "--暫不支持的類型--" + curFieldType.getSimpleName());
}
} else {
System.out.println("Ignore----");
}
}
return null;
}
/**
* 連接sql並獲取數據
*
* @param obj
* @param clazz
* @param tablePath
* @param tableAndParams
* @param sideTablePath
* @return
* @throws Exception
*/
private static Object connectSqlAndExexute(Object obj, Class<?> clazz, String tablePath,
TableAndParamsDTO tableAndParams, String sideTablePath) throws Exception {
int lastIndex = tablePath.lastIndexOf(".");
String tableName = tablePath.substring(lastIndex + 1);
List<Object> param = new ArrayList<>();
// 查詢語句
StringBuilder selectBuilder = new StringBuilder(" select * from " + tableName + " where 1=1");
// 查詢條件
StringBuilder whereBuilder = new StringBuilder();
// 拼接共通參數
if (commonParameter != null) {
// 拼接共通參數
Field[] fields = commonParameter.getClass().getDeclaredFields();
for (Field curField : fields) {
// 設置字段可訪問(必須,否則報錯)
curField.setAccessible(true);
String name = curField.getName();
whereBuilder.append(" and " + name + "=?");
Object vlaue = ReflectionUtil.getVlaue(commonParameter, name, "");
param.add(vlaue);
}
}
// 如果設置了表和特殊參數則按照特殊情況處理,否則使用共通參數拼接條件
if (tableAndParams != null) {
// 遍歷該實體對象設置的配置信息
// 獲取主表
String table = tableAndParams.getTable();
// 拼接自定義經營范圍
Field[] fields = tableAndParams.getClass().getDeclaredFields();
for (Field field : fields) {
// 判斷是否為查詢條件
if (field.isAnnotationPresent(CustomAnnotation.search.class)) {
whereBuilder.append(" and " + field.getName() + "=?");
Object vlaue = ReflectionUtil.getVlaue(tableAndParams, field.getName(), "");
param.add(vlaue);
}
// 關聯查詢
if (field.isAnnotationPresent(CustomAnnotation.connectSearchTerm.class)) {
String name = field.getName();
String values = GsUtils.blankNull(ReflectionUtil.getVlaue(tableAndParams, name, ""));
String[] split = values.split(",");
String sideWhere = "";
for (int i = 0; i < split.length; i++) {
sideWhere += " and " + name + " in(";
sideWhere += "'" + split[i] + "'" + ",";
}
;
sideWhere = sideWhere.substring(0, sideWhere.length() - 1);
sideWhere += " )";
whereBuilder.append(sideWhere);
}
}
}
// 獲取查詢對象的class對象
Class tableClazz = Class.forName(tablePath);
// hql不為空和hql中不包含and符號時,禁止執行sql,防止全庫掃描
if (StringUtils.isEmpty(whereBuilder.toString())) {
throw new Exception("hql錯誤,因不存在and查詢條件,會導致全庫掃描" + selectBuilder.toString());
}
// TODO 執行sql 將查詢到數據封裝到list<bean>對象中
// List list = baseDao.findByHql(selectBuilder.toString()+whereBuilder.toString(),tableClazz,param);
// TODO 這段代碼為無用的為獲取數據的代碼
List list = findDataInfo(tableName,whereBuilder,param);
// 將查詢到的信息添加到傳輸文件實體對象中
if (list != null && list.size() > 0) {
obj = ReflectionUtil.setValue(obj, clazz, tableName, list);
}
// 連表查詢
if (sideTablePath != null) {
String sideTableName = Class.forName(sideTablePath).getSimpleName();
// 獲取該實體對象設置的自定義信息
TableAndParamsDTO sideTableAndParams = tableAndParamsList.stream().filter(o -> o.getTable().equals(sideTableName)).findAny().orElse(null);
// 拼接自定義經營范圍
Field[] sideFields = sideTableAndParams.getClass().getDeclaredFields();
for (Field field : sideFields) {
// 關聯查詢
if (field.isAnnotationPresent(CustomAnnotation.connectSearchTerm.class)) {
String term = field.getAnnotation(CustomAnnotation.connectSearchTerm.class).term();
String sideParam = "";
for (Object obj1 : list) {
Object value = ReflectionUtil.getVlaue(obj1, (String) term, "");
if (value != null) {
sideParam += value + ",";
}
}
if (StringUtils.isEmpty(sideParam)) {
throw new Exception("關聯表但為獲取到關聯條件信息" + selectBuilder.toString());
}
// 將值設置到對象中
field.setAccessible(true);
field.set(sideTableAndParams, sideParam);
}
}
// 拼接hql和執行獲取數據
obj = connectSqlAndExexute(obj, clazz, sideTablePath, sideTableAndParams, null);
}
System.out.println("tableAndParams:" + tableAndParams + "commonParams:" + commonParameter + "執行sql語句:" + selectBuilder.toString() + whereBuilder.toString() + "查詢條件:" + param + "查詢結果:" + list);
return obj;
}
private static List findDataInfo(String tableName, StringBuilder whereBuilder, List<Object> param) {
List<Object> list = new ArrayList<Object>();
if("Cat".equals(tableName)){
list.add(catList.get(0));
return list;
}
if("Dog".equals(tableName)){
list.add(dogList.get(0));
return list;
}
return list;
}
}
執行完成之后,就可以獲取到咱們需要的數據了。
4.獲取到數據后,發送給另一端,進行解析保存
/**
* 保存待處理數據
*
* @param obj
*/
@Override
public void saveOtherInfo(Object obj) throws Exception {
// 首先得到pojo所定義的字段
Field[] fields = obj.getClass().getDeclaredFields();
for (Field curField : fields) {
// 設置字段可訪問(必須,否則報錯)
curField.setAccessible(true);
Class<?> curFieldType = curField.getType();
// 集合List元素
if (curFieldType.equals(List.class)) {
// 當前集合的泛型類型
Type genericType = curField.getGenericType();
if (null == genericType) {
continue;
}
if (genericType instanceof ParameterizedType) {
Object object = ReflectionUtil.getVlaue(obj,(String) curField.getName(),"");
if(object!=null){
List list = (List)object;
for (int i=0;i<list.size();i++){
Object o = list.get(i);
// baseDao.saveOrUpdate(o);
}
}
}
}else{
System.out.println((curField.getName() + "--暫不支持的類型--" + curFieldType.getSimpleName()));
}
}
}
這樣兩端進行數據傳輸就完成了,或中間使用消息中間件進行傳輸也是可以的。
對於上面的有什么不懂或者有疑問的可以聯系qq:1197852132