博客专区 > neo-chen的博客 > 博客详情
Spring data mongodb @Document定义Enum/List/Map数据结构
neo-chen 发表于3个月前
Spring data mongodb @Document定义Enum/List/Map数据结构
  • 发表于 3个月前
  • 阅读 27
  • 收藏 0
  • 点赞 0
  • 评论 0

330元/年抢阿里云香港云服务器,节省80%出海成本>>>   

摘要: 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 快速开始

 

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

共有 人打赏支持
neo-chen
粉丝 586
博文 193
码字总数 225987
作品 10
×
neo-chen
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: