spring boot使用经验分享(六)mongodb

一、概述

最为常用的NoSQL 型的数据库,MongoDB虽然没有redis读写速度快,但是比较适合存储大数据量的数据。文档结构的存储方式,能够更便捷的获取数据。

千万级别的文档对象,查询有索引的ID的不比mysql慢,在对非索引字段的查询方面上,更是全面胜出。

二、整合mongodb

引入

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-mongodb</artifactId>

</dependency>

配置

spring.data.mongodb.host=127.0.0.1

spring.data.mongodb.port=27017

spring.data.mongodb.database=product_db

spring.data.mongodb.username=product_db

spring.data.mongodb.password=123456

创建实体类

@Document
public class UserReadHistory {
    @Id
    private String id;
    @Indexed
    private Long userId;
    private String userName;
    @Indexed
    private Long productId;
    private String productName;
    private Date createTime;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Long getProductId() {
        return productId;
    }

    public void setProductId(Long productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public Date getCreateTime() {
        return createTime;
    }

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

}

 

@Service
public class UserReadHistoryServiceImpl implements UserReadHistoryService {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public UserReadHistory save(UserReadHistory bean) {
        return mongoTemplate.save(bean);
    }

    @Override
    public UserReadHistory save(UserReadHistory bean, String collectionName) {
        return mongoTemplate.save(bean, "testCollection");
    }

    @Override
    public long update(UserReadHistory bean) {
        Query query = new Query(Criteria.where("id").is(bean.getId()));
        Update update = new Update();
        update.set("userName", bean.getUserName());
        //更新查询返回的第一条结果
        UpdateResult result = mongoTemplate.updateFirst(query, update, UserReadHistory.class);
        return result.getMatchedCount();
    }

    @Override
    public long delete(Integer id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
        DeleteResult result = mongoTemplate.remove(query, UserReadHistory.class);
        return result.getDeletedCount();

    }

    @Override
    public UserReadHistory query(Integer id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
        return mongoTemplate.findOne(query, UserReadHistory.class);
    }

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值