使用Spring Boot的mongodb vs CRUD操作

时间:2018-06-20 15:29:03

标签: mongodb spring-boot crud

下面是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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.test</groupId>
    <artifactId>DemoMongoDBProduc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>DemoMongoDBProduc</name>
    <description>Say Hello To Boot Using Mongo</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <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-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

// **Below is my main method**

    package com.mono.mongo.crud;

   import org.springframework.boot.SpringApplication;
   import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoMongoDbProducApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoMongoDbProducApplication.class, args);
    }
}

// 下面是我的Controller类

    package com.mono.mongo.crud.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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.mono.mongo.crud.model.Person;
import com.mono.mongo.crud.service.PersonService;

@RestController 
public class PersonController {

    @Autowired
    private PersonService personService;

    @RequestMapping("/create")
    public String create(@RequestParam String firstName, @RequestParam String lastName, @RequestParam int age) {
        Person p = personService.create(firstName, lastName, age);
        return p.toString();
    }

    @RequestMapping("/get")
    public Person getPerson(@RequestParam String firstName) {
        return personService.getByFirstName(firstName);
    }

    @RequestMapping("/getAll")
    public List<Person> getAll(){
        return personService.getAll();
    }

    @RequestMapping("/update")
    public String update(@RequestParam String firstName, @RequestParam String lastName, @RequestParam int age) {
        Person p = personService.update(firstName, lastName, age);
        return p.toString();
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam String firstName) {
        personService.delete(firstName);
        return "Deleted : "+firstName;
    }

    @RequestMapping("/delete")
    public String deleteAll() {
        personService.deleteAll();
        return "Deleted all records";
    }
}

// 以下是我的服务课程

   package com.mono.mongo.crud.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mono.mongo.crud.model.Person;
import com.mono.mongo.crud.repository.PersonRepository;

@Service
public class PersonService {

    @Autowired
    private PersonRepository personRepository;

    //  Create operation
    public Person create(String firstName, String lastName, int age)
    {
        return personRepository.save(new Person(firstName, lastName, age));
    }

    //  Retrieve operation
    public List<Person> getAll()
    {
        return personRepository.findAll();
    }
    public Person getByFirstName(String firstName)
    {
        return personRepository.findByFirstName(firstName);
    }

    //  Update Operation
    public Person update(String firstName, String lastName, int age)
    {
        Person p = personRepository.findByFirstName(firstName);
        p.setLastName(lastName);
        p.setAge(age);
        return personRepository.save(p);
    }

    //  Delete Operation
    public void deleteAll() {
        personRepository.deleteAll();
    }
    public void delete(String firstName) {
        Person p = personRepository.findByFirstName(firstName);
        personRepository.delete(p);
    }
}

// 下面是我的PersonRepository类

package com.mono.mongo.crud.repository;

import java.util.List;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import com.mono.mongo.crud.model.Person;

@Repository

public interface PersonRepository extends MongoRepository<Person, String> {
    public Person findByFirstName(String firstName);
    public List<Person> findbyAge(int age);
//  public Person findByFirstName(String firstName);
//  public List<Person> findByAge(int age);
}

// 以下是我的服务课程

    package com.mono.mongo.crud.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mono.mongo.crud.model.Person;
import com.mono.mongo.crud.repository.PersonRepository;

@Service
public class PersonService {

    @Autowired
    private PersonRepository personRepository;

    //  Create operation
    public Person create(String firstName, String lastName, int age)
    {
        return personRepository.save(new Person(firstName, lastName, age));
    }

    //  Retrieve operation
    public List<Person> getAll()
    {
        return personRepository.findAll();
    }
    public Person getByFirstName(String firstName)
    {
        return personRepository.findByFirstName(firstName);
    }

    //  Update Operation
    public Person update(String firstName, String lastName, int age)
    {
        Person p = personRepository.findByFirstName(firstName);
        p.setLastName(lastName);
        p.setAge(age);
        return personRepository.save(p);
    }

    //  Delete Operation
    public void deleteAll() {
        personRepository.deleteAll();
    }
    public void delete(String firstName) {
        Person p = personRepository.findByFirstName(firstName);
        personRepository.delete(p);
    }
}

// 下面是我的POJO课

    package com.mono.mongo.crud.model;

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

@Document
public class Person {

    @Id
    String id;
    String firstName;
    String lastName;
    int age;

    public Person(String firstName, String lastName, int age)
    {
        this.firstName=firstName;
        this.lastName=lastName;
        this.age=age;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "Person FirstName: "+firstName+" LastName: "+lastName+" age: "+age;
    }
}

我将spring工具套件用作IDE,MongoDB,Maven 3.2。我正在尝试使用MongoDB学习CRUD操作。但是当我将这些代码作为Spring Boot App运行时,我得到了

  

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为'personController'的bean时出错:通过字段'personService'表示的不满足的依赖关系;嵌套的异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ personService”的bean时出错:通过字段“ personRepository”表示的不满足的依赖关系;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为'personRepository'的bean时出错:调用init方法失败;嵌套的异常是org.springframework.data.mapping.PropertyReferenceException:没有找到类型Person的属性findbyAge!

有人可以告诉我我在哪里以及发生什么错误

这是我收到的错误的屏幕截图: Error

2 个答案:

答案 0 :(得分:0)

在您的存储库类中。

重命名接口方法
findbyAge(int age);

findByAge(int age);

答案 1 :(得分:0)

我已经解决了问题。这很简单,我犯了一个愚蠢的错误。 在delete()方法和deleteAll()方法的Controller类中,@ RequestMapping仅像@RequestMapping(“ / delete”)一样被删除,相反,我已将其更改为@RequestMapping(“ / deleteAll”)以进行完全删除和@RequestMapping (“ / delete”)代表deleteByFirstName。它对我有用。