1. Import respective dependent
<!--mongodb--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
2. And then the configuration file of configuration information Mogo
#xxx that you customize the name
spring.data.mongodb.xxx.database = library name #Mongo specify the use of spring.data.mongodb.xxx.host = 192.168.xx #Mongo where IP spring.data.mongodb.xxx .port = 3633 #Mongo where Port spring.data.mongodb.xxx.password = #Mongo account password if you need to configure spring.data.mongodb.xxx.username = #Mongo account password if you need to configure
3. Get properties of Mongo information
/ **
* This profile information is acquired Mongo configured
* package configuration information to the MongoProperties
* /
@Configuration public class MultipleMongoProperties {
// PerFix represents the name of the configuration file is, up to xxx
// here this is named Mongo xxxMongoTemplate @Primary @Bean (name = "xxxMongoTemplate") @ConfigurationProperties (prefix = "spring.data.mongodb.xxx") public MongoProperties statisMongoProperties () { return new new MongoProperties (); } }
4. Configuration MongoTemplate
// basePackages: MongoProperties specified above configuration packet position
@Configuration @EnableMongoRepositories (basePackages = "com.xxx.config.mongoConfig", mongoTemplateRef = "ZZZ") public class RentFollowMongo { // the package information into the vessel above the MongoProperties @Autowired @Qualifier ( "xxxMongoTemplate") Private mongoProperties mongoProperties; // name of MongoTemplate: customizable, into the container for use to MongoTemplate @Bean (name = "ZZZ") public MongoTemplate zzzTemplate () throws Exception { return new new MongoTemplate (rentfollowFactory (this.mongoProperties)); } // here acquired Mongo plant @Bean public MongoDbFactory mongoFactory (MongoProperties mongoProperties) {
// address register ServerAddress serverAdress = new ServerAddress(mongoProperties.getHost(),mongoProperties.getPort());
//获取到MongoFactory return new SimpleMongoDbFactory(new MongoClient(serverAdress), mongoProperties.getDatabase()); } }
5. Use MongoTemplate
@Component public class MongoTest { @Autowired @Qualifier (value = "ZZZ") Private MongoTemplate mongoTemplate;
public List <Xxx> findFromMongo () { / * * query * / Query Query Query new new = (); / * * Name Database : Mongo in the database name * / String mongoDbName = "Test_Mongo"; // query this database all the data, Xxx is encapsulated entity Mongo database fields List <Xxx> xxxList = mongoTemplate.fin ( query, Xxx.class, mongoDbName); // add query queries: where the information field is in Mongo, in of the range represents, similar the Sql: SELECT * WHERE from Table a in (a, B, C) query.addCriteria (Criteria.where ( "a ") .in (a, b, c);
// tab //query.skip((pageNo-1)*pageSize);
//query.limit(pageSize); // (ascending) query.with (new new the Sort (Sort.Direction.ASC, "Sort")); / / (descending) query.with (new new the Sort (Sort.Direction.DESC, "Sort")); return pageResult; }