订单分箱需求,我把它简化为如下模型:
一张表实现,实现分箱的效果,总结一下做个小demo。
package com.kaspar.order.model; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name = "Orders") public class Order implements Serializable { /** * */ private static final long serialVersionUID = 1L; @Id @GeneratedValue(generator = "orderid") @GenericGenerator(name = "orderid", strategy = "native") private int orderid; private String ordernum; public String getOrdernum() { return ordernum; } public void setOrdernum(String ordernum) { this.ordernum = ordernum; } @Column(length = 100, name = "item") private String item; private String box; private String quality; public int getOrderid() { return orderid; } public void setOrderid(int orderid) { this.orderid = orderid; } public String getItem() { return item; } public void setItem(String item) { this.item = item; } public String getBox() { return box; } public void setBox(String box) { this.box = box; } public String getQuality() { return quality; } public void setQuality(String quality) { this.quality = quality; } public Order() { } public Order(String ordernum,String item, String box, String quality) { this.ordernum = ordernum; this.item = item; this.box = box; this.quality = quality; } }
public class fenxiangbyorder { private ArrayList<Order> getList(){ ArrayList<Order> list = new ArrayList<Order>(); Order b1 = new Order(); Order b2 = new Order(); Order b3 = new Order(); b1.setOrderid(1); b2.setOrderid(2); b3.setOrderid(3); b1.setBox("1"); b2.setBox("4"); b3.setBox("1"); b1.setItem("1"); b2.setItem("2"); b3.setItem("3"); b1.setOrdernum("WR20191213001"); b2.setOrdernum("WR20191213001"); b3.setOrdernum("WR20191213001"); b1.setQuality("75"); b2.setQuality("36"); b3.setQuality("23"); list .add(b1); list .add(b2); list .add(b3); return list; } public Map<String, Object> getOrder2(){ Map<String, Object> map = new HashMap<String, Object>(); List<Order> listresult =getList();//三个bean对象 ArrayList<Order> listBox = new ArrayList<Order>(); Integer num1=1; for (int i = 0; i < listresult.size(); i++) { //根据条目遍历,因为这个和条目一致直接拿来用了 Order box = (Order) listresult.get(i); int boxNum = Integer.valueOf(box.getBox()); for (int k = 0; k < boxNum; k++) { //根据箱子数量遍历 Order Order1 = new Order(); Order1 = box; System.out.println("dayin第一遍"+num1); Order1.setOrderid(num1); System.out.println(Order1); num1=num1+1; listBox.add(Order1); } } map.put("result", listBox); return map; } public static void main(String[] args) { fenxiangbyorder a = new fenxiangbyorder(); Map<String, Object> ss = a.getOrder2(); System.out.println("---------------------------------"); System.out.println(ss); } }
数据就直接在里面取,实际业务直接从持久层拿就欧克。
到这里结束,一共六箱。关于设置箱号这里有点补充。
原因就是引用类型变量的赋值只复制对象的引用,而不复制对象本身;而将一个值类型赋给另一个值类型变量时, 将复制包含的值。我的实体bean自然是引用类型,所以我在for循环赋值的时候,实体类添加但对象一直都是相同的, bt.setValues将之 前的赋值更新成。新的赋值然后再添加一-次就会出现上面的结果。
原文地址:https://www.cnblogs.com/kaspar/p/12011836.html
时间: 2024-10-17 09:16:19