如何通过模型,服务和控制器使用springboot进行粗操作

时间:2019-06-05 02:55:41

标签: mongodb model-view-controller crud

我是Spring Boot框架的新手。根据基本知识,我知道Spring Boot是使用MVC架构的框架。谁能提供给我一个如何在此框架中使用模型,服务存储库和服务的编码示例

5 个答案:

答案 0 :(得分:0)

首先,您必须连接数据库。将以下代码放入application.propperties类

spring.data.mongodb.uri=mongodb://localhost:27017/test

答案 1 :(得分:0)

然后您的模型类将如下所示

package com.example.demo.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 void Person(String fname,String lname,int age)
{
    this.firstName=fname;
    this.LastName=lname;
    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) {
    LastName = lastName;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}
public String toString()
{
    return "Person First Name: "+this.firstName+" "+this.LastName+"     
age:"+this.age;
}
}

答案 2 :(得分:0)

这将是您的存储库界面

package com.example.demo.repository;

import java.util.List;

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

import com.example.demo.model.Person;


@Repository
public interface PersonRepository extends MongoRepository<Person,String>
{
   public Person findByFirstName(String fname);
   public List<Person> findByAge(int age);
}

答案 3 :(得分:0)

这将是您的服务

package com.example.demo.service;

import java.util.List;

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

import com.example.demo.model.Person;
import com.example.demo.repository.PersonRepository;

@Service
public class PersonService 
{

 @Autowired
 private PersonRepository personRepository;

 //create operation
 public Person create(String fname,String lname,int age) 
 {
    return personRepository.save(new Person(fname,lname,age));
 }

 //retriew Operation
 public List<Person> getAll()
 {
    return personRepository.findAll();
 }

 public Person getByFirstName(String fname)
 {
    return personRepository.findByFirstName(fname);
 }

 //update operation
 public Person update(String fname,String lname,int age)
 {
    Person p = personRepository.findByFirstName(fname);
    p.setFirstName(fname);
    p.setLastName(lname);
    p.setAge(age);
    return personRepository.save(p);
    //personRepository.delete(p);
  }

 //delete
 public void deleteAll()
 {
    personRepository.deleteAll();
 }

 public void delete(String fname)
 {
    Person p  = personRepository.findByFirstName(fname);
    personRepository.delete(p);
 }
}

答案 4 :(得分:0)

这是控制器类

package 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.example.demo.model.Person;
import com.example.demo.service.PersonService;

@RestController
public class PersonController {

@Autowired
private PersonService personService;

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

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

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

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

@RequestMapping("/delete")
public void deleteByName(@RequestParam String fname)
{
    personService.delete(fname);
}

@RequestMapping("/deleteAll")
public void deleteAll()
{
    personService.deleteAll();
}


}

这是另一个例子

@CrossOrigin(origins = "*", allowedHeaders = "*")
@RestController
public class CourseTotalController {

@Autowired
CourseTotalRepository repository;

@Autowired
SubjectRepository SubRepository;

@RequestMapping(value = "/total/{id}", method = RequestMethod.GET)
public Courses calcTotal(@PathVariable("id") String id) {
    float total = 0;

    Optional<Courses> opt =  repository.findById(id);
    Courses modifiedCourse = opt.get();
    String [] Subjects = opt.get().getSubjects();   

    for(int i=0;i< Subjects.length;i++) {
        Optional<Subject> SubjectOpt =  SubRepository.findById(Subjects[i]);
        total = total + SubjectOpt.get().getAmount();
    }
    modifiedCourse.setTotal(total);

    return modifiedCourse;
}
}