1. 简介
本教程将通过一个简单的示例演示如何使用SpringBoot和DoytoQueryMongoDB创建一个增删查改应用来访问MongoDB数据库。
测试数据来自MongoDB的查询文档。
[
{"item": "journal", "qty": 25, "size": {"h": 14, "w": 21, "uom": "cm"}, "status": "A"},
{"item": "notebook", "qty": 50, "size": {"h": 8.5, "w": 11, "uom": "in"}, "status": "A"},
{"item": "paper", "qty": 100, "size": {"h": 8.5, "w": 11, "uom": "in"}, "status": "D"},
{"item": "planner", "qty": 75, "size": {"h": 22.85, "w": 30, "uom": "cm"}, "status": "D"},
{"item": "postcard", "qty": 45, "size": {"h": 10, "w": 15.25, "uom": "cm"}, "status": "A"}
]
2. 基于SpringBoot和DoytoQueryMongoDB构建增删查改应用
2.1. 在Spring Initializer上初始化工程,添加以下4个依赖:
- Lombok
- Validation
- Spring Web
- Embedded MongoDB Database
2.2. 引入DoytoQuery依赖
在pom.xml中添加如下依赖:
<dependencies>
<dependency>
<groupId>win.doyto</groupId>
<artifactId>doyto-query-mongodb</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>win.doyto</groupId>
<artifactId>doyto-query-web</artifactId>
<version>1.0.0</version>
</dependency>
...
</dependencies>
2.3. 添加默认Web配置
DemoApplication需要继承win.doyto.query.web.WebMvcConfigurerAdapter
package win.doyto.query.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import win.doyto.query.web.WebMvcConfigurerAdapter;
@SpringBootApplication
public class DemoApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(DoytoQueryDemoApplication.class, args);
}
}
2.4. 编写业务类
编写InventorySize.java和InventoryEntity.java用于数据模型的映射。
@Getter
@Setter
public class InventorySize {
private Double h;
private Double w;
private String uom;
}
@Getter
@Setter
@Entity(type = EntityType.MONGO_DB, database = "doyto", name = "c_inventory")
public class InventoryEntity extends MongoPersistable<String> {
private String item;
private Integer qty;
private InventorySize size;
private String status;
}
编写SizeQuery.java和InventoryQuery.java用于数据查询。
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SizeQuery implements NestedQuery {
@JsonProperty("hLt")
private Double hLt;
private String uom;
}
@Getter
@Setter
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class InventoryQuery extends PageQuery {
private String itemContain;
private String status;
private SizeQuery size;
}
编写InventoryController.java,提供增删查改接口。
@RestController
@RequestMapping("inventory")
public class InventoryController extends AbstractEIQController<InventoryEntity, String, InventoryQuery> {
}
2.6. 编写测试进行验证
@AutoConfigureMockMvc
@SpringBootTest(properties = {"spring.mongodb.embedded.version=5.0.5"})
class InventoryMvcTest {
@Resource
protected MockMvc mockMvc;
@BeforeAll
static void beforeAll(@Autowired MockMvc mockMvc) throws Exception {
String data = "[" +
" {\"item\": \"journal\", \"qty\": 25, \"size\": {\"h\": 14, \"w\": 21, \"uom\": \"cm\"}, \"status\": \"A\"}," +
" {\"item\": \"notebook\", \"qty\": 50, \"size\": {\"h\": 8.5, \"w\": 11, \"uom\": \"in\"}, \"status\": \"A\"}," +
" {\"item\": \"paper\", \"qty\": 100, \"size\": {\"h\": 8.5, \"w\": 11, \"uom\": \"in\"}, \"status\": \"D\"}," +
" {\"item\": \"planner\", \"qty\": 75, \"size\": {\"h\": 22.85, \"w\": 30, \"uom\": \"cm\"}, \"status\": \"D\"}," +
" {\"item\": \"postcard\", \"qty\": 45, \"size\": {\"h\": 10, \"w\": 15.25, \"uom\": \"cm\"}, \"status\": \"A\"}" +
"]";
mockMvc.perform(post("/inventory/").content(data).contentType(MediaType.APPLICATION_JSON));
}
@Test
void queryExamples() throws Exception {
mockMvc.perform(get("/inventory/?itemContain=book"))
.andExpect(jsonPath("$.data.total").value(1));
mockMvc.perform(get("/inventory/?status=A"))
.andExpect(jsonPath("$.data.total").value(3));
mockMvc.perform(get("/inventory/?size.hLt=12&status=A"))
.andExpect(jsonPath("$.data.total").value(2))
.andExpect(jsonPath("$.data.list[*].item",
containsInRelativeOrder("notebook", "postcard")));
mockMvc.perform(get("/inventory/?size.uom=in"))
.andExpect(jsonPath("$.data.total").value(2))
.andExpect(jsonPath("$.data.list[*].item",
containsInRelativeOrder("notebook", "paper")));
}
}
3. 小结
在这篇教程中,介绍了如何基于SpringBoot和DoytoQueryMongoDB快速构建一套增删查改应用来访问存储于MongoDB的数据。
本文示例代码请访问GitHub。