springboot integration Mangodb

// linux installation mangodb Tutorial: https: //www.cnblogs.com/yangxiaohui227/p/11347832.html

1. introduced maven dependent 

<dependency> 
    <the groupId> org.springframework.boot </ the groupId> 
    <the artifactId> Starter-Spring-Boot-Data-MongoDB </ the artifactId> 
</ dependency> 
2. connection information disposed application.yml 
srpring:     
  the Data: 
    MongoDB: 
      uri: MongoDB: // yangxiaohui: [email protected]: 27017/256 = shopdb maxPoolSize - Notes:? uri format: mongodb: // username: password @ IP: port / database maxPoolSize? = 256

 3.mysql in a table in the mysql database have an entity (entiy / pojo / domain) with its corresponding, similarly mango in the collection (table) there will be an entity with its corresponding:

// Create a table following order:

@Document (collection = "Order")   // posted the comment characters are mapped into a corresponding set of names of 
public  class ShopMongoOrder { 
    @Id    // after affixed Id annotation, generating a collection of objects mongodb, annotated fields are posted primary key is unique, not repeated 
    Private String the orderId; 

    Private String orderTitle; 

    Private the BigDecimal orderTotalAmount; 

    Private the BigDecimal orderPayAmount; 

    Private String createTime; 

    Private String finishTime; 

    Private String DeliveryTime; 

    Private   Integer orderStatus; 

    Private Integer payStatus; 

    Private String the customerId; 

    / **
     * If you do not represent a date format type String embodiment can use the following format 
     * @JsonFormat (pattern = "the MM-dd-YYYY HH: mm: SS", TimeZone = "GMT +. 8") 
       Private a Date updateTime; // [Browse time 
     * 
     * 
     * / 

    public String. getCustomerId () {
         return the customerId; 
    } 

    public  void setCustomerId (String the customerId) {
         the this .customerId = the customerId; 
    } 

    public String getOrderId () {
         return the orderId; 
    } 

    public  void setOrderId (String the orderId) {
         the this .OrderID = the orderId; 
    }

    public String getOrderTitle() {
        return orderTitle;
    }

    public void setOrderTitle(String orderTitle) {
        this.orderTitle = orderTitle;
    }

    public BigDecimal getOrderTotalAmount() {
        return orderTotalAmount;
    }

    public void setOrderTotalAmount(BigDecimal orderTotalAmount) {
        this.orderTotalAmount = orderTotalAmount;
    }

    public BigDecimal getOrderPayAmount() {
        return orderPayAmount;
    }

    public void setOrderPayAmount(BigDecimal orderPayAmount) {
        this.orderPayAmount = orderPayAmount;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getFinishTime() {
        return finishTime;
    }

    public void setFinishTime(String finishTime) {
        this.finishTime = finishTime;
    }

    public String getDeliveryTime() {
        return deliveryTime;
    }

    public void setDeliveryTime(String deliveryTime) {
        this.deliveryTime = deliveryTime;
    }

    public Integer getOrderStatus() {
        return orderStatus;
    }

    public void setOrderStatus(Integer orderStatus) {
        this.orderStatus = orderStatus;
    }

    public Integer getPayStatus() {
        return payStatus;
    }

    public void setPayStatus(Integer payStatus) {
        this.payStatus = payStatus;
    }
}

// single drop

@Service
 public  class MangodbService { 
    @Autowired 
    Private MongoTemplate mongoTemplate; 
    @Autowired 
    Private ShopOrderMastMapper orderMastMapper;
     // new 
    public  void insertOrder (String the orderId) { 
        ShopMongoOrder shopMongoOrder = orderMastMapper.selectMongoOrderByOrderId (the orderId);
         IF ( null ! = ShopMongoOrder) { 
            mongoTemplate. INSERT (shopMongoOrder); // call is insert (T objectToSave) the method, because T is the entity class via annotations to know which collection found out, mango mysql not need to create the same table, it is not, the first inserting data found no corresponding collection is created 
         }
    }
}

// first insert data:

After repeated insertion will complain:

// add bulk

//批量插入
    public void insertAll(){
        List<ShopMongoOrder> shopMongoOrders = orderMastMapper.selectOrderList();
        Collection<ShopMongoOrder> mongoOrders = mongoTemplate.insertAll(shopMongoOrders);
        return ;
    }

// delete individual data

 //删除单条
    public void deleteByOrderId(String orderId){
        ShopMongoOrder shopMongoOrder = orderMastMapper.selectMongoOrderByOrderId(orderId);
        mongoTemplate.remove(shopMongoOrder);
    }

// delete data based on conditions

    public void deleteByOrderId(String orderId){
        Criteria c = Criteria.where("orderId").is(orderId); //相当于mysql的where orderId="ddd"
        Query query = new Query(c);
        DeleteResult ret = mongoTemplate.remove(query,ShopMongoOrder.class);
    }

 // single modification

 // update the order 
    public  void updateByOrderId (String orderId) {
        // use updateFirst (<?> Query query, Update update, Class entityClass) the method, the difference between updateFirst and update methods are: updateFirst only change to the first query, update is to change all that satisfy the condition
         // new new Update (). the SET ( "orderTitle", "the iPhone"). the SET ( "a", "b") can be used chain expression 
        UpdateResult UpdateResult = mongoTemplate.updateFirst ( new new Query (Criteria.where ( "the orderId") IS (the orderId).), new new the Update () SET ( "orderTitle", "iPhone"), ShopMongoOrder.. class ); 
      
    }

// Find a single data

 //查找
    public ShopMongoOrder selectOrderId(String orderId){
        //findOne(Query query, Class<T> entityClass)
        ShopMongoOrder mongoOrder = mongoTemplate.findOne(new Query(Criteria.where("orderId").is(orderId)), ShopMongoOrder.class);
        return mongoOrder;
    }

 

Guess you like

Origin www.cnblogs.com/yangxiaohui227/p/11353803.html