Posted In: MongoDB, Spring, Spring Data

Spring Boot + Spring Data MongoDB + Embedded MongoDB – Complexity1

Technologies used

1. Spring Boot 1.5.6

2. Spring data commons 1.13.6

3. Spring data mongodb 1.10.6

4. MongoDB driver 3.4.2

5. MongoDB Embedded de.flapdoodle.embed.mongo

Steps

1. Create Spring Boot starter project or download and import example source project from this link

2. Verify maven pom.xml to have spring-boot-starter, Spring data mongodb, spring-boot-starter-test and de.flapdoodle.embed

3. Add application properties

4. Map the POJO with MongoDB Document

5. Implement MongoRepository interface

6. Test User Repository

1. Create Spring Boot starter project

2. Verify maven pom.xml

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>
<dependency>
	<groupId>de.flapdoodle.embed</groupId>
	<artifactId>de.flapdoodle.embed.mongo</artifactId>
	<scope>test</scope>
</dependency>
<dependency>
	<groupId>com.google.code.gson</groupId>
	<artifactId>gson</artifactId>
	<version>2.8.1</version>
</dependency>

3. Add application properties

spring.data.mongodb.port=27017

4. Map the POJO properties with Database table columns

@Document- Name of the collection.

@Id – Primary key column

import java.io.Serializable;
import java.util.Date;

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

@Document(collection = "users")
public class User implements Serializable {
	private static final long serialVersionUID = 65007985219089594L;

	@Id
	private Long userId;
	private String userLoginId;
	private String userPassword;
	private String firstName;
	private String lastName;
	private Date dateofBirth;
	private String emailId;
        
        //GETTERS AND SETTERS .....
}

5. Implement MongoRepository interface

package com.example.springapp;

import java.util.Date;
import java.util.List;

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

public interface UserRepository extends MongoRepository<User, Long> {
	List<User> findByEmailId(String emailId);

	List<User> findByDateofBirth(Date date);
}

6. Test User Repository

package com.example.springapp;

import java.text.SimpleDateFormat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class UserRepositoryTest {

	private static final Logger LOGGER = LoggerFactory.getLogger(UserRepositoryTest.class);

	@Test
	public void contextLoads() {
	}

	@Autowired
	UserRepository userRepository;

	@Test
	public void testUserRepository() throws Exception {
		LOGGER.debug("Running testUserRepository()....");

		User newUser = new User();
		newUser.setUserId(System.currentTimeMillis());
		newUser.setFirstName("Abhijit");
		newUser.setLastName("Pednekar");
		newUser.setDateofBirth(new 
				SimpleDateFormat("yyyy-MM-dd").parse("1976-09-09"));
		newUser.setEmailId("abh@as.com");
		newUser.setUserLoginId("abhijit");
		newUser.setUserPassword("password");

		userRepository.save(newUser);

		LOGGER.debug("\n\nfindByAll()");
		for (User user : userRepository.findAll()) {
			LOGGER.debug(user.toString());
		}

		LOGGER.debug("\n\nfindByEmailId(String emailId)");
		for (User user : userRepository.findByEmailId("abh@as.com")) {
			LOGGER.debug(user.toString());
		}

		LOGGER.debug("\n\nfindByDateofBirth(Date date)");
		for (User user : userRepository.findByDateofBirth(
				new SimpleDateFormat("yyyy-MM-dd").parse("1976-09-09"))) {
			LOGGER.debug(user.toString());
		}

	}
}

 

 

by , on August 5th, 2017

  • Categories