mongoDB是一个使用非常广的非关系型数据库,在springboot中,可以非常方便的实现CRUD操作。
1.pom配置
目前大多数项目都用maven进行依赖包管理,因此,本文也采用maven进行配置。pom需要增加如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- MongoDB -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<!-- junit test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
<scope>provided</scope>
</dependency>
主要的依赖包有三个:
spring-boot-starter 核心启动包,spring-boot-starter-data-mongodb mongoDB的相关依赖包,spring-boot-starter-test junit单元测试包。
其他的包如 lombok 则是为了省略部分代码。
2.数据模型类
package com.dhb.domain;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import lombok.Data;
@Document(collection = "users")
@Data
public class User {
@Id
private String id;
private String name;
private int age;
public User(String name,int age){
this.name = name;
this.age = age;
}
}
通过lombok的@Data注解,省略了get和set方法。
@Document 表示映射到mongoDB中的表。collection = "users" 通过collection进行指定。
@Id 映射mongoDB中的ID字段。
3.cuid方法Repository
springBoot提供了一个MongoRepository接口,只需要继承该接口即可。亦可自定义方法。
package com.dhb.domain;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface UserMongoRepository extends MongoRepository<User, Integer> {
User findByName(String name);
}
4.junit单元测试
测试代码如下:
package com.dhb.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import org.junit.After;
import org.junit.Before;
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.SpringJUnit4ClassRunner;
import com.dhb.domain.User;
import com.dhb.domain.UserMongoRepository;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class UserMongoRepositoryTest {
@Autowired
private UserMongoRepository userMongoRepository;
@Before
public void setUp() throws Exception {
User user1 = new User("user1", 19);
User user2 = new User("user2", 20);
// save product, verify has ID value after save
assertNull(user1.getId());
assertNull(user2.getId());// null before save
this.userMongoRepository.save(user1);
this.userMongoRepository.save(user2);
assertNotNull(user1.getId());
assertNotNull(user2.getId());
}
@Test
public void testFetchData() {
/* Test data retrieval */
User userA = userMongoRepository.findByName("user1");
assertNotNull(userA);
assertEquals(19, userA.getAge());
/* Get all products, list should only have two */
Iterable<User> users = userMongoRepository.findAll();
int count = 0;
for (User p : users) {
count++;
}
assertEquals(count, 2);
}
@Test
public void testDataUpdate() {
/* Test update */
User userB = userMongoRepository.findByName("user2");
userB.setAge(40);
userMongoRepository.save(userB);
User userC = userMongoRepository.findByName("user2");
assertNotNull(userC);
assertEquals(40, userC.getAge());
}
@After
public void tearDown() throws Exception {
this.userMongoRepository.deleteAll();
}
}
执行结果: