大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本篇文章为大家展示了SpringBoot中如何整合MongoDB多数据源,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
专注于为中小企业提供成都网站制作、做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业汉阳免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
// build.gradle implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
# application.yml spring: data: mongodb: primary: uri: mongodb://localhost:27017/db1 secondary: uri: mongodb://localhost:27017/db2
// PrimaryMongoConfig.java package com.fengwenyi.springboot_mongo_multi_source.config; import com.mongodb.MongoClientURI; import org.springframework.boot.autoconfigure.mongo.MongoProperties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; /** * MongoDB Primary Config * @author Erwin Feng * @since 2019-07-01 17:12 */ @Configuration @EnableMongoRepositories(basePackages = "com.fengwenyi.springboot_mongo_multi_source.primary", mongoTemplateRef = "primaryMongoTemplate") public class PrimaryMongoConfig { @Bean @Primary @ConfigurationProperties(prefix="spring.data.mongodb.primary") public MongoProperties primaryMongoProperties() { return new MongoProperties(); } @Primary @Bean(name = "primaryMongoTemplate") public MongoTemplate primaryMongoTemplate() throws Exception { return new MongoTemplate(primaryFactory(primaryMongoProperties())); } @Bean @Primary public MongoDbFactory primaryFactory(MongoProperties mongoProperties) throws Exception { return new SimpleMongoDbFactory(new MongoClientURI(primaryMongoProperties().getUri())); } }
// SecondaryMongoConfig.java package com.fengwenyi.springboot_mongo_multi_source.config; import com.mongodb.MongoClientURI; import org.springframework.boot.autoconfigure.mongo.MongoProperties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; /** * MongoDB Secondary Config * @author Erwin Feng * @since 2019-07-01 17:12 */ @Configuration @EnableMongoRepositories(basePackages = "com.fengwenyi.springboot_mongo_multi_source.secondary", mongoTemplateRef = "secondaryMongoTemplate") public class SecondaryMongoConfig { @Bean @ConfigurationProperties(prefix="spring.data.mongodb.secondary") public MongoProperties secondaryMongoProperties() { return new MongoProperties(); } @Bean(name = "secondaryMongoTemplate") public MongoTemplate secondaryMongoTemplate() throws Exception { return new MongoTemplate(secondaryFactory(secondaryMongoProperties())); } @Bean public MongoDbFactory secondaryFactory(MongoProperties mongoProperties) throws Exception { return new SimpleMongoDbFactory(new MongoClientURI(secondaryMongoProperties().getUri())); } }
// User.java package com.fengwenyi.springboot_mongo_multi_source.secondary.entity; import lombok.Data; import lombok.experimental.Accessors; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; import java.io.Serializable; import java.time.Instant; /** * 用户 * @author Erwin Feng * @since 2019-07-01 17:15 */ @Data @Accessors(chain = true) @Document(collection = "t_user") public class User implements Serializable { private static final long serialVersionUID = -7229906944062898852L; /** ID */ @Id private String id; /** 用户名 */ private String username; /** 年龄 */ private Integer age; /** 注册时间 */ private Instant registerTime; }
// UserRepository.java package com.fengwenyi.springboot_mongo_multi_source.secondary.repository; import com.fengwenyi.springboot_mongo_multi_source.secondary.entity.User; import org.springframework.data.mongodb.repository.MongoRepository; import java.util.List; /** * 用户 * @author Erwin Feng * @since 2019-07-01 17:18 */ public interface UserRepository extends MongoRepository{ /** * 通过用户名查询 * @param username 用户名 * @return */ List findAllByUsername(String username); }
// LoginLog.java package com.fengwenyi.springboot_mongo_multi_source.primary.entity; import lombok.Data; import lombok.experimental.Accessors; import org.springframework.data.annotation.Id; import java.io.Serializable; import java.time.Instant; /** * 登录日志 * @author Erwin Feng * @since 2019-07-01 17:18 */ @Data @Accessors(chain = true) public class LoginLog implements Serializable { private static final long serialVersionUID = -6694661682102504919L; /** ID */ @Id private String id; /** 用户ID */ private String uid; /** 用户名 */ private String username; /** 登录时间 */ private Instant loginTime; }
// LoginLogRepository.java package com.fengwenyi.springboot_mongo_multi_source.primary.repository; import com.fengwenyi.springboot_mongo_multi_source.primary.entity.LoginLog; import org.springframework.data.mongodb.repository.MongoRepository; /** * 登录日志 * @author Erwin Feng * @since 2019-07-01 17:21 */ public interface LoginLogRepository extends MongoRepository{ }
// InitController.java package com.fengwenyi.springboot_mongo_multi_source.controller; import com.fengwenyi.springboot_mongo_multi_source.secondary.entity.User; import com.fengwenyi.springboot_mongo_multi_source.secondary.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RestController; import javax.annotation.PostConstruct; import java.time.Instant; import java.util.ArrayList; import java.util.List; /** * 初始化工具类 * @author Erwin Feng * @since 2019-07-01 17:24 */ @RestController public class InitController { /** [mongo] 用户 */ @Autowired private UserRepository userRepository; @PostConstruct public void init() { Listall = userRepository.findAll(); if (all.size() > 0) return; userRepository.save(new User().setUsername("Zhangsan").setAge(20).setRegisterTime(Instant.now())); List users = new ArrayList<>(); User u1 = new User().setUsername("u1").setAge(19).setRegisterTime(Instant.now()); User u2 = new User().setUsername("u2").setAge(20).setRegisterTime(Instant.now()); User u3 = new User().setUsername("u3").setAge(10).setRegisterTime(Instant.now()); users.add(u1); users.add(u2); users.add(u3); userRepository.saveAll(users); } }
// TestController.java package com.fengwenyi.springboot_mongo_multi_source.controller; import com.fengwenyi.springboot_mongo_multi_source.primary.entity.LoginLog; import com.fengwenyi.springboot_mongo_multi_source.primary.repository.LoginLogRepository; import com.fengwenyi.springboot_mongo_multi_source.secondary.entity.User; import com.fengwenyi.springboot_mongo_multi_source.secondary.repository.UserRepository; import net.iutil.ApiResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Query; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.time.Instant; import java.util.List; /** * 测试 * @author Erwin Feng * @since 2019-07-01 17:28 */ @RestController @RequestMapping("/test") public class TestController { /** [mongo] 用户 */ @Autowired private UserRepository userRepository; /** [mongo] 登录日志 */ @Autowired private LoginLogRepository loginLogRepository; /** [mongo] */ @Autowired private MongoTemplate mongoTemplate; /** * 登录 * @param username * @return */ @GetMapping("/login") public ApiResult login(String username) { if (StringUtils.isEmpty(username)) return ApiResult.error().setMsg("用户名不能为空"); Listusers = userRepository.findAllByUsername(username); if (users.size() == 1) { // 记录日志 loginLogRepository.save(new LoginLog().setUid(users.get(0).getId()).setUsername(username).setLoginTime(Instant.now())); return ApiResult.success(); } if (users.size() == 0) return ApiResult.error().setMsg("用户名查询失败"); return ApiResult.error().setMsg("用户异常"); } /** * 登录日志 * @return */ @GetMapping("/login-log") public ApiResult loginLog() { Query query = new Query(); List loginLogs = mongoTemplate.find(query, LoginLog.class); return ApiResult.success(loginLogs); } }
GET http://localhost:8080/test/login?username=Zhangsan
响应:
{ "code": 0, "msg": "Success" }
GET http://localhost:8080/test/login-log
响应:
{ "code": 0, "msg": "Success", "data": [ { "id": "5d19d7f5cede54c46b6b20c5", "uid": "5d19d560cede54c45701e12a", "username": "Zhangsan", "loginTime": "2019-07-01T09:52:53.447Z" }, { "id": "5d19da82cede54c46f77579a", "uid": "5d19d560cede54c45701e12a", "username": "Zhangsan", "loginTime": "2019-07-01T10:03:46.496Z" }, { "id": "5d19df5fcede54c46f77579b", "uid": "5d19d560cede54c45701e12a", "username": "Zhangsan", "loginTime": "2019-07-01T10:24:31.272Z" }, { "id": "5d19df6acede54c46f77579c", "uid": "5d19d560cede54c45701e12b", "username": "u1", "loginTime": "2019-07-01T10:24:42.199Z" }, { "id": "5d19df6dcede54c46f77579d", "uid": "5d19d560cede54c45701e12d", "username": "u3", "loginTime": "2019-07-01T10:24:45.421Z" } ] }
上述内容就是SpringBoot中如何整合MongoDB多数据源,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。