A CRUD With MongoDB and Java in One Line of Code

In this quick tutorial, I'll show you how to implement a web CRUD (Create, Read, Update, and Delete) using MongoDB, Spring Boot (version 2.3.4), and Vaadin (version 14.3.7). I assume you have MongoDB, the JDK version 8 or later, Maven, and a Java IDE installed on your computer.

Here's a video version of this tutorial if you prefer:

Step 1: Create a New Spring Boot Project

Go to start.spring.io and create a new project with the following dependencies:

Download and extract the ZIP file. The extracted content is a Maven project that you can import into your favorite IDE.

Add the following repository and dependency to your pom.xml file:

XML
 




x


1
<repository>
2
 <id>vaadin-addons</id>
3
 <url>https://maven.vaadin.com/vaadin-addons</url>
4
</repository>
5
 
          
6
<dependency>
7
 <groupId>org.vaadin.crudui</groupId>
8
    <artifactId>crudui</artifactId>
9
    <version>4.3.1</version>
10
</dependency>
9
            <artifactId>crudui</artifactId>



Step 2: Create a Domain Class

Create a new Student class as follows:

Java
 




xxxxxxxxxx
1
16


 
1
import lombok.*;
2
import org.springframework.data.annotation.Id;
3
 
          
4
@Data
5
@NoArgsConstructor
6
@AllArgsConstructor
7
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
8
public class Student {
9
 
          
10
    @Id
11
    private Integer regNumber;
12
    private String firstName;
13
    private String lastName; 
14
 
          
15
}



Step 3: Create a Repository Interface

Create a new StudentRepository interface as follows:

Java
 




xxxxxxxxxx
1


 
1
import org.springframework.data.mongodb.repository.MongoRepository;
2
 
          
3
public interface StudentRepository extends MongoRepository<Student, Integer> {
4
}



Step 4: Create a Service Class

Create a new StudentService class as follows:

Java
 




xxxxxxxxxx
1
36


1
import org.springframework.stereotype.Repository;
2
import org.vaadin.crudui.crud.CrudListener;
3
 
          
4
import java.util.Collection;
5
 
          
6
@Repository
7
public class StudentService implements CrudListener<Student> {
8
 
          
9
    private final StudentRepository repository;
10
 
          
11
    public StudentService(StudentRepository repository) {
12
        this.repository = repository;
13
    }
14
 
          
15
    @Override
16
    public Collection<Student> findAll() {
17
        return repository.findAll();
18
    }
19
 
          
20
    @Override
21
    public Student add(Student student) {
22
        return repository.insert(student);
23
    }
24
 
          
25
    @Override
26
    public Student update(Student student) {
27
        return repository.save(student);
28
    }
29
 
          
30
    @Override
31
    public void delete(Student student) {
32
        repository.delete(student);
33
    }
34
 
          
35
}



Step 5: Implement a View Class

Create a new class as follows:

Java
 




xxxxxxxxxx
1
15


 
1
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
2
import com.vaadin.flow.router.Route;
3
import org.vaadin.crudui.crud.impl.GridCrud;
4
 
          
5
@Route("")
6
public class StudentsView extends VerticalLayout {
7
 
          
8
    public StudentsView(StudentService service) {
9
        GridCrud<Student> crud = new GridCrud<>(Student.class, service);
10
        add(crud);
11
        setSizeFull();
12
    }
13
 
          
14
}



See line 9. With this one line, you are creating the actual CRUD web UI component.

Step 6: Run the Application

Make sure MongoDB is running and start the application as follows:

Plain Text
 




xxxxxxxxxx
1


 
1
mvn spring-boot:run



Note: The first time you run the application will take more time than subsequent runs.

Go to http://localhost:8080 and try the application:

add name

Next Steps

You can configure the MongoDB connection string in the application.properties file. For example:

Properties files
 




xxxxxxxxxx
1


1
spring.data.mongodb.uri=mongodb://localhost/test
2
spring.data.mongodb.username=user
3
spring.data.mongodb.password=password



You can customize the CRUD component. For example:

Java
 




xxxxxxxxxx
1


1
crud.getCrudFormFactory().setVisibleProperties("firstName", "lastName");
2
crud.getGrid().setColumns("firstName", "lastName");



There are many more customization options. See the examples at https://vaadin.com/directory/component/crud-ui-add-on.

 

 

 

 

Top