SpringBoot 中 mongo多数据源配置新姿势

2020-05-28 00:00:00 数据库 配置 添加 封装 写了


SpringBoot对常用的数据库支持外,对NoSQL 数据库也进行了封装自动化。这一篇主要讲springboot与mongo多数据源相关的配置

1、mongo的介绍

MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种。它在许多场景下可用于替代传统的关系型数据库或键/值存储方式,这里不再多做说明。详细介绍参考mongo入门

2、项目构建

添加pom文件

  1. <dependency>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-data-mongodb</artifactId>

  4. </dependency>

3、在application.properties中添加配置

  1. ##start mongodb for basic

  2. #----------------------------------------------

  3. basic.mongodb.host= 127.0.0.1

  4. basic.mongodb.port= 27017

  5. basic.mongodb.database= basic

  6. #----------------------------------------------

  7. ##end mongodb for spirit


  8. ##start mongodb for basic

  9. #----------------------------------------------

  10. order.mongodb.host= 127.0.0.2

  11. order.mongodb.port= 27017

  12. order.mongodb.database= order

  13. #----------------------------------------------

  14. ##end mongodb for spirit

4、配置相应的数据源

这里我们采用mongoTemplate这种模式去做mongo的相关操作

先写一个基础的抽象类

  1. public abstract class AbstractMongoConfigure {


  2. private String host, database;

  3. private int port;

  4. /*

  5. * Method that creates MongoDbFactory

  6. * Common to both of the MongoDb connections

  7. */

  8. public MongoDbFactory mongoDbFactory() throws Exception {


  9. return new SimpleMongoDbFactory(new MongoClient(host, port), database);

  10. }


  11. /*

  12. * Factory method to create the MongoTemplate

  13. */

  14. abstract public MongoTemplate getMongoTemplate() throws Exception;


  15. public String getHost() {

  16. return host;

  17. }


  18. public void setHost(String host) {

  19. this.host = host;

  20. }


  21. public String getDatabase() {

  22. return database;

  23. }


  24. public void setDatabase(String database) {

  25. this.database = database;

  26. }


  27. public int getPort() {

  28. return port;

  29. }


  30. public void setPort(int port) {

  31. this.port = port;

  32. }

  33. }

3.1)接下来个库的封装

  1. @Configuration

  2. @EnableMongoRepositories(basePackages = {"com.sunliangliang.service.basic"}, mongoTemplateRef = "basicMongoTemplate")

  3. @ConfigurationProperties(prefix = "basic.mongodb")

  4. public class BasicMongoConfigure extends AbstractMongoConfigure {

  5. @Override

  6. @Bean(name = "basicMongoTemplate")

  7. public MongoTemplate getMongoTemplate() throws Exception {

  8. return new MongoTemplate(mongoDbFactory());

  9. }

  10. }

其中 basePackages 的值是该库应用于相应的基础包,prefix即为相应的applications中的配置值

3.2)接下来第两个库的封装

  1. @Configuration

  2. @EnableMongoRepositories(basePackages = {"com.sunliangliang.service.order"}, mongoTemplateRef = "orderMongoTemplate")

  3. @ConfigurationProperties(prefix = "order.mongodb")

  4. public class OrderMongoConfigure extends AbstractMongoConfigure{

  5. @Primary

  6. @Override

  7. @Bean(name = "orderMongoTemplate")

  8. public MongoTemplate getMongoTemplate() throws Exception {

  9. return new MongoTemplate(mongoDbFactory());

  10. }

  11. }

封装完成后接下来进行相应实体的配置

4、创建数据实体

  1. public class UserInfo implements Serializable{

  2. private long id;

  3. private String username;

  4. private String password;

  5. private String nickname;

  6. private int age;

  7. private String name;


  8. public long getId() {

  9. return id;

  10. }


  11. public void setId(long id) {

  12. this.id = id;

  13. }


  14. public String getUsername() {

  15. return username;

  16. }


  17. public void setUsername(String username) {

  18. this.username = username;

  19. }


  20. public String getPassword() {

  21. return password;

  22. }


  23. public void setPassword(String password) {

  24. this.password = password;

  25. }


  26. public String getNickname() {

  27. return nickname;

  28. }


  29. public void setNickname(String nickname) {

  30. this.nickname = nickname;

  31. }


  32. public int getAge() {

  33. return age;

  34. }


  35. public void setAge(int age) {

  36. this.age = age;

  37. }


  38. public String getName() {

  39. return name;

  40. }


  41. public void setName(String name) {

  42. this.name = name;

  43. }

  44. }

4、添加对实体的操作

在这里我写了一个公用的接口用于基础的增删改查

  1. public interface BaseService<T> {

  2. Boolean save(T entity);


  3. Boolean update(T entity);


  4. Boolean delete(Long id);


  5. T getById(Long id);


  6. List<T> listAll();


  7. List<T> listAll(T entity);

  8. }

接下来接口的实现

  1. @Service

  2. public class UserInfoService implements BaseService<UserInfo>{

  3. @Autowired

  4. @Qualifier("basicMongoTemplate")

  5. private MongoTemplate mongoTemplate;

  6. @Override

  7. public Boolean save(UserInfo entity) {

  8. mongoTemplate.save(entity);

  9. return true;

  10. }


  11. @Override

  12. public Boolean update(UserInfo entity) {

  13. return null;

  14. }


  15. @Override

  16. public Boolean delete(Long id) {

  17. return null;

  18. }


  19. @Override

  20. public UserInfo getById(Long id) {

  21. return null;

  22. }


  23. @Override

  24. public List<UserInfo> listAll() {

  25. return mongoTemplate.findAll(UserInfo.class);

  26. }


  27. @Override

  28. public List<UserInfo> listAll(UserInfo entity) {

  29. return null;

  30. }

  31. }

注意此处@Qualifier(“basicMongoTemplate”)中即为相应的具体实现类。

这里只写了添加和查询列表的实现,其他的查询相应的api即可

  1. @RunWith(SpringJUnit4ClassRunner.class)

  2. @SpringBootTest

  3. public class UserInfoServiceTest {

  4. @Autowired

  5. private UserInfoService userInfoService;

  6. @Test

  7. public void save() throws Exception {

  8. UserInfo userInfo = new UserInfo();

  9. userInfo.setAge(23);

  10. userInfo.setId(1000l);

  11. userInfo.setName("管理员");

  12. userInfo.setNickname("hehe");

  13. userInfo.setPassword("123456");

  14. userInfo.setUsername("admin");

  15. userInfoService.save(userInfo);

  16. }


  17. @Test

  18. public void listAll() throws Exception {

  19. userInfoService.listAll();

  20. }

  21. }

若库不存在,会自动生成相应的库和文档 如图:

另一个order相关库的配置就不再这里写了,相应的请参考代码

github中lessons-5



相关文章