【问题标题】:mongodb vs CRUD operation using spring boot使用spring boot的mongodb vs CRUD操作
【发布时间】:2018-11-29 19:17:57
【问题描述】:

下面是 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);
    }
}

// 下面是我的控制器类

    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: No property findbyAge found for type Person!

谁能告诉我我在哪里犯了什么错误

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

【问题讨论】:

  • 只发布代码的相关部分而不是项目的一半可能更有帮助。没有多少人有时间首先阅读大量代码。通过首先自己分析您的问题,您可以确定哪些部分是相关的。
  • @Capricorn 是的,我会记住这一点,因为我是新来的,这是我的第一个问题,所以想提供所有细节。你现在能帮我吗?

标签: mongodb spring-boot crud


【解决方案1】:

在您的 Repository 类中。

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

findByAge(int age);

【讨论】:

  • 如果这解决了您的问题,那么您可以投票并将其标记为正确。 :)
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2020-11-07
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 2021-02-09
    • 1970-01-01
    相关资源
    最近更新 更多