欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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、最终效果

MongoDB SpringBoot整合(二十一)