目标: 根据两个list中每条记录的某个属性是否相同来拼接。
1.首先定义一个字符串
String str = "[{\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":1,\"BILLTYPENAME\":\"一般缴款书\"},\n" +
" {\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":3,\"BILLTYPENAME\":\"退付书(微机)\"},\n" +
" {\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":41,\"BILLTYPENAME\":\"高速公路收费专用票据(500元)\"},\n" +
" {\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":720,\"BILLTYPENAME\":\"单位手写缴款书(本局)\"},\n" +
" {\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":721,\"BILLTYPENAME\":\"单位微机缴款书(本局)\"},\n" +
" {\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":743,\"BILLTYPENAME\":\"收入专用收据(机打)\"}\n" +
"]";
2.然后将str先转换成json格式,然后再转换成list1: 代码如下
ObjectMapper mapper = new ObjectMapper();
List<Map<String,Object>> conList = null;
conList = mapper.readValue(str, List.class);
3.ObjectMapper的使用需要导入jar包或者在pom文件中添加依赖:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.7.1</version>
</dependency>
4.通过mybatis查询表中的数据,用list接收
List<EInvoiceThresholdDTO> list = new ArrayList<EInvoiceThresholdDTO>();
list = eInoviceThresholdMapper.selectAll();
5.然后通过循环嵌套,对比出BILLTYPEID,如果相同则将表中查出的部分属性通过list的put方法添加到list conList中
上完整代码:
@Override
public List<Map<String,Object>> selectAll() {
String str = "[{\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":1,\"BILLTYPENAME\":\"一般缴款书\"},\n" +
" {\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":3,\"BILLTYPENAME\":\"退付书(微机)\"},\n" +
" {\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":41,\"BILLTYPENAME\":\"高速公路收费专用票据(500元)\"},\n" +
" {\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":720,\"BILLTYPENAME\":\"单位手写缴款书(本局)\"},\n" +
" {\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":721,\"BILLTYPENAME\":\"单位微机缴款书(本局)\"},\n" +
" {\"ITEMID\":2,\"ITEMNAME\":\"人大常委办公厅\",\"BILLTYPEID\":743,\"BILLTYPENAME\":\"收入专用收据(机打)\"}\n" +
"]";
List<EInvoiceThresholdDTO> list = new ArrayList<EInvoiceThresholdDTO>();
list = eInoviceThresholdMapper.selectAll();
ObjectMapper mapper = new ObjectMapper();
List<Map<String,Object>> conList = null;
try {
conList = mapper.readValue(str, List.class);
for (int i=0;i<conList.size();i++){
for (int j=0;j<list.size();j++){
if (conList.get(i).get("BILLTYPEID").toString().equals(list.get(j).getBillTypeID().toString())){
conList.get(i).put("AGENCY",list.get(j).getAGENCY());
conList.get(i).put("YEAR",list.get(j).getYEAR());
conList.get(i).put("WRITEUSER",list.get(j).getWRITEUSER());
conList.get(i).put("WRITEDATE",list.get(j).getWRITEDATE());
conList.get(i).put("ISENABLE",list.get(j).getISENABLE());
conList.get(i).put("ISAUTO",list.get(j).getISAUTO());
conList.get(i).put("threshold",list.get(j).getThreshold());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return conList;
}