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

}

发布了43 篇原创文章 · 获赞 0 · 访问量 3907

猜你喜欢

转载自blog.csdn.net/zhangdx001/article/details/105054107