Spring Boot Sample 023之spring-boot-data-mongodb

一、环境

  • Idea 2020.1
  • JDK 1.8
  • maven

二、目的

spring boot 通过整合mongodb gitHub地址: https://github.com/ouyushan/ouyushan-spring-boot-samples

三、步骤

3.1、点击File -> New Project -> Spring Initializer,点击next

Spring Boot Sample 023之spring-boot-data-mongodb

3.2、在对应地方修改自己的项目信息

Spring Boot Sample 023之spring-boot-data-mongodb

3.3、选择Web依赖,选中Spring Web、Spring Boot Redis。可以选择Spring Boot版本,本次默认为2.2.7,点击Next

Spring Boot Sample 023之spring-boot-data-mongodb

3.4、项目结构

Spring Boot Sample 023之spring-boot-data-mongodb

四、添加文件

pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.ouyushan</groupId>
        <artifactId>ouyushan-spring-boot-samples</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>org.ouyushan</groupId>
    <artifactId>spring-boot-data-mongodb</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-data-mongodb</name>
    <description>Mongodbproject for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

application.properties文件
# MONGODB (MongoProperties)
spring.data.mongodb.uri=mongodb://username:password@localhost:27017/admin

 

User.java

 

package org.ouyushan.spring.boot.data.mongodb.entity;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.Date;

/**
 * @Description:
 * @Author: ouyushan
 * @Email: ouyushan@hotmail.com
 * @Date: 2020/6/2 16:50
 */
@Document(collection = "user")
public class User {
    @Id
    private Long id;
    private String name;
    private Date createTime;

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getCreateTime() {
        return createTime;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", createTime=" + createTime +
                '}';
    }
}

 

UserDao.java

 


UserService.java

 

package org.ouyushan.spring.boot.data.mongodb.service;

import org.ouyushan.spring.boot.data.mongodb.entity.User;
import org.ouyushan.spring.boot.data.mongodb.repository.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Author: ouyushan
 * @Email: ouyushan@hotmail.com
 * @Date: 2020/6/2 16:53
 */
@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    public User insert(User user){
        return userDao.insert(user);
    }

    public void deleteById(Long id){
        userDao.deleteById(id);
    }

    public void updateById(User user){
        userDao.updateById(user);
    }

    public User selectById(Long id){
        return userDao.selectById(id);
    }

}
UserController.java

 

package org.ouyushan.spring.boot.data.mongodb.repository;

import org.ouyushan.spring.boot.data.mongodb.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

/**
 * @Description:
 * @Author: ouyushan
 * @Email: ouyushan@hotmail.com
 * @Date: 2020/6/2 16:52
 */

@Repository
public class UserDao {
    @Autowired
    public MongoTemplate mongoTemplate;

    public User insert(User user) {
        return mongoTemplate.insert(user);
    }

    public void deleteById(Long id) {
        Criteria criteria = Criteria.where("_id").is(id);
        Query query = new Query(criteria);
        mongoTemplate.remove(query, User.class);
    }

    public void updateById(User user) {
        Criteria criteria = Criteria.where("id").in(user.getId());
        Query query = new Query(criteria);
        Update update = new Update();
        update.set("name",user.getName());
        update.set("createTime",user.getCreateTime());
        mongoTemplate.updateMulti(query, update, User.class);
    }

    public User selectById(Long id) {
        Criteria criteria = Criteria.where("id").in(id);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query,User.class);
    }
}

 

package org.ouyushan.spring.boot.data.mongodb.controller;

import org.ouyushan.spring.boot.data.mongodb.entity.User;
import org.ouyushan.spring.boot.data.mongodb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Author: ouyushan
 * @Email: ouyushan@hotmail.com
 * @Date: 2020/6/2 16:54
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/save")
    public User getUser(User user) {
        return userService.insert(user);
    }

    @RequestMapping("/delete")
    public void removeUser(Long id) {
        userService.deleteById(id);
    }

    @RequestMapping("/update")
    public void updateUser(User user) {
        userService.updateById(user);
    }

    @RequestMapping("/select")
    public User getUser(Long id) {
        return userService.selectById(id);
    }

}

 

五、测试

 

SpringBootDataMongodbApplicationTests.java
package org.ouyushan.spring.boot.data.mongodb;

import org.junit.jupiter.api.Test;
import org.ouyushan.spring.boot.data.mongodb.entity.User;
import org.ouyushan.spring.boot.data.mongodb.repository.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Date;

@SpringBootTest
class SpringBootDataMongodbApplicationTests {

    @Autowired
    UserDao userDao;

    @Test
    public void testSaveUser(){
        User user = new User();
        user.setId(1L);
        user.setName("ouyuhsan");
        user.setCreateTime(new Date());
        userDao.insert(user);
    }

    @Test
    public void testUpdateUser(){
        User user = new User();
        user.setId(1L);
        user.setName("ouyuhsan-new");
        user.setCreateTime(new Date());
        userDao.updateById(user);
    }

    @Test
    public void testSelectUserById(){
        User user = userDao.selectById(1L);
        System.out.println(user.toString());
    }

}

 

 

上一篇:【AtCoder】AtCoder Grand Contest 023 解题报告


下一篇:Android Studio的下载和安装教程(从ADT到AS)