为什么80%的码农都做不了架构师?>>>
多数据源:4个mongodb库!
springboot动态数据源。目录结构图:
1、添加pom依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope> </dependency>
2、application.properties
spring.data.mongodb.first.database=node1 spring.data.mongodb.first.uri=localhost:27017spring.data.mongodb.second.database=node2 spring.data.mongodb.second.uri=localhost:27017spring.data.mongodb.third.database=node3 spring.data.mongodb.third.uri=localhost:27017spring.data.mongodb.fourth.database=node4 spring.data.mongodb.fourth.uri=localhost:27017
3、配置类,一个主类,四个各自的mongodb配置类
@Configuration public class MultipleMongoProperties {@Bean(name="firstMongoProperties")@Primary@ConfigurationProperties(prefix="spring.data.mongodb.first")public MongoProperties firstMongoProperties() {System.out.println("-------------------- statisMongoProperties init ---------------------");return new MongoProperties();}@Bean(name="secondMongoProperties")@ConfigurationProperties(prefix="spring.data.mongodb.second")public MongoProperties secondMongoProperties() {System.out.println("-------------------- listMongoProperties init ---------------------");return new MongoProperties();}@Bean(name="thirdMongoProperties")@ConfigurationProperties(prefix="spring.data.mongodb.third")public MongoProperties thirdMongoProperties() {System.out.println("-------------------- thirdMongoProperties init ---------------------");return new MongoProperties();}@Bean(name="fourthMongoProperties")@ConfigurationProperties(prefix="spring.data.mongodb.fourth")public MongoProperties fourthMongoProperties() {System.out.println("-------------------- fourthMongoProperties init ---------------------");return new MongoProperties();} }
@Configuration @EnableMongoRepositories(basePackages = "com.dtb.mongodb.dao.first", mongoTemplateRef = "firstMongo") public class FirstMongoTemplate {@Autowired@Qualifier("firstMongoProperties")private MongoProperties mongoProperties;@Primary@Bean(name = "firstMongo")public MongoTemplate firstMongoTemplate() throws Exception {return new MongoTemplate(firstFactory(this.mongoProperties));}@Bean@Primarypublic MongoDbFactory firstFactory(MongoProperties mongoProperties) throws Exception {ServerAddress serverAdress = new ServerAddress(mongoProperties.getUri());return new SimpleMongoDbFactory(new MongoClient(serverAdress), mongoProperties.getDatabase());} }
@Configuration @EnableMongoRepositories(basePackages = "com.dtb.mongodb.dao.second", mongoTemplateRef = "secondMongo") public class SecondMongoTemplate {@Autowired@Qualifier("secondMongoProperties")private MongoProperties mongoProperties;@Bean(name = "secondMongo")public MongoTemplate secondTemplate() throws Exception {return new MongoTemplate(secondFactory(this.mongoProperties));}@Beanpublic MongoDbFactory secondFactory(MongoProperties mongoProperties) throws Exception {ServerAddress serverAdress = new ServerAddress(mongoProperties.getUri());return new SimpleMongoDbFactory(new MongoClient(serverAdress), mongoProperties.getDatabase());} }
@Configuration @EnableMongoRepositories(basePackages = "com.dtb.mongodb.dao.third", mongoTemplateRef = "thirdMongo") public class ThirdMongoTemplate {@Autowired@Qualifier("thirdMongoProperties")private MongoProperties mongoProperties;@Bean(name = "thirdMongo")public MongoTemplate thirdTemplate() throws Exception {return new MongoTemplate(thirdFactory(this.mongoProperties));}@Beanpublic MongoDbFactory thirdFactory(MongoProperties mongoProperties) throws Exception {ServerAddress serverAdress = new ServerAddress(mongoProperties.getUri());return new SimpleMongoDbFactory(new MongoClient(serverAdress), mongoProperties.getDatabase());} }
@Configuration @EnableMongoRepositories(basePackages = "com.dtb.mongodb.dao.fourth", mongoTemplateRef = "fourthMongo") public class FourthMongoTemplate {@Autowired@Qualifier("fourthMongoProperties")private MongoProperties mongoProperties;@Bean(name = "fourthMongo")public MongoTemplate fourthTemplate() throws Exception {return new MongoTemplate(fourthFactory(this.mongoProperties));}@Beanpublic MongoDbFactory fourthFactory(MongoProperties mongoProperties) throws Exception {ServerAddress serverAdress = new ServerAddress(mongoProperties.getUri());return new SimpleMongoDbFactory(new MongoClient(serverAdress), mongoProperties.getDatabase());}}
4、dao层
看我刚刚的目录结构图:在dao下面创建四个first、second、third、fourth文件夹,对应下面的四个mongodb数据库repository
public interface FirstRepository extends MongoRepository<People,String> { }
public interface SecondRepository extends MongoRepository<People,String> { }
public interface ThirdRepository extends MongoRepository<People,String> { }
public interface FourthRepository extends MongoRepository<People,String> { }
5、entity层
mongodb事务。也是如上的操作,他们没有在同一个文件夹下面
@Data @AllArgsConstructor @NoArgsConstructor @Document(collection = "first_people") public class People {@Idprivate String id;private String value;@Overridepublic String toString() {return "PrimaryMongoObject{" + "id='" + id + '\'' + ", value='" + value + '\''+ '}';} }
@Data @AllArgsConstructor @NoArgsConstructor @Document(collection = "second_people") public class People {@Idprivate String id;private String value;@Overridepublic String toString() {return "PrimaryMongoObject{" + "id='" + id + '\'' + ", value='" + value + '\''+ '}';} }
@Data @AllArgsConstructor @NoArgsConstructor @Document(collection = "third_people") public class People {@Idprivate String id;private String value;@Overridepublic String toString() {return "PrimaryMongoObject{" + "id='" + id + '\'' + ", value='" + value + '\''+ '}';} }
@Data @AllArgsConstructor @NoArgsConstructor @Document(collection = "fourth_people") public class People {@Idprivate String id;private String value;@Overridepublic String toString() {return "PrimaryMongoObject{" + "id='" + id + '\'' + ", value='" + value + '\''+ '}';} }
6、controller层
@Controller @RequestMapping(value = "/mongodb") public class TestController {@AutowiredFirstRepository firstRepository;@AutowiredSecondRepository secondRepository;@AutowiredThirdRepository thirdRepository;@AutowiredFourthRepository fourthRepository;@GetMapping(value = "/test1")public void test1(){firstRepository.save(new com.dtb.mongodb.entity.first.People("1","第一个"));}@GetMapping(value = "/test2")public void test2(){secondRepository.save(new People("2","添加第二个数据"));}@GetMapping(value = "/test3")public void test3(){thirdRepository.save(new com.dtb.mongodb.entity.third.People("1","第三个"));}@GetMapping(value = "/test4")public void test4(){fourthRepository.save(new com.dtb.mongodb.entity.fourth.People("1","第四个"));} }
7、添加成功!
springboot注入数据源?