springboot副本集mongoDB

来源:互联网 发布:178个经典c语言源代码 编辑:程序博客网 时间:2024/05/17 07:15
  1. pom
    在pom文件引入相关的jar包
    <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-data-mongodb</artifactId>        </dependency>

注意:一定不要在pom中引入mongoDB的jar,会引起项目抛出Factory method ‘mongo’ threw exception; nested exception is java.lang.NoSuchFieldError: ACKNOWLEDGED

2 mongoDB Configuration

package cn.creditease.mongo.init;import com.mongodb.MongoClient;import com.mongodb.MongoCredential;import com.mongodb.ServerAddress;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.boot.autoconfigure.mongo.MongoProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Profile;import org.springframework.data.mongodb.MongoDbFactory;import org.springframework.data.mongodb.core.SimpleMongoDbFactory;import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;import javax.inject.Inject;import java.util.ArrayList;import java.util.List;@Configuration@EnableMongoRepositories("cn.creditease.repository")public class CloudDatabaseConfiguration  {    private final Logger log = LoggerFactory.getLogger(CloudDatabaseConfiguration.class);    @Inject    private MongoProperties mongoProperties;    @Bean    public MongoDbFactory mongoDbFactory() throws Exception {        List<ServerAddress> addresses = new ArrayList<ServerAddress>();        ServerAddress addr = new ServerAddress(mongoProperties.getHost(), mongoProperties.getPort());        ServerAddress addr2 = new ServerAddress("10.100.138.95", 27017);        ServerAddress addr3 = new ServerAddress("10.100.138.96", 27018);        addresses.add(addr);        addresses.add(addr2);        addresses.add(addr3);        MongoCredential mongoCredential = MongoCredential.createMongoCRCredential(                mongoProperties.getUsername(),                mongoProperties.getDatabase(),                mongoProperties.getPassword());        List<MongoCredential> credentialsList = new ArrayList<MongoCredential>();        credentialsList.add(mongoCredential);        MongoDbFactory mongoDbFactory =   new SimpleMongoDbFactory(new MongoClient(addresses, credentialsList),                mongoProperties.getDatabase());        return mongoDbFactory;    }}

以上是对多个mongoDB的配置类

3 application.properties

  spring.data.mongodb.database=oltpdb  spring.data.mongodb.host=10.100.138.94  spring.data.mongodb.port=27016  spring.data.mongodb.password=888888  spring.data.mongodb.username=mongouser

注意:springboot默认是在application.properties配置文件中读取mongoDB配置信息。

4 Repository

package cn.creditease.repository;import java.util.List;import org.springframework.data.mongodb.repository.MongoRepository;public interface CustomerRepository extends MongoRepository<Customer, String> {    public Customer findByFirstName(String firstName);    public List<Customer> findByLastName(String lastName);}
0 0
原创粉丝点击