MongoDB 是最早热门非关系数据库的之一,使用也比较普遍,一般会用做离线数据分析来使用,放到内网的居
多。由于很多公司使用了云服务,服务器默认都开放了外网地址,导致前一阵子大批 MongoDB 因配置漏洞被攻
击,数据被删,引起了人们的注意,同时也说明了很多公司生产中大量使用mongodb。
MongoDB(来自于英文单词“Humongous”,中文含义为“庞大”)是可以应用于各种规模的企业、各个行业以及各
类应用程序的开源数据库。基于分布式文件存储的数据库。由C++语言编写。旨在为 WEB 应用提供可扩展的高性
能数据存储解决方案。MongoDB 是一个高性能,开源,无模式的文档型数据库,是当前 NoSql 数据库中比较热
门的一种。
MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库
的。他支持的数据结构非常松散,是类似 json 的 bjson 格式,因此可以存储比较复杂的数据类型。MongoDB 最
大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单
表查询的绝大部分功能,而且还支持对数据建立索引。
传统的关系数据库一般由数据库(database)、表(table)、记录(record)三个层次概念组成,MongoDB 是
由数据库(database)、集合(collection)、文档对象(document)三个层次组成。MongoDB 对于关系型数
据库里的表,但是集合中没有列、行和关系概念,这体现了模式自由的特点。
MongoDB 中的一条记录就是一个文档,是一个数据结构,由字段和值对组成。MongoDB 文档与 JSON 对象类
似。字段的值有可能包括其它文档、数组以及文档数组。MongoDB 支持 OS X、Linux 及 Windows 等操作系统,
并提供了 Python,PHP,Ruby,Java及 C++ 语言的驱动程序,社区中也提供了对 Erlang 及 .NET 等平台的驱动
程序。
MongoDB 的适合对大量或者无固定格式的数据进行存储,比如:日志、缓存等。对事物支持较弱,不适用复杂的
多文档(多表)的级联查询。
文中演示 Mongodb 版本为 3.4.24
。
Spring Boot 对各种流行的数据源都进行了封装,当然也包括了 Mongodb,下面给大家介绍如何在 Spring Boot 中
使用 Mongodb。
pom 包里面添加 spring-boot-starter-data-mongodb
包引用
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.0.RELEASEversion>
<relativePath/>
parent>
<groupId>com.examplegroupId>
<artifactId>spring-boot-mongodb-contentartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>spring-boot-mongodb-contentname>
<description>spring-boot-mongodb-contentdescription>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-mongodbartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
spring.application.name=spring-boot-mongodb
spring.data.mongodb.uri=mongodb://127.0.0.1:27017/test
多个 IP 集群可以采用以下配置:
spring.data.mongodb.uri=mongodb://user:pwd@ip1:port1,ip2:port2/database
package com.example.model;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = -3258839839160856613L;
private Long id;
private String userName;
private String passWord;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
@Override
public String toString() {
return "UserEntity{" +
"id=" + id +
", userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
'}';
}
}
Repository 层实现了 User 对象的增删改查
package com.example.repository;
import com.example.model.User;
public interface UserRepository {
public void saveUser(User user);
public User findUserByUserName(String userName);
public long updateUser(User user);
public void deleteUserById(Long id);
}
package com.example.repository.impl;
import com.example.model.User;
import com.example.repository.UserRepository;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
@Component
public class UserRepositoryImpl implements UserRepository {
@Autowired
private MongoTemplate mongoTemplate;
/**
* 创建对象
*
* @param user
*/
@Override
public void saveUser(User user) {
mongoTemplate.save(user);
}
/**
* 根据用户名查询对象
*
* @param userName
* @return
*/
@Override
public User findUserByUserName(String userName) {
Query query = new Query(Criteria.where("userName").is(userName));
User user = mongoTemplate.findOne(query, User.class);
return user;
}
/**
* 更新对象
*
* @param user
*/
@Override
public long updateUser(User user) {
Query query = new Query(Criteria.where("id").is(user.getId()));
Update update = new Update().set("userName", user.getUserName()).set("passWord", user.getPassWord());
//更新查询返回结果集的第一条
UpdateResult result = mongoTemplate.updateFirst(query, update, User.class);
// 更新查询返回结果集的所有
// mongoTemplate.updateMulti(query,update,UserEntity.class);
if (result != null) {
return result.getMatchedCount();
} else {
return 0;
}
}
/**
* 删除对象
*
* @param id
*/
@Override
public void deleteUserById(Long id) {
Query query = new Query(Criteria.where("id").is(id));
mongoTemplate.remove(query, User.class);
}
}
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootMongodbContentApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMongodbContentApplication.class, args);
}
}
package com.example.repository;
import com.example.model.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTest {
@Autowired
private UserRepository userDao;
@Test
public void testSaveUser() throws Exception {
User user = new User();
user.setId(1l);
user.setUserName("小明");
user.setPassWord("fffooo123");
userDao.saveUser(user);
}
@Test
public void findUserByUserName() {
User user = userDao.findUserByUserName("小明");
System.out.println("user is " + user);
}
@Test
public void updateUser() {
User user = new User();
user.setId(1l);
user.setUserName("天空");
user.setPassWord("123456");
userDao.updateUser(user);
}
@Test
public void deleteUserById() {
userDao.deleteUserById(1l);
}
}
可以使用工具 MongoVUE
工具来连接后直接图形化展示查看,也可以登录服务器用命令来查看
1、登录 mongos
mongo -host localhost -port 27017
2、切换到 test 库
use test
3、查询 user 集合数据
db.user.find()
根据查询的结果来观察测试用例的执行是否正确。
执行testSaveUser()
方法,查看结果:
执行findUserByUserName()
方法,查看结果:
执行updateUser()
方法,查看结果:
执行deleteUserById()
方法,查看结果:
到此 Spring Boot 对应 MongoDB 的增删改查功能已经全部实现。
接下来实现 MongoDB 多数据源的使用
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.0.RELEASEversion>
parent>
<groupId>com.examplegroupId>
<artifactId>spring-boot-multi-mongodbartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>spring-boot-multi-mongodbname>
<description>spring-boot-multi-mongodbdescription>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-mongodbartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
# 由于这里没有安装其它的mongodb,所以用同一个
spring.application.name=spring-boot-multi-mongodb
mongodb.primary.uri=mongodb://127.0.0.1:27017
mongodb.primary.database=primary
mongodb.secondary.uri=mongodb://127.0.0.1:27017
mongodb.secondary.database=secondary
封装读取以 Mongodb 开头的两个配置文件:
package com.example.config.props;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "mongodb")
public class MultipleMongoProperties {
private MongoProperties primary = new MongoProperties();
private MongoProperties secondary = new MongoProperties();
public MongoProperties getPrimary() {
return primary;
}
public void setPrimary(MongoProperties primary) {
this.primary = primary;
}
public MongoProperties getSecondary() {
return secondary;
}
public void setSecondary(MongoProperties secondary) {
this.secondary = secondary;
}
}
配置不同包路径下使用不同的数据源
第一个库的封装
package com.example.config;
import com.example.config.props.MultipleMongoProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
@Configuration
@EnableConfigurationProperties(MultipleMongoProperties.class)
@EnableMongoRepositories(basePackages = "com.example.repository.primary",
mongoTemplateRef = "primaryMongoTemplate")
public class PrimaryMongoConfig {
}
第二个库的封装
package com.example.config;
import com.example.config.props.MultipleMongoProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
@Configuration
@EnableConfigurationProperties(MultipleMongoProperties.class)
@EnableMongoRepositories(basePackages = "com.example.repository.secondary",
mongoTemplateRef = "secondaryMongoTemplate")
public class SecondaryMongoConfig {
}
读取对应的配置信息并且构造对应的 MongoTemplate
package com.example.config;
import com.example.config.props.MultipleMongoProperties;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
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;
@Configuration
public class MultipleMongoConfig {
@Autowired
private MultipleMongoProperties mongoProperties;
@Primary
@Bean(name = "primaryMongoTemplate")
public MongoTemplate primaryMongoTemplate() throws Exception {
return new MongoTemplate(primaryFactory(this.mongoProperties.getPrimary()));
}
@Bean
@Qualifier("secondaryMongoTemplate")
public MongoTemplate secondaryMongoTemplate() throws Exception {
return new MongoTemplate(secondaryFactory(this.mongoProperties.getSecondary()));
}
@Bean
@Primary
public MongoDbFactory primaryFactory(MongoProperties mongo) throws Exception {
MongoClient client = new MongoClient(new MongoClientURI(mongoProperties.getPrimary().getUri()));
return new SimpleMongoDbFactory(client, mongoProperties.getPrimary().getDatabase());
}
@Bean
public MongoDbFactory secondaryFactory(MongoProperties mongo) throws Exception {
MongoClient client = new MongoClient(new MongoClientURI(mongoProperties.getSecondary().getUri()));
return new SimpleMongoDbFactory(client, mongoProperties.getSecondary().getDatabase());
}
}
两个库的配置信息已经完成。
对应可以共用
package com.example.model;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = -3258839839160856613L;
private String id;
private String userName;
private String passWord;
public User(String userName, String passWord) {
this.userName = userName;
this.passWord = passWord;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
'}';
}
}
对应的 Repository
package com.example.repository.primary;
import com.example.model.User;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface PrimaryRepository extends MongoRepository<User, String> {
}
package com.example.repository.secondary;
import com.example.model.User;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface SecondaryRepository extends MongoRepository<User, String> {
}
继承了 MongoRepository 会默认实现很多基本的增删改查,省了很多自己写 Repository 层的代码。
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootMultiMongodbApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMultiMongodbApplication.class, args);
}
}
package com.example.repository;
import com.example.model.User;
import com.example.repository.primary.PrimaryRepository;
import com.example.repository.secondary.SecondaryRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MuliDatabaseTest {
@Autowired
private PrimaryRepository primaryRepository;
@Autowired
private SecondaryRepository secondaryRepository;
@Test
public void TestSave() {
System.out.println("************************************************************");
System.out.println("测试开始");
System.out.println("************************************************************");
this.primaryRepository.save(new User("小张", "123456"));
this.secondaryRepository.save(new User("小王", "654321"));
List<User> primaries = this.primaryRepository.findAll();
for (User primary : primaries) {
System.out.println(primary.toString());
}
List<User> secondaries = this.secondaryRepository.findAll();
for (User secondary : secondaries) {
System.out.println(secondary.toString());
}
System.out.println("************************************************************");
System.out.println("测试完成");
System.out.println("************************************************************");
}
}
************************************************************
测试开始
************************************************************
2022-08-11 21:00:04.489 INFO 11760 --- [ main] org.mongodb.driver.connection : Opened connection [connectionId{localValue:3, serverValue:14}] to 127.0.0.1:27017
2022-08-11 21:00:04.508 INFO 11760 --- [ main] org.mongodb.driver.connection : Opened connection [connectionId{localValue:4, serverValue:15}] to 127.0.0.1:27017
User{id='6107c180c8a8b046b09d783c', userName='小张', passWord='123456'}
User{id='62f4fd54c8a8b02df08761ea', userName='小王', passWord='654321'}
************************************************************
测试完成
************************************************************
到此,MongoDB 多数据源的使用已经完成。