MongoDB基本使用
概要
- MongoDB是什么
- NoSql是什么
- 为什么要使用NoSql
- MongoDB的特点
- Spring整合MongoDB
- springboot项目
- 非springboot项目
- 相关实体类注解的解释
MongoDB简介
1.MongoDB是什么
- MongoDB是使用C++编写,开源的,面向文档的NoSql(Not Only SQL)数据库
2.NoSql是什么
- NoSql(not only sql)是非关系型数据库的统称,常见的NoSql有Redis,MongoDB,Hbase,Cassandra等
3.为什么要使用NoSql
为了解决常规数据库以下问题
- 高并发下读写压力大
- 海量数据的高效存储和访问
- 数据库的高可用和高拓展性
4.MongoDB的特点
- 高性能易于使用,易于拓展
- 面向集合存储
- 支持动态查询,支持索引
- 支持分片
5.Spring整合MongoDB
springboot项目
依赖
1
2
3
4
5
6
7
8
9<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>在application.perproties/yml配置mongodb的连接地址
1
spring.data.mongodb.uri=mongodb://127.0.0.1:27017/myMongoDB
创建存储的User实体
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21public class User {
private Long id;
private String username;
private Integer age;
public User(Long id, String username, Integer age) {
this.id = id;
this.username = username;
this.age = age;
}
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", age=" + age +
'}';
}
}实现User的数据访问对象
1
2
3public interface UserRepository extends MongoRepository<User, Long> {
User findByUsername(String username);
}测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
.class) (SpringRunner
public class MongodbConnectionTestTest {
private UserRepository userRepository;
protected MongoTemplate mongoOperations;
public void test() throws Exception {
// 创建三个User,并验证User总数
userRepository.save(new User(1L, "didi", 30));
userRepository.save(new User(2L, "mama", 40));
userRepository.save(new User(3L, "kaka", 50));
Assert.assertEquals(3, userRepository.findAll().size());
// 删除一个User,再验证User总数
User u = userRepository.findOne(1L);
userRepository.delete(u);
Assert.assertEquals(2, userRepository.findAll().size());
// 删除一个User,再验证User总数
u = userRepository.findByUsername("mama");
userRepository.delete(u);
Assert.assertEquals(1, userRepository.findAll().size());
}
public void test1() throws Exception {
List<User> users = mongoOperations.findAll(User.class);
users.forEach(System.out::println);
}
}- 非springboot项目
- 依赖
1
2
3
4
5
6
7
8
9
10<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.14.2</version>
</dependency> - xml配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">
<mongo:mongo-client id="mongoClient" host="127.0.0.1" port="27017">
<mongo:client-options
connections-per-host="8"
threads-allowed-to-block-for-connection-multiplier="4"
connect-timeout="1000"
max-wait-time="1500"
socket-keep-alive="true"
socket-timeout="1500"
/>
</mongo:mongo-client>
<mongo:db-factory id="mongoDbFactory"
dbname="myMongoDB"
mongo-ref="mongoClient"/>
<mongo:template id="mongoTemplate" db-factory-ref="mongoDbFactory" write-concern="NORMAL"/>
<mongo:repositories base-package="com.xxx.core.modules.*.repository"/>
</beans> - 创建存储的User实体
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21public class User {
private Long id;
private String username;
private Integer age;
public User(Long id, String username, Integer age) {
this.id = id;
this.username = username;
this.age = age;
}
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", age=" + age +
'}';
}
} - 实现User的数据访问对象
1
2
3public interface UserRepository extends MongoRepository<User, Long> {
User findByUsername(String username);
} - 测试
6.相关实体类注解的解释
1 | - 文档的唯一标识,在mongodb中为ObjectId,它是唯一的,通过时间戳+机器标识+进程ID+自增计数器(确保同一秒内产生的Id不会冲突)构成。 |