In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to integrate springboot into mongodb, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
Preparatory work
Install MongoDB
Jdk 1.8
Maven 3.0
Idea
Environmental dependence
Introduce spring-boot-starter-data-mongodb dependencies in the pom file:
Org.springframework.boot spring-boot-starter-data-mongodb data source configuration
If the mongodb port is the default port and the password is not set, but not configured, sprinboot will enable the default port.
Spring.data.mongodb.uri=mongodb://localhost:27017/springboot-db
Mongodb sets the password to configure:
Spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/dbname defines a simple entity
Mongodb
Package com.forezp.entity;import org.springframework.data.annotation.Id;public class Customer {@ Id public String id; public String firstName; public String lastName; public Customer () {} public Customer (String firstName, String lastName) {this.firstName = firstName; this.lastName = lastName } @ Override public String toString () {return String.format ("Customer [id=%s, firstName='%s', lastName='%s']", id, firstName, lastName);}} data manipulation dao layer public interface CustomerRepository extends MongoRepository {public Customer findByFirstName (String firstName); public List findByLastName (String lastName);}
Write an interface that inherits MongoRepository, which has the functions of several CURD books. If you want to customize some queries, such as querying according to firstName, and getting querying according to lastName, you only need to define a method. Note that the firstName corresponds strictly to the fields of the stored mongodb. In a typical java application, the method of writing such an interface needs to be implemented by yourself, but in springboot, you only need to write an interface name and the corresponding parameters according to the format, because springboot has already implemented it for you.
Test @ SpringBootApplicationpublic class SpringbootMongodbApplication implements CommandLineRunner {@ Autowired private CustomerRepository repository; public static void main (String [] args) {SpringApplication.run (SpringbootMongodbApplication.class, args);} @ Override public void run (String...) Args) throws Exception {repository.deleteAll (); / / save a couple of customers repository.save (new Customer ("Alice", "Smith")); repository.save (new Customer ("Bob", "Smith")); / / fetch all customers System.out.println ("Customers found with findAll ():") System.out.println ("- -"); for (Customer customer: repository.findAll ()) {System.out.println (customer);} System.out.println (); / / fetch an individual customer System.out.println ("Customer found with findByFirstName ('Alice'):") System.out.println ("- -"); System.out.println (repository.findByFirstName ("Alice")); System.out.println ("Customers found with findByLastName ('Smith'):") System.out.println ("-"); for (Customer customer: repository.findByLastName ("Smith")) {System.out.println (customer);}}
In the springboot application, add the test code. Start the program, and the console prints:
> Customers found with findAll (): Customer [id=58f880f589ffb696b8a6077e, firstName='Alice', lastName='Smith'] Customer [id=58f880f589ffb696b8a6077f, firstName='Bob', lastName='Smith'] Customer found with findByFirstName ('Alice'):-- Customer [id=58f880f589ffb696b8a6077e, firstName='Alice' LastName='Smith'] Customers found with findByLastName ('Smith'):-- Customer [id=58f880f589ffb696b8a6077e, firstName='Alice', lastName='Smith'] Customer [id=58f880f589ffb696b8a6077f, firstName='Bob', lastName='Smith']
The test passed.
This is the answer to the question about how springboot integrates mongodb. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.