首页 > 数据库技术 > 详细

springboot整合Mangodb

时间:2019-08-14 18:33:22      阅读:213      评论:0      收藏:0      [点我收藏+]

//linux安装mangodb教程:https://www.cnblogs.com/yangxiaohui227/p/11347832.html

1.引入maven 依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
2.在application.yml进行连接信息配置
srpring:    
  data:
    mongodb:
      uri: mongodb://yangxiaohui:Yangxiaohui227@106.52.85.248:27017/shopdb?maxPoolSize=256        -- 注释:uri的格式:mongodb://username:password@IP:port/database?maxPoolSize=256

 3.mysql中的一张表,在mysql数据库中会有一个实体(entiy/pojo/domain)跟其对应,同理mango中的collection(表) 也会有一个实体跟其对应:

//创建一个order表如下:

@Document(collection = "order")  //贴有该注解的字符会被映射为对应的集合名字
public class ShopMongoOrder {
    @Id   //贴有Id注解,生成mongodb的collection对象后,被贴注解的字段就是唯一主键,不能重复
    private String orderId;

    private String orderTitle;

    private BigDecimal orderTotalAmount;

    private BigDecimal orderPayAmount;

    private String createTime;

    private String finishTime;

    private String deliveryTime;

    private  Integer orderStatus;

    private Integer payStatus;

    private String customerId;

    /**
     * 日期格式如果不想用String类型表示,可以用下面的格式化方式
     * @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") 
       private Date updateTime;//最新浏览时间
     *
     * 
     */

    public String getCustomerId() {
        return customerId;
    }

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

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = 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;
    }
}

//单条插入

@Service
public class MangodbService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private ShopOrderMastMapper orderMastMapper;
    //新增
    public void insertOrder(String orderId){
        ShopMongoOrder shopMongoOrder = orderMastMapper.selectMongoOrderByOrderId(orderId);
        if(null!=shopMongoOrder){
            mongoTemplate.insert(shopMongoOrder); //调用的是insert(T objectToSave)该方法,因为T是实体类,通过注解就知道查到哪个collection了,mango不像mysql一样需要先创建表,它不用,第一条插入的数据,发现没有对应的collection就会创建
        }
    }
}

//插入第一条数据后:

技术分享图片

重复插入后会报错:

技术分享图片

//批量新增

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

//删除单条数据

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

//根据条件删除数据

    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);
    }

 //单条修改

 //更新订单
    public void updateByOrderId(String orderId){
       //使用updateFirst(Query query, Update update, Class<?> entityClass)该方法,updateFirst和update方法的区别是:updateFirst只更改查询到的第一条,update是更改所有满足条件的
        //new Update().set("orderTitle", "iphone").set("a","b") 可以使用链式表达式
        UpdateResult updateResult = mongoTemplate.updateFirst(new Query(Criteria.where("orderId").is(orderId)), new Update().set("orderTitle", "iphone"), ShopMongoOrder.class);
      
    }

//查找单条数据

 //查找
    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;
    }

 

springboot整合Mangodb

原文:https://www.cnblogs.com/yangxiaohui227/p/11353803.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!