SpringBoot+MongoDB 实现简单CURD

使用SpringBoot框架,实现MongoDB的CRUD

创建springboot项目

项目结构:
SpringBoot+MongoDB 实现简单CURD_第1张图片

pom文件配置:


        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            org.springframework.boot
            spring-boot-starter-data-mongodb
        

        
            org.mongodb
            mongo-java-driver
        
        
            org.projectlombok
            lombok
            1.16.20
        
     

yml文件配置:

server:
  port: 8888
spring:
  data:
    mongodb:
      uri: mongodb://localhost:27017
      database: shop  //数据库名称

pojo内容: 字段,get,set方法,构造方法

import org.springframework.data.annotation.Id;
import java.io.Serializable;

public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    // @id这个注解来对应mongo的_id这个字段
    @Id
    private String _id;
    private int id;
    private String name;
    private int age;

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }
    public String get_id() {
        return _id;
    }
    public void set_id(String _id) {
        this._id = _id;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    public User() {
    }
    public User(String _id, int id, String name, int age) {
        this._id = _id;
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public User(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
}

dao内容:

import com.czxy.pojo.User;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.repository.MongoRepository;

@Configuration
public interface UserRepository extends MongoRepository<User, String> {
}

service内容:

import com.czxy.dao.UserRepository;
import com.czxy.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    //查询所有
    public List<User> findAllUser() {
        return userRepository.findAll();
    }

    //添加
    public void insert(User user) {
        userRepository.insert(user);
    }

    //删除
    public void delete(String id) {
        userRepository.deleteById(id);
    }

    //修改
    public void update(User user) {
        userRepository.save(user);
    }
}

controller内容:

import com.czxy.pojo.User;
import com.czxy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/get")
    public List<User> getUser() {
        return userService.findAllUser();
    }


    @PostMapping("/add")
    public void insert(@RequestBody User user) {
        userService.insert(user);
    }


    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable String id) {
        userService.delete(id);
        return "delete success";
    }

    @PutMapping("/update")
    public void update(User user){
        userService.update(user);
    }
}

最终项目结构:
SpringBoot+MongoDB 实现简单CURD_第2张图片

启动Postman进行测试:
SpringBoot+MongoDB 实现简单CURD_第3张图片

测试ok

你可能感兴趣的:(SpringBoot+MongoDB 实现简单CURD)