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