MongoDB SpringBoot整合(二十一)
程序员文章站
2022-07-12 22:39:05
...
1、application.properties文件添加MongoDB配置信息
## mongodb
spring.data.mongodb.uri=mongodb://root:aaa@qq.com:27017/?authSource=admin&authMechanism=SCRAM-SHA-1
spring.data.mongodb.database=Test
2、在pom.xml中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>2.1.5.RELEASE</version><!-- 不指定默认下载2.1.3版本,会出问题 -->
</dependency>
3、entity
package com.zit.mongodb.entity;
import java.util.Date;
public class Book {
private String _id;
private String title;
private String[] author;
private Date published_date;
private Integer pages;
private String language;
private String[] checkout;
private String[] myPush;
private Integer available1;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String[] getAuthor() {
return author;
}
public void setAuthor(String[] author) {
this.author = author;
}
public Date getPublished_date() {
return published_date;
}
public void setPublished_date(Date published_date) {
this.published_date = published_date;
}
public Integer getPages() {
return pages;
}
public void setPages(Integer pages) {
this.pages = pages;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String[] getCheckout() {
return checkout;
}
public void setCheckout(String[] checkout) {
this.checkout = checkout;
}
public String[] getMyPush() {
return myPush;
}
public void setMyPush(String[] myPush) {
this.myPush = myPush;
}
public Integer getAvailable1() {
return available1;
}
public void setAvailable1(Integer available1) {
this.available1 = available1;
}
}
4、dao
package com.zit.mongodb.dao.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.data.mongodb.core.MongoTemplate;
import com.zit.mongodb.dao.BookDao;
import com.zit.mongodb.entity.Book;
@Component
public class BookDaoImpl implements BookDao{
@Autowired
private MongoTemplate mongoTemplate;
@Override
public void saveBook(Book book) {
mongoTemplate.save(book);
}
@Override
public List<Book> getBooks() {
List<Book> result = mongoTemplate.findAll(Book.class);
return result;
}
}
5、service
package com.zit.mongodb.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zit.mongodb.dao.BookDao;
import com.zit.mongodb.entity.Book;
import com.zit.mongodb.service.BookService;
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
public void saveBook(Book book) {
bookDao.saveBook(book);
}
@Override
public List<Book> getBooks() {
List<Book> result = bookDao.getBooks();
return result;
}
}
6、controller
package com.zit.mongodb.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zit.mongodb.entity.Book;
import com.zit.mongodb.service.BookService;
@RestController
@RequestMapping("mongo")
public class MongoDBController {
@Autowired
private BookService bookService;
@RequestMapping("books")
public List<Book> getBooks(){
List<Book> result = bookService.getBooks();
return result;
}
}
7、集合sql
/ ----------------------------
// Collection structure for book
// ----------------------------
db.getCollection("book").drop();
db.createCollection("book");
// ----------------------------
// Documents of book
// ----------------------------
db.getCollection("book").insert([ {
_id: 15112345678,
title: "MongoDB",
author: [
"Tom",
"Jerry"
],
"published_date": ISODate("2019-01-01T00:00:00.000Z"),
pages: 216,
language: "Chinese",
checkout: [
{
by: "joe",
date: ISODate("2019-01-01T00:00:00.000Z")
},
{
by: "abc",
date: ISODate("2019-07-03T08:47:13.112Z")
},
{
by: "abc",
date: ISODate("2019-07-03T11:28:44.8Z")
},
{
by: "abc",
date: ISODate("2019-07-03T11:28:47.545Z")
}
],
myPush: [ ],
available1: 7
} ]);
8、最终效果
推荐阅读
-
springboot+springmvc+mybatis项目整合
-
SpringBoot中整合knife4j接口文档
-
SpringBoot缓存详解并整合Redis架构
-
springboot整合freemarker模板
-
详解SpringBoot开发案例之整合定时任务(Scheduled)
-
SpringBoot整合Elasticsearch7.2.0的实现方法
-
SpringBoot整合UEditor的示例代码
-
完整SpringBoot Cache整合redis缓存(二)
-
详解mall整合SpringBoot+MyBatis搭建基本骨架
-
SpringBoot整合Redis、ApachSolr和SpringSession的示例