GithubHelp home page GithubHelp logo

ieti-spring-boot-data's Introduction

Spring Boot Data with MongoDB

Learning Objectives

  • Explain what MongoDB is.
  • Explain the difference between SQL and NoSQL.
  • Create a MongoDB Cluster on Atlas.
  • Connect your Spring Boot project with a MongoDB Cluster.

Detail Orientation 🤹🏽

Read the following document Attention to details

Main Topics

  • NoSQL.
  • MongoDB.
  • Cluster.

Codelab 🧪

🗣️ "I hear and I forget I see and I remember I do and I understand." Confucius

Part 1: Creating your Atlas account and first cluster:

If you haven't created your MongoDB Cluster follow part 1 - 4:

Part 2: Connecting my MongoDB Cluster with Spring Boot

  1. Login into your MongoDB Atlas account

  2. Click connect on the cluster you created on Part 1:

  3. Select Connect your application:

  4. Choose the Java driver, select the latest version and copy the connection string:

  5. Replace the password on the connection string with the password used when creating your database user.

  6. Add an Environment Variable to the application.properties file to store the MongoDB URI:

    spring.data.mongodb.uri=${MONGODB_URI}
  7. Add the environment variable to IntelliJ Idea by editing the Run/Debug Configurations:

  8. Add the Spring Boot starter data MongoDB dependency to your build.gradle:

       dependencies {
            implementation 'org.springframework.boot:spring-boot-starter-web'
            implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
            testImplementation 'org.springframework.boot:spring-boot-starter-test'
        }
  9. Run your project and verify that the connection is successful.

Part 3: Implementing the MongoDB Service

  1. Create a new package called repository.

  2. Create a new class called UserDocument:

    Java:

      import org.springframework.data.annotation.Id;
      import org.springframework.data.mongodb.core.index.Indexed;
      import org.springframework.data.mongodb.core.mapping.Document;
      
      import java.util.Date;
      
      @Document
      public class UserDocument
      {
         @Id
         String id;
      
         String name;
      
         @Indexed( unique = true )
         String email;
      
         String lastName;
      
         Date createdAt;
      
         public UserDocument()
         {
         }
      }

    Kotlin:

        @Document
        class User(
           @Id var id: String?,
           var name: String,
           var lastName: String,
           @Indexed(unique = true)
           var email: String,
           var createdAt: Date
        )
  3. Create a new interface called UserRepository inside the repository package:

    Java:

      import org.springframework.data.mongodb.repository.MongoRepository;
      
      public interface UserRepository extends MongoRepository<UserDocument, String>
      {}

    Kotlin:

        interface UserRepository: MongoRepository<UserDocument, String>
  4. Create a new UserService implementation called UserServiceMongoDB and inject inside the UserRepository:

    Java:

       import java.util.List;
       
       public class UserServiceMongoDB
       implements UserService
       {
       
           private final UserRepository userRepository;
       
           public UserServiceMongoDB(@Autowired UserRepository userRepository )
           {
               this.userRepository = userRepository;
           }
       
           @Override
           public User create( User user )
           {
               return null;
           }
       
           @Override
           public User findById( String id )
           {
               return null;
           }
       
           @Override
           public List<User> all()
           {
               return null;
           }
       
           @Override
           public boolean deleteById( String id )
           {
               return false;
           }
       
           @Override
           public User update( UserDto userDto, String id )
           {
               return null;
           }
       }

    Kotlin:

        class UserServiceMongoDB(@Autowired private val userRepository: UserRepository) : UserService {
        override fun create(user: User): User? {
        return null
        }
        
            override fun findById(id: String): User? {
                return null
            }
        
            override fun all(): List<User>? {
                return null
            }
        
            override fun deleteById(id: String): Boolean {
                return false
            }
        
            override fun update(userDto: UserDto, id: String): User? {
                return null
            }
        }         
  5. Implement the methods of the UserServiceMongoDB using the UserRepository.

  6. Remove the @Service annotation from the UserServiceHashMap and add it to the UserServiceMongoDB.

  7. Test your API and verify that your data is stored in your cluster.

Challenge Yourself: Implement complex queries using the Spring Data Query Methods

  1. Modify the UserService interface adding the following methods:

    Java:

        List<User> findUsersWithNameOrLastNameLike(String queryText);
        
        List<User> findUsersCreatedAfter(Date startDate);
      {}

    Kotlin:

       fun findUsersWithNameOrLastNameLike(queryText: String): List<User>
       
       fun findUsersCreatedAfter(startDate: Date): List<User>

Tip: take a look at the official documenation and learn how to create custom queries with Spring Data

ieti-spring-boot-data's People

Contributors

paulasanchez810 avatar

Watchers

James Cloos avatar Santiago Carrillo avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.