在写项目时遇到从数据库取出一堆关于评论的信息,在Service层中进行数据处理,最终想以List<List<Map<>>>>形式进行解析交互。
先看我正确的代码,之后在放出遇到的坑,
package com.platebbg.product.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.platebbg.product.dao.CommentProductDao;
import com.platebbg.product.dao.dto.ProductComment;
import com.platebbg.product.service.CommentProductService;
@Service
public class CommentProductServiceImpl implements CommentProductService{
@Autowired
private CommentProductDao commentProductDao;
@Override
public List< List<Map<String, Object>>> commentproductInfo(int plateId) {
List< List<Map<String, Object>>> result=new ArrayList<>();
List<Map<String, Object>> resultlist=new ArrayList<>();
List<ProductComment> commentlist=commentProductDao.getCommentByplateId(plateId);
int temp=commentlist.get(0).getCommentid();
for(int i=0;i<commentlist.size();i++){
ProductComment productcomment =commentlist.get(i);
Map<String,Object> productresult=new HashMap<>();
Integer commentId=productcomment.getCommentid();
System.out.println(commentId);
System.out.println(temp);
if(commentId==temp){
System.out.println("11111111111");
productresult.put("commentId", commentId);
productresult.put("commentlevel", productcomment.getCommentlevel());
productresult.put("commenttitle", productcomment.getCommenttitle());
productresult.put("commentuser", productcomment.getCommentuser());
productresult.put("commentphone", productcomment.getCommentphone());
productresult.put("commenttime", productcomment.getCommenttime());
productresult.put("comsubId",productcomment.getComsubid());
productresult.put("comsubcontent", productcomment.getComsubcontent());
productresult.put("comsubimg", productcomment.getComsubimg());
productresult.put("comsubuser",productcomment.getComsubuser() );
productresult.put("comsubphone", productcomment.getComsubphone());
productresult.put("comsubtime", productcomment.getComsubtime());
resultlist.add(productresult);
}
else{
System.out.println("222222222222");
result.add(resultlist);
resultlist=new ArrayList<>();
productresult.put("commentId", commentId);
productresult.put("commentlevel", productcomment.getCommentlevel());
productresult.put("commenttitle", productcomment.getCommenttitle());
productresult.put("commentuser", productcomment.getCommentuser());
productresult.put("commentphone", productcomment.getCommentphone());
productresult.put("commenttime", productcomment.getCommenttime());
productresult.put("comsubId",productcomment.getComsubid());
productresult.put("comsubcontent", productcomment.getComsubcontent());
productresult.put("comsubimg", productcomment.getComsubimg());
productresult.put("comsubuser",productcomment.getComsubuser() );
productresult.put("comsubphone", productcomment.getComsubphone());
productresult.put("comsubtime", productcomment.getComsubtime());
resultlist.add(productresult);
temp=commentId;
}
}
result.add(resultlist);
return result;
}
}
不知大家主要到没有,我这使用的
resultlist=new ArrayList<>();
是重新new一个对象,在之前我想着不要重复创建新对象,便使用了
resultlist.clear();
来进行清除处理,但是传到页面上的数据会都是一样的数据,我反复测验,表示在传到Service层的数据没有错误,
之后终于发现list里面放的是同一个对象map的堆地址,指向的都是同一个对象
所以就算你clear之后,你之前的result.add中存放进去的ArrayList的对象地址并没有改变,就是说你clear之后在放进去值,之前已经放入result中的ArrayList也会跟着改变,
最后之后变成都是最后改变的resultlist的值。
这是我在开发评论模块中遇到的问题,不喜勿喷。