向List中数据添加实体对象,实体对象最后一个会把之前的内容覆盖

错误的写法:(这样写等于一直在操作同一个对象,对象中的内容都一样)

List<CommissionSystem> cList = new ArrayList<CommissionSystem>();

for (Goods goods : gList) {
             CommissionSystem comSystem = coresult.get(goods.getCatelogId());
             if (comSystem != null) {
                            comSystem .setTenantId(tenantId);
                            comSystem .setCode(code);
                            comSystem .setName(goods.getName());
                            comSystem .setCreateDate(new Date());
                            comSystem .setSn(goods.getCatelogId());
                            comSystem .setGoodsSn(goods.getBarcode());
                            comSystem .setCreateUser(getCurrentUser().getUserName());
                            cList.add(comSystem );
           }
 }

正确的写法:

List<CommissionSystem> cList = new ArrayList<CommissionSystem>();

for (Goods goods : gList) {
             CommissionSystem comSystem = coresult.get(goods.getCatelogId());
             if (comSystem != null) {
                            CommissionSystem cSystem = new CommissionSystem();
                            BeanUtils.copyProperties(comSystem,cSystem, new String[] { "id", "createDate", "modifyDate" });
                            cSystem.setTenantId(tenantId);
                            cSystem.setCode(code);
                            cSystem.setName(goods.getName());
                            cSystem.setCreateDate(new Date());
                            cSystem.setSn(goods.getCatelogId());
                            cSystem.setGoodsSn(goods.getBarcode());
                            cSystem.setCreateUser(getCurrentUser().getUserName());
                            cList.add(cSystem);
           }
 }

猜你喜欢

转载自blog.csdn.net/qq_38410795/article/details/84141016
今日推荐