Mongodb学习笔记--springboot中使用mongodb

# MongoDB springboot集成mongoDB的使用
在springboot中对mongodb和redis等NoSql数据库都已经有集成了,这里主要学习如何去使用springboot框架集成的mongodb。
首先导入依赖:
```xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
```
打开mongodb副本集,创建study数据库,并为数据库创建用户名密码。
在springboot的配置文件中添加mongodb的配置:
```properties
#单节点配置
#spring.data.mongodb.uri=mongodb://user:pwd@ip:port/database
#集群配置
spring.data.mongodb.uri=mongodb://root:123456@192.168.226.130:27017,192.168.226.130:27018,192.168.226.130:27019/study
```
mongodb的增删改查:
+ 创建实体类book
```java
@Data
@Accessors(chain = true)
public class Book implements Serializable {

private static final long serialVersionUID = 1L;

private Long id;
private String name;
private String content;
private BigDecimal price;
}
```
+ 创建mongoService接口:
```java
public interface MongoService<T> {

void save(T t);

void delete(Long id);

Long update(T t);

T queryForOne(T t);
}
```
+ 实现接口:
```java
@Service
@Slf4j
public class MongoServiceImpl implements MongoService<Book> {

@Autowired
private MongoTemplate mongoTemplate;

@Override
public void save(Book book) {
log.debug("要保存的对象[{}]", book.toString());
mongoTemplate.save(book);
}

@Override
public void delete(Long id) {
log.debug("要删除的对象id:[{}]", id);
Query query = new Query(Criteria.where("id").is(id));
mongoTemplate.remove(query, Book.class);
}

@Override
public Long update(Book book) {
Query query = new Query(Criteria.where("id").is(book.getId()));
Update update = new Update().set("name", book.getName()).set("price", book.getPrice()).set("content", book.getContent());
UpdateResult result = mongoTemplate.updateFirst(query, update, Book.class);

return Objects.isNull(result) ? 0 : result.getMatchedCount();
}

@Override
public Book queryForOne(Book book) {
Query query = new Query(Criteria.where("name").is(book.getName()));
return mongoTemplate.findOne(query, Book.class);
}
}
```
+ 测试:
```
@Test
void test() {
mongoService.save(new Book().setId(1L).setName("java").setPrice(BigDecimal.valueOf(50.55)).setContent("hello java"));
mongoService.save(new Book().setId(2L).setName("c").setPrice(BigDecimal.valueOf(55.55)).setContent("hello c"));
mongoService.update(new Book().setId(1L).setName("python").setContent("hello python").setPrice(BigDecimal.valueOf(60.01)));
Book book = mongoService.queryForOne(new Book().setName("python"));
System.out.println(book.toString());
mongoService.delete(1L);

}
```
mongodb增删改查实现完成。
可以在mongodb中使用shell命令查看数据是否正确。
```shell script
rs0:PRIMARY> use study
switched to db study
rs0:PRIMARY> db.auth("root","123456")
1
rs0:PRIMARY> show collections
book
system.indexes
rs0:PRIMARY> db.book.find()
{ "_id" : NumberLong(2), "name" : "c", "content" : "hello c", "price" : "55.55", "_class" : "com.xiazhi.mongodb.bean.Book" }
```
posted @ 2020-05-08 10:22  Zs夏至  阅读(305)  评论(0编辑  收藏  举报