Mongodb with CURD example with image in Spring Boot

Download Mongo DB From here:

Mongo DB Download

After download you need to install and open like below:

After this you need to click on connect then click on create database then enter collection and database name like below:


Now your database and collection is successfully created like below:

Project Structure:

SpringBootMongoDbApplication.java


package com.example.demo;

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

@SpringBootApplication
public class SpringBootMongoDbApplication {

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

}
  

ImageController.java


package com.example.demo.controller;

import java.io.IOException;

import org.bson.BsonBinarySubType;
import org.bson.types.Binary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.example.demo.model.ProfileImage;
import com.example.demo.repository.ImageRepo;

@RestController
public class ImageController {

	@Autowired
	private ImageRepo imageRepo;

	@PostMapping("/image")
	public ProfileImage imageUpload(@RequestParam("image") MultipartFile file) throws IOException {
		System.out.println(file.getOriginalFilename());
		System.out.println(file.getSize());
		ProfileImage profileImage = new ProfileImage();
		profileImage.setImage(new Binary(BsonBinarySubType.BINARY, file.getBytes()));
		return imageRepo.insert(profileImage);
	}
	
}
  

StudentController.java


  package com.example.demo.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.Student;
import com.example.demo.service.StudentService;

@RestController
public class StudentController {

	@Autowired
	private StudentService studentService;

	@PostMapping("/addStudent")
	public Student addStudentPage(@RequestBody Student student)
			throws Exception {
		return studentService.addStudentPage(student);
	}

	@DeleteMapping("/delete/{id}")
	public String deleteStudent(@PathVariable String id) throws Exception {
		return studentService.deleteStudent(id);
	}

	@PutMapping("/update/{id}")
	public Student updateStudent(@PathVariable String id, @RequestBody Student student) throws Exception {
		return studentService.updateStudent(id, student);
	}

	@GetMapping("/students")
	public List<Student> getAllStudents() {
		return studentService.getAllStudents();
	}

	@GetMapping("/student/{id}")
	public Student getStudent(@PathVariable String id) throws Exception {
		return studentService.getStudent(id);
	}

}
  

ProfileImage.java


package com.example.demo.model;

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

@Document(collection = "images")
public class ProfileImage {
	@Id
	private String id;
	private Binary image;

	public String getId() {
		return id;
	}

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

	public Binary getImage() {
		return image;
	}

	public void setImage(Binary image) {
		this.image = image;
	}

}
  

Student.java


package com.example.demo.model;

import org.bson.types.Binary;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.stereotype.Component;

@Document(collection = "student")
public class Student {
	@Id
	private String id;
	private String name;
	private float marks;
	private Binary image;

	public Binary getImage() {
		return image;
	}

	public void setImage(Binary image) {
		this.image = image;
	}

	public String getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

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

	public float getMarks() {
		return marks;
	}

	public void setMarks(float marks) {
		this.marks = marks;
	}

}
  

ImageRepo.java


package com.example.demo.repository;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import com.example.demo.model.ProfileImage;

@Repository
public interface ImageRepo extends MongoRepository<ProfileImage, String> {

}
  

StudentRepository.java


package com.example.demo.repository;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import com.example.demo.model.Student;

@Repository
public interface StudentRepository extends MongoRepository<Student, String>{

}
  

StudentService.java


  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.Student;
import com.example.demo.repository.StudentRepository;

@Service
public class StudentService {
	@Autowired
	private StudentRepository studentRepository;

	public Student addStudentPage(Student student) {
		return studentRepository.save(student);
	}

	public String deleteStudent(String id) throws Exception {
		Student dbstudent = studentRepository.findById(id)
				.orElseThrow(() -> new Exception("This Student id is not found : " + id));
		studentRepository.delete(dbstudent);
		return "delete successfully";
	}

	public Student updateStudent(String id, Student student) throws Exception {
		Student dbstudent = studentRepository.findById(id)
				.orElseThrow(() -> new Exception("This Student id is not found : " + id));
		dbstudent.setMarks(student.getMarks());
		dbstudent.setName(student.getName());
		return studentRepository.save(dbstudent);
	}

	public List<Student> getAllStudents() {
		return studentRepository.findAll();
	}

	public Student getStudent(String id) throws Exception {
		Student student = studentRepository.findById(id)
				.orElseThrow(() -> new Exception("This Student id is not found : " + id));
		return student;
	}

}
  

application.properties


server.port=9898
spring.data.mongodb.uri=mongodb://localhost:27017/collegedb

spring.servlet.multipart.max-file-size=256MB
spring.servlet.multipart.max-request-size=256MB
spring.servlet.multipart.enabled=true
  

Add below Maven Depenedency:

  <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>
  

Output Screens:









No comments: