【xxx-mall学习系列-基础框架使用-005】-基于ssm整合MongoDB

271 阅读1分钟

1. 引入依赖

        <!-- web模块 -->
        <dependency>
            <groupId>com.xxx.basic</groupId>
            <artifactId>xxx-starter-web</artifactId>
        </dependency>

        <!--db-->
        <dependency>
            <groupId>com.xxx.basic</groupId>
            <artifactId>xxx-starter-mybatis-plus</artifactId>
        </dependency>

        <!-- mongoDB -->
        <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;

/**
 * @author amyzhang
 * @history 2020-09-17 新建
 * @since JDK1.7
 */
@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";

    /**
     * skuId
     */
    @Id
    private String id;

    @Field("sku_id")
    private Long skuId;

    /**
     * 规格
     */
    @Field("spec")
    private String spec;

    /**
     * sku状态
     */
    @Field("status")
    private Integer status;

    /**
     * sku状态
     */
    @Field("price")
    private Integer price;

    /**
     * spuId
     */
    @Field("spu_id")
    private Long spuId;
    /**
     * 昂司店铺id
     */
    @Field("ansi_shop_id")
    private String ansiShopId;

    /**
     * 云里物里店铺id
     */
    @Field("ylwl_shop_id")
    private String ylwlShopId;

    /**
     * 品类id
     */
    @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;

/**
 * @author amyzhang
 * @history 2020-09-17 新建
 * @since JDK1.7
 */
@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. 单元测试