近期遇到一个问题,就是当 添加、修改、删除 三个业务,都写入controller(控制层)同一个方法时,该怎么区分呢?虽然前端可以加上操作类型,但是后台还得做遍历,效率不高而且比较麻烦。于是乎,我就想了一个方法,在这里跟大家分享一下。
- 原理:
a、添加的数据一般uuid为空;
b、修改的数据拥有uuid;
c、不变的数据toString()结果是相同的;
因此,又以上a、b、c三项可以得出如下公式:
伪新增的数据 【包含修改的数据】= 新数据 - (新数据 ∩老数据);
伪删除的数据 【包含修改的数据】= 老数据 - (老数据 ∩新数据);
更新的数据 = 伪新增数据 ∩ 伪删除数据;
新增的数据 = 伪新增数据 - 更新数据;
删除的数据 = 伪删除的数据 - 更新数据;
2、代码实现
A、OperationAides.java
package com.cerno.opration_helper.base;
/**
*
* Copyright: Copyright (c) 2019 Jun_Zhou
*
* @ClassName: OperationAides.java
* @Description: 描述操作助手的抽象类,当对象需要实现操作辨别帮助的时候需要集成该类;
*
* @version: v1.0.0
* @author: JunZhou
* @Email: 1769676159@qq.com
* @Site: CERNO
* @date: 2019年3月23日 下午1:57:31
*/
public abstract class OperationAides
{
/**
* 定义未发生变更的数据;
* @param obj
* @return
*/
public abstract boolean equals4Fixed(Object obj);
/**
* 定义发生修改的数据;
* @param obj
* @return
*/
public abstract boolean equals4Updated(Object obj);
}
B、OperationHelper.java
package com.cerno.opration_helper.base;
import java.util.List;
public interface OperationHelper<T>
{
/**
* 获取两个list中的公共部分;
*/
List<T> remainAll(List<T> sourceDataList, List<T> targetDataList);
/**
* 从源集合中移除指定的集合;
* @param sourceDataList:源集合;
* @param targetDataList:目标集合[被移除的集合];
* @return
*/
List<T> removeAll(List<T> sourceDataList, List<T> targetDataList);
/**
* 从源集合中移除所有被更新的集合;
* @param sourceDataList:源集合;
* @param targetDataList:目标集合[被移除的集合];
* @return
*/
List<T> removeAllUpdated(List<T> sourceDataList, List<T> targetDataList);
/**
* 获取伪新增数据,包含修改部分;
* @return
*/
List<T> getFakeAddPart();
/**
* 获取伪删除部分数据,包含修改部分;
* @return
*/
List<T> getFakeDelPart();
/**
* 获取发生修改的数据集合;
* @return
*/
List<T> getUpdatedPart();
/**
* 获取新数据中的新增部分;
* @param oldList:老数据;
* @param newList:最新数据;
* @return :数据中的新增部分;
* 新增部分 = 新数据 - (新数据∩老数据)
*/
List<T> getAddPart();
/**
* 获取老数据中的删除部分;
* @param oldList:老数据;
* @param newList:最新数据;
* @return :数据中的新增部分;
* 删除部分 = 新数据 - (新数据∩老数据)
*/
List<T> getDelPart();
}
C、ComplexOperationHelper.java
package com.cerno.opration_helper;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import com.cerno.opration_helper.base.OperationAides;
import com.cerno.opration_helper.base.OperationHelper;
/**
*
* Copyright: Copyright (c) 2019 Jun_Zhou
*
* @ClassName: ComplexOperationHelper.java
* @Description: 复杂操作助手类;
*
* @version: v1.0.0
* @author: JunZhou
* @Email: 1769676159@qq.com
* @Site: CERNO
* @date: 2019年3月23日 下午1:49:09
*/
public class ComplexOperationHelper<T extends OperationAides> implements OperationHelper<T>
{
//老数据;
protected List<T> oldDataList = new ArrayList<T>();
//新数据;
protected List<T> newDataList = new ArrayList<T>();
/**
* 构造函数;
* @param oldDataList:老数据;
* @param newDataList:新数据;
*/
public ComplexOperationHelper(List<T> oldDataList, List<T> newDataList)
{
this.oldDataList = oldDataList;
this.newDataList = newDataList;
}
public List<T> remainAll(List<T> sourceDataList, List<T> targetDataList)
{
//执行数据的深度复制;
List<T> sourceList = new ArrayList<T>(sourceDataList);
List<T> targetList = new ArrayList<T>(targetDataList);
//执行逻辑运算;
List<T> remainedList = sourceList.stream().filter(sourceEle -> {
boolean isUpdated = false;
for (T t : targetList)
{
if (sourceEle.equals4Updated(t))
{
isUpdated = true;
break;
}
}
return isUpdated;
}).collect(Collectors.toList());
return remainedList;
}
@Override
public List<T> removeAll(List<T> sourceDataList, List<T> targetDataList)
{
//执行数据的深度复制;
List<T> sourceList = new ArrayList<T>(sourceDataList);
List<T> targetList = new ArrayList<T>(targetDataList);
//执行逻辑运算;
sourceList.removeIf(sourceEle -> {
for (T t : targetList)
{
if (sourceEle.equals4Fixed(t)) { return true; } //equals4OpHelperFakeCD=>定义未发生变化的数据;
}
return false;
});
List<T> listAfterRemoved = new ArrayList<T>(sourceList);
return listAfterRemoved;
}
@Override
public List<T> removeAllUpdated(List<T> sourceDataList, List<T> targetDataList)
{
//执行数据的深度复制;
List<T> sourceList = new ArrayList<T>(sourceDataList);
List<T> targetList = new ArrayList<T>(targetDataList);
//执行逻辑运算;
sourceList.removeIf(sourceEle -> {
for (T t : targetList)
{
if (sourceEle.equals4Updated(t)) { return true; }
}
return false;
});
List<T> listAfterRemoved = new ArrayList<T>(sourceList);
return listAfterRemoved;
}
@Override
public List<T> getFakeAddPart()
{
//执行数据的深度复制;
List<T> oldDataListCopy = new ArrayList<T>(this.oldDataList);
List<T> newDataListCopy = new ArrayList<T>(this.newDataList);
//获取伪新增的集合;
List<T> fakeAddPart = this.removeAll(newDataListCopy, oldDataListCopy);
return fakeAddPart;
}
@Override
public List<T> getFakeDelPart()
{
//执行数据的深度复制;
List<T> oldDataListCopy = new ArrayList<T>(this.oldDataList);
List<T> newDataListCopy = new ArrayList<T>(this.newDataList);
//获取伪删除的集合;
List<T> fakeDelPart = this.removeAll(oldDataListCopy, newDataListCopy);
return fakeDelPart;
}
@Override
public List<T> getAddPart()
{
//执行数据的深度复制;
List<T> fakeAddPartCopy = new ArrayList<T>(this.getFakeAddPart());
List<T> updatePartCopy = new ArrayList<T>(this.getUpdatedPart());
//获取新增数据的集合;
List<T> addPart = this.removeAllUpdated(fakeAddPartCopy, updatePartCopy);
return addPart;
}
@Override
public List<T> getDelPart()
{
//执行数据的深度复制;
List<T> fakeDelPartCopy = new ArrayList<T>(this.getFakeDelPart());
List<T> updatePartCopy = new ArrayList<T>(this.getUpdatedPart());
//获取被删除的集合数据;
List<T> delPart = this.removeAllUpdated(fakeDelPartCopy, updatePartCopy);
return delPart;
}
@Override
public List<T> getUpdatedPart()
{
//执行数据的深度复制;
List<T> fakeAddPart = new ArrayList<T>(this.getFakeAddPart());
List<T> fakeDelPart = new ArrayList<T>(this.getFakeDelPart());
//获取被更新的数据的集合;
List<T> remainedList = this.remainAll(fakeAddPart, fakeDelPart);
return remainedList;
}
}
D、模拟数据
Simulation.java
package com.cerno.data;
import java.util.ArrayList;
import java.util.List;
import com.cerno.model.People;
/**
*
* Copyright: Copyright (c) 2019 Jun_Zhou
*
* @ClassName: Simulation.java
* @Description: 模拟数据;
*
* @version: v1.0.0
* @author: JunZhou
* @Email: 1769676159@qq.com
* @Site: CERNO
* @date: 2019年3月20日 下午9:56:13
*/
public class Simulation
{
/**
* 获取新的模拟数据;
* @return
*/
public static List<People> getNewSimulationDatas() {
List<People> PeopleList = new ArrayList<People>();
People People1 = new People("1", "People12", "1", "tinghua1", "man1");
People People2 = new People("2", "People2", "2", "tinghua2", "man2");
People People3 = new People("3", "People3", "3", "tinghua3", "man3");
People People4 = new People("4", "People4", "4", "tinghua4", "man4");
People People5 = new People("5", "People5", "5", "tinghua65", "man5");
//People People6 = new People("6", "People6", "6", "tinghua6", "man6");
//People People7 = new People("7", "People7", "7", "tinghua7", "man7");
//People People8 = new People("8", "People8", "8", "tinghua8", "man8");
People People9 = new People("9", "People9", "9", "tinghua9", "man9");
People People10 = new People("10", "People10", "10", "tinghua10", "man10");
People People11 = new People("11", "People11", "11", "tinghua11", "man11");
People People12 = new People("", "People12", "12", "tinghua12", "man12");
People People13 = new People("", "People13", "13", "tinghua13", "man13");
People People14 = new People("", "People14", "14", "tinghua14", "man14");
PeopleList.add(People10);
PeopleList.add(People9);
//PeopleList.add(People8);
//PeopleList.add(People7);
//PeopleList.add(People6);
PeopleList.add(People5);
PeopleList.add(People4);
PeopleList.add(People3);
PeopleList.add(People2);
PeopleList.add(People1);
PeopleList.add(People11);
PeopleList.add(People12);
PeopleList.add(People13);
PeopleList.add(People14);
return PeopleList;
}
/**
* 获取老的模拟数据;
* @return
*/
public static List<People> getOldSimulationDatas() {
List<People> PeopleList = new ArrayList<People>();
People People1 = new People("1", "People1", "1", "tinghua1", "man1");
People People2 = new People("2", "People2", "2", "tinghua2", "man2");
People People3 = new People("3", "People3", "3", "tinghua3", "man3");
People People4 = new People("4", "People4", "4", "tinghua4", "man4");
People People5 = new People("5", "People5", "5", "tinghua5", "man5");
People People6 = new People("6", "People6", "6", "tinghua6", "man6");
People People7 = new People("7", "People7", "7", "tinghua7", "man7");
People People8 = new People("8", "People8", "8", "tinghua8", "man8");
People People9 = new People("9", "People9", "9", "tinghua9", "man9");
People People10 = new People("10", "People10", "10", "tinghua10", "man10");
PeopleList.add(People10);
PeopleList.add(People9);
PeopleList.add(People8);
PeopleList.add(People7);
PeopleList.add(People6);
PeopleList.add(People5);
PeopleList.add(People4);
PeopleList.add(People3);
PeopleList.add(People2);
PeopleList.add(People1);
return PeopleList;
}
}
E、测试实体
People.java
package com.cerno.model;
import com.cerno.opration_helper.base.OperationAides;
/**
*
* Copyright: Copyright (c) 2019 Jun_Zhou
*
* @ClassName: people.java
* @Description: 描述人员的实体类;
*
* @version: v1.0.0
* @author: JunZhou
* @Email: 1769676159@qq.com
* @Site: CERNO
* @date: 2019年3月23日 下午2:00:23
*/
public class People extends OperationAides
{
private String uuid;
private String name;
private String age;
private String school;
private String sex;
public People(String uuid, String name, String age, String school, String sex)
{
this.uuid = uuid;
this.name = name;
this.age = age;
this.school = school;
this.sex = sex;
}
public String getUuid()
{
return uuid;
}
public void setUuid(String uuid)
{
this.uuid = uuid;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAge()
{
return age;
}
public void setAge(String age)
{
this.age = age;
}
public String getSchool()
{
return school;
}
public void setSchool(String school)
{
this.school = school;
}
public String getSex()
{
return sex;
}
public void setSex(String sex)
{
this.sex = sex;
}
@Override
public boolean equals4Fixed(Object obj)
{
People object = (People) obj;
return this.toString().equals(object.toString());
}
@Override
public boolean equals4Updated(Object obj)
{
People object = (People) obj;
String objectUuid = object.getUuid();
String currentUuid = this.uuid;
if (((!"".equals(objectUuid)) && (objectUuid != null)) && ((!"".equals(currentUuid)) && (currentUuid != null)))
{
if (objectUuid.equals(currentUuid)) { return true; }
}
return false;
}
@Override
public String toString()
{
return "Person [uuid=" + uuid + ", name=" + name + ", age=" + age + ", school=" + school + ", sex=" + sex + "]";
}
}
F、测试客户端
TestClient.java
package com.cerno.test;
import java.util.List;
import com.cerno.data.Simulation;
import com.cerno.model.People;
import com.cerno.opration_helper.ComplexOperationHelper;
/**
*
* Copyright: Copyright (c) 2019 Jun_Zhou
*
* @ClassName: TestClient.java
* @Description: 测试客户端;
*
* @version: v1.0.0
* @author: JunZhou
* @Email: 1769676159@qq.com
* @Site: CERNO
* @date: 2019年3月20日 下午9:48:04
*/
public class TestClient
{
public static void main(String[] args)
{
//原始数据的集合;
List<People> oldPeopleList = Simulation.getOldSimulationDatas();
//最新数据的集合;
List<People> newPeopleList = Simulation.getNewSimulationDatas();
ComplexOperationHelper<People> complexOperationHelper = new ComplexOperationHelper<People>(oldPeopleList, newPeopleList);
//获取新增部分【修改+真新增】;
List<People> addPart = complexOperationHelper.getAddPart();
//获取删除部分【修改+真删除】;
List<People> delPart = complexOperationHelper.getDelPart();
//获取更新部分;
List<People> updatedPart = complexOperationHelper.getUpdatedPart();
System.out.println("---------------Add-Part--------------");
addPart.forEach(oldEle -> {
System.out.println(oldEle.getUuid() + "------>" + oldEle.getName());
});
System.out.println("---------------Del-Part--------------");
delPart.forEach(oldEle -> {
System.out.println(oldEle.getUuid() + "------>" + oldEle.getName());
});
System.out.println("---------------Update-Part--------------");
updatedPart.forEach(oldEle -> {
System.out.println(oldEle.getUuid() + "------>" + oldEle.getName());
});
}
}
3、使用步骤
1、实体类继承OperationAides类,并重写equals4Fixed和equals4Updated以及生成对象的toString()方法;
2、使用助手工具类获取增加、修改、删除部分的数据集合;
//原始数据的集合;
List<People> oldPeopleList = Simulation.getOldSimulationDatas();
//最新数据的集合;
List<People> newPeopleList = Simulation.getNewSimulationDatas();
ComplexOperationHelper<People> complexOperationHelper = new ComplexOperationHelper<People>(oldPeopleList, newPeopleList);
//获取新增部分【修改+真新增】;
List<People> addPart = complexOperationHelper.getAddPart();
//获取删除部分【修改+真删除】;
List<People> delPart = complexOperationHelper.getDelPart();
//获取更新部分;
List<People> updatedPart = complexOperationHelper.getUpdatedPart();
开源地址:https://github.com/JunZhou2016/ComplexOprationHelper.git