1. 引入依赖
<dependency>
<groupId>com.xxx.basic</groupId>
<artifactId>xxx-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.xxx.basic</groupId>
<artifactId>xxx-starter-mybatis-plus</artifactId>
</dependency>
<dependency>
<groupId>com.xxx.basic</groupId>
<artifactId>xxx-starter-mongodb</artifactId>
</dependency>
<dependency>
<groupId>com.xxx.basic</groupId>
<artifactId>xxx-starter-test</artifactId>
</dependency>
2. 编写单元测试类
1. 领域对象
package com.xxx.learn.common.domain;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
@Document(collection = "pushed_commondity")
@Data
@Accessors(chain = true)
@EqualsAndHashCode(of = "id", callSuper = false)
public class PushedCommondityDo implements Serializable {
public static final String PUSHED_COLLECTION_NAME = "pushed_commondity";
@Id
private String id;
@Field("sku_id")
private Long skuId;
@Field("spec")
private String spec;
@Field("status")
private Integer status;
@Field("price")
private Integer price;
@Field("spu_id")
private Long spuId;
@Field("ansi_shop_id")
private String ansiShopId;
@Field("ylwl_shop_id")
private String ylwlShopId;
@Field("class_id")
private Long classId;
@Field("item_name")
private String itemName;
@Field("item_en_name")
private String itemEnName;
@Field("category_name")
private String categoryName;
@Field("push_time")
private Date pushTime;
}
2. 单元测试类
package com.xxx.learn.mongo;
import com.alibaba.fastjson.JSONObject;
import com.xxx.learn.common.domain.PushedCommondityDo;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import java.util.Date;
@Slf4j
public class MongoTest extends BaseTestCase {
@Autowired
private MongoTemplate mongoTemplate;
@Test
public void testAdd(){
PushedCommondityDo pushedCommondityDo = new PushedCommondityDo();
pushedCommondityDo.setAnsiShopId("111");
pushedCommondityDo.setCategoryName("111");
pushedCommondityDo.setClassId(1L);
pushedCommondityDo.setId("1");
pushedCommondityDo.setItemName("商品名称");
pushedCommondityDo.setItemEnName("english name");
pushedCommondityDo.setPrice(2200);
pushedCommondityDo.setPushTime(new Date());
pushedCommondityDo.setSpuId(1L);
pushedCommondityDo.setSkuId(2L);
pushedCommondityDo.setSpec("中杯 正常糖 正常冰");
pushedCommondityDo.setStatus(1);
pushedCommondityDo.setCategoryName("类名");
pushedCommondityDo.setYlwlShopId("222");
PushedCommondityDo result = mongoTemplate.save(pushedCommondityDo);
log.info("------------->{}", JSONObject.toJSONString(result));
}
}
3. 单元测试
