Spring-boot Accessing MongoDB

Source: Internet
Author: User
Tags mongoclient

1. Accessing configuration information

 PackageHello;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;Importorg.springframework.data.authentication.UserCredentials;Importorg.springframework.data.mongodb.config.AbstractMongoConfiguration;Importorg.springframework.data.mongodb.core.MongoOperations;Importorg.springframework.data.mongodb.core.MongoTemplate;ImportCom.mongodb.Mongo;Importcom.mongodb.MongoClient; @Configuration Public classMongoconfigextendsabstractmongoconfiguration {@Bean PublicMongo Mongo ()throwsException {return Newmongoclient (); } @Bean PublicMongotemplate Mongotemplate ()throwsException {usercredentials user=NewUsercredentials ("Scott", "Tiger"); return NewMongotemplate (MONGO (), "Test1", user); } @OverrideprotectedString GetDatabaseName () {//TODO auto-generated Method Stub        return"Test1"; } }

2, Pojo class

 PackageHello;Importorg.springframework.data.annotation.Id; Public classCustomer {@IdPrivateString ID; PrivateString FirstName; PrivateString LastName;  PublicCustomer () {} PublicCustomer (String firstName, String lastName) { This. FirstName =FirstName;  This. LastName =LastName; } @Override PublicString toString () {returnString.Format ("customer[id=%s, Firstname= '%s ', lastname= '%s ']", ID, firstName, lastName); }}

3. Repository interface

 Package Hello; Import java.util.List; Import org.springframework.data.mongodb.repository.MongoRepository;  Public Interface extends Mongorepository<customer, string> {    public  Customer findbyfirstname (String FirstName);      Public List<customer> findbylastname (String lastName);}

4, Application

 PackageHello;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.boot.CommandLineRunner;Importorg.springframework.boot.SpringApplication;ImportOrg.springframework.boot.autoconfigure.AutoConfigureBefore;Importorg.springframework.boot.autoconfigure.SpringBootApplication;ImportOrg.springframework.context.annotation.Import, @SpringBootApplication @autoconfigurebefore@import ( Mongoconfig.class) Public classApplicationImplementsCommandlinerunner {@AutowiredPrivatecustomerrepository repository;  Public Static voidMain (string[] args) {springapplication.run (application.class, args); } @Override Public voidRun (String ... args)throwsException {repository.deleteall (); //save a couple of customersRepository.save (NewCustomer ("Alice", "Smith")); Repository.save (NewCustomer ("Bob", "Smith")); //Fetch All CustomersSystem.out.println ("Customers found with FindAll ():"); System.out.println ("-------------------------------");  for(Customer customer:repository.findAll ()) {System.out.println (customer);        } System.out.println (); //fetch an individual customerSystem.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(The Customer customer:repository.findByLastName ("Smith") {System.out.println (customer); }    }}

Spring-boot Accessing MongoDB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.