Skip to content

Instantly share code, notes, and snippets.

@sabljakovich
Created September 11, 2021 14:18
Show Gist options
  • Save sabljakovich/034f3626882962017cb25e11ff711f29 to your computer and use it in GitHub Desktop.
Save sabljakovich/034f3626882962017cb25e11ff711f29 to your computer and use it in GitHub Desktop.
package com.sabljakovic.mongospringdemo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class MongoSpringDemoApplication {
Logger log = LoggerFactory.getLogger(MongoSpringDemoApplication.class);
public static void main(String[] args) {
SpringApplication.run(MongoSpringDemoApplication.class, args);
}
@Bean
CommandLineRunner runner(final ProductsRepository productsRepository){
return args -> {
log.info("Cleaning existing products from the database");
productsRepository.deleteAll();
log.info("Creating a new product");
productsRepository.insert(new Product("A new product"));
log.info("Products in the database: {}", productsRepository.findAll());
log.info("Total number of products in the database: {}", productsRepository.count());
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment