Spring data mongodb @Document定义Enum/List/Map数据结构
2017-04-14 景峯 Netkiller
本文节选自《Netkiller Java 手札》
7.2.2. @Document
复杂的 @Document 数据类型定义
package cn.netkiller.domain; import java.util.Date; import java.util.List; import java.util.Map; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @Document public class MultilevelDirectSellingTradingRebate { public enum Type { POINT, CASH, GIFT } public enum Rebate { DIRECT, INDIRECT } public enum Status { New, Rejected, Approved } @Id private String id; public String name; public Date beginDate; public Date endDate; public double lowAmount; public double highAmount; public Type type; public Status status = Status.New; public List<Map<String, Map<?, ?>>> product; @Override public String toString() { return "MultilevelDirectSellingTradingRebate [id=" + id + ", name=" + name + ", beginDate=" + beginDate + ", endDate=" + endDate + ", lowAmount=" + lowAmount + ", highAmount=" + highAmount + ", type=" + type + ", status=" + status + ", product=" + product + "]"; } }
7.2.2.1. 在 @Document 中使用 Enum 类型
public enum Type { POINT, CASH, GIFT } public enum Rebate { DIRECT, INDIRECT } public enum Status { New, Rejected, Approved }
枚举类型的赋值方法
MultilevelDirectSellingTradingRebate multilevelDirectSellingTradingRebate = new MultilevelDirectSellingTradingRebate(); multilevelDirectSellingTradingRebate.name = "TEST"; multilevelDirectSellingTradingRebate.beginDate = new Date(); multilevelDirectSellingTradingRebate.endDate = new Date(); multilevelDirectSellingTradingRebate.lowAmount = 1.5d; multilevelDirectSellingTradingRebate.highAmount = 100d; multilevelDirectSellingTradingRebate.type = Type.CASH;
7.2.2.2. 在 @Document 中定义数据结构 List/Map
public List<Map<String, Map<?, ?>>> product;
下面是数据集结构的赋值例子
Map<Enum<Rebate>, Double> rebate = new HashMap<Enum<Rebate>, Double>(); rebate.put(Rebate.DIRECT, 10.05d); rebate.put(Rebate.INDIRECT, 6.05d); Map<String, Map<?, ?>> prod1 = new HashMap<String, Map<?, ?>>(); prod1.put("USDRMB", rebate); List<Map<String, Map<?, ?>>> products = new ArrayList<Map<String, Map<?, ?>>>(); products.add(prod1); multilevelDirectSellingTradingRebate.product = products;
相关文章
String boot with Apache kafka 完整的发布订阅例子
Spring boot 将 Session 放入 Redis
Spring boot with Spring security
Spring RestFul and RestTemplate
Spring boot with Velocity template
Spring Boot · Mongo Repository
作者微信公众号,长按图片扫描二维码添加。