Spring data mongodb @Document定义Enum/List/Map数据结构

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 with Hessian

Spring Properties 文件读取

Spring boot 将 Session 放入 Redis

Spring boot with Redis

Spring boot with Scheduling

Spring boot · 链接池配置

Spring Tool Suite

Spring 常见问题与解决方法

Spring boot with Spring security

Spring RestFul and RestTemplate

Spring boot with Velocity template

Spring boot with Email

Spring boot with Oracle

Spring boot with MySQL 实例参考

Spring Boot · Mongo Repository

Spring Boot MongoDB 实例

Spring Boot 快速开始

 

作者微信公众号,长按图片扫描二维码添加。

640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1

转载于:https://my.oschina.net/neochen/blog/879459

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值