{"id":5203,"date":"2024-12-08T21:11:06","date_gmt":"2024-12-08T13:11:06","guid":{"rendered":"http:\/\/xinblog.ltd\/?p=5203"},"modified":"2024-12-09T21:23:28","modified_gmt":"2024-12-09T13:23:28","slug":"%e5%a6%82%e4%bd%95%e5%86%99%e6%b5%8b%e8%af%95-%e4%b8%83","status":"publish","type":"post","link":"http:\/\/xinblog.ltd\/?p=5203","title":{"rendered":"\u5982\u4f55\u5199\u6d4b\u8bd5 \u4e03"},"content":{"rendered":"<p>\u5982\u4f55\u5199\u6d4b\u8bd5 \u4e03<\/p>\n<p>\u5728\u8fd9\u4e00\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c31\u4ee5\u6211\u4eec\u4e4b\u524d\u5199\u8fc7\u7684Todo\u9879\u76ee\uff0c\u6539\u9020\u4e3a\u4e00\u4e2a\u5b9e\u9645\u63a5\u5165\u6570\u636e\u5e93\uff0c\u5e76\u901a\u8fc7Spring boot\u5bf9\u5916\u66b4\u9732web\u63a5\u53e3\u7684\u9879\u76ee\u3002\u5e76\u5bf9\u8fd9\u4e2a\u9879\u76ee\u8fdb\u884c\u96c6\u6210\u6d4b\u8bd5\u3002<\/p>\n<p>\u90a3\u4e48\u9996\u5148\uff0c\u6211\u4eec\u5148\u5b9e\u73b0Repository\u7684\u90e8\u5206\uff0c\u4e4b\u540e\u624d\u662fweb\u63a5\u53e3\u3002\u5bf9\u4e8eRepository\u7684\u5b9e\u73b0\uff0c\u5c06\u91c7\u7528MySQL\u4f5c\u4e3a\u6570\u636e\u5e93\uff0c\u5e76\u4e14\u4f7f\u7528Spring Data JPA\u4f5c\u4e3a\u6765\u7a0b\u5e8f\u8fde\u63a5\u6570\u636e\u5e93\u7684\u4f9d\u8d56\u9879\u3002<\/p>\n<p>\u8fd9\u91cc\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u6570\u636e\u5e93\u4e4b\u4e2d\u521b\u5efa\u51fa\u5bf9\u5e94\u7684Table<\/p>\n<table>\n<tbody>\n<tr>\n<td>CREATE TABLE todo_items (<\/p>\n<p>`id` int auto_increment,<\/p>\n<p>`content` varchar(255) not null,<\/p>\n<p>`done` tinyint not null default 0,<\/p>\n<p>primary key (`id`)<\/p>\n<p>) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u91cc\u6211\u4eec\u5c06\u5229\u7528id\u4f5c\u4e3a\u552f\u4e00\u6807\u8bc6\u3002<\/p>\n<p>\u7136\u540e\u5728Todo\u7684\u5b9e\u4f53\u4e4b\u4e2d\uff0c\u6211\u4eec\u4e5f\u6807\u8bb0\u4e86Id<\/p>\n<table>\n<tbody>\n<tr>\n<td>@Entity<\/p>\n<p>@Table(name = &#8220;todo_items&#8221;)<\/p>\n<p>public class TodoItem {<\/p>\n<p>@Id<\/p>\n<p>@Column(name = &#8220;id&#8221;)<\/p>\n<p>@GeneratedValue(strategy = GenerationType.IDENTITY)<\/p>\n<p>private long index;<\/p>\n<p>@Column<\/p>\n<p>private String content;<\/p>\n<p>@Column<\/p>\n<p>private boolean done;<\/p>\n<p>&#8230;<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7136\u540e\u6211\u4eec\u5148\u7f16\u5199\u4e00\u4e2aRepository\u7684\u63a5\u53e3\u3002<\/p>\n<p>\u56e0\u4e3a\u5229\u7528\u5230\u4e86Spring Data JPA,\u90a3\u4e48\u6211\u4eec\u53ea\u9700\u8981\u8ba9TodoItemRepository \u7ee7\u627f\u5b98\u65b9\u7684Repository\u7684\u63a5\u53e3\u5373\u53ef\u3002<\/p>\n<p>\u56e0\u4e3aSpring Data JPA\u4f1a\u5728\u5b9e\u9645\u8fd0\u884c\u7684\u65f6\u5019\u751f\u6210\u5bf9\u5e94\u7684\u793a\u4f8b\uff0c\u6240\u4ee5\u65e0\u9700\u7f16\u5199\u5bf9\u5e94\u7684\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<td>@ExtendWith(SpringExtension.class)<\/p>\n<p>@DataJpaTest<\/p>\n<p>@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)<\/p>\n<p>@TestPropertySource(&#8220;classpath:test.properties&#8221;)<\/p>\n<p>public class TodoItemRepositoryTest {<\/p>\n<p>@Autowired<\/p>\n<p>private TodoItemRepository repository;<\/p>\n<p>@Test<\/p>\n<p>public void should_find_nothing_for_empty_repository() {<\/p>\n<p>final Iterable&lt;TodoItem&gt; items = repository.findAll();<\/p>\n<p>assertThat(items).hasSize(0);<\/p>\n<p>}<\/p>\n<p>&#8230;<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u518d\u4e4b\u540e\u5c31\u662f\u6d4b\u8bd5\u7684\u76f8\u5173\u4ee3\u7801<\/p>\n<p>\u5728\u4e0a\u9762\u6211\u4eec\u901a\u8fc7Autowired\u6ce8\u89e3\u5c06\u4ed3\u5e93\u7c7b\u8fdb\u884c\u4e86\u6ce8\u5165\u3002<\/p>\n<p>\u5e76\u4e14\u901a\u8fc7ExtendWith\uff0c\u8fd9\u5229\u7528\u4e86Junit\u7684\u6269\u5c55\u673a\u5236\uff0c\u8ba9Spring \u53c2\u4e0e\u4e86\u6d4b\u8bd5\u3002\u5176\u4e2d\u58f0\u660e\u7684SpringExtension\u7c7b\u8fdb\u884c\u4e86\u4f9d\u8d56\u6ce8\u5165\u3002<\/p>\n<p>DataJpaTest \u8fd9\u4e2a\u6d4b\u8bd5\u91c7\u7528\u4e86Spring Data JPA\uff0c\u4ece\u800c\u5728\u5b9e\u9645\u8fd0\u884c\u7684\u65f6\u5019\uff0c\u6839\u636e\u63a5\u53e3\u5b9a\u4e49\uff0c\u83b7\u53d6\u5230\u4e86\u63a5\u53e3\u5b9e\u4f8b\u3002\u5e76\u4e14\u5176\u8fd8\u4f1a\u8d1f\u8d23\u8fd0\u884c\u4e4b\u540e\u7684\u6570\u636e\u56de\u6eda\u6e05\u7406\u64cd\u4f5c\u3002<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u5c06\u6846\u67b6\u548c\u5355\u5143\u6d4b\u8bd5\u96c6\u6210\uff0c\u5bf9\u6570\u636e\u5c42\u8fdb\u884c\u4e86\u4e00\u4e2a\u5c0f\u5c0f\u7684\u96c6\u6210\u6d4b\u8bd5\u3002<\/p>\n<p>\u518d\u4e4b\u540e\u662fController\u5c42\uff0c\u8fd9\u91cc\u6211\u4eec\u8003\u8651\u4f7f\u7528Rest\u98ce\u683c\u5bf9\u5916\u66b4\u9732\u63a5\u53e3\u3002<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u8003\u8651\u7ed9Controller\u5c42\u8bbe\u8ba1\u51e0\u4e2a\u63a5\u53e3\u6765\u5bf9\u5916\u66b4\u9732<\/p>\n<p>\u6dfb\u52a0Todo\u9879<\/p>\n<p>\u5b8c\u6210Todo\u9879<\/p>\n<p>Todo\u9879\u5217\u8868<\/p>\n<p>\u6dfb\u52a0\u8fd9\u91cc\u6211\u4eec\u76f4\u63a5\u4f7f\u7528POST\u63a5\u53e3\uff0c\u5e76\u4e14\u5229\u7528JSON\u7684\u65b9\u5f0f\u4f20\u5165\u4fdd\u5b58\u7684\u5185\u5bb9\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<td>POST \/todo-items<\/p>\n<p>{<\/p>\n<p>&#8220;content&#8221;: &#8220;foo&#8221;<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7136\u540e\u5c31\u662f\u5b8c\u6210\u4e00\u4e2aTodo\u9879\uff0c\u5229\u7528PUT\u63a5\u53e3\uff0c\u8fdb\u884c\u4fee\u6539<\/p>\n<table>\n<tbody>\n<tr>\n<td>PUT \/todo-items\/{index}<\/p>\n<p>{<\/p>\n<p>done: true<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6700\u540e\u662f\u83b7\u53d6Todo\u5217\u8868\uff0c\u90a3\u4e48\u5c31\u662f\u4e00\u4e2aGET\u8bf7\u6c42\uff0c\u83b7\u53d6\u6240\u6709\u7684\u503c<\/p>\n<table>\n<tbody>\n<tr>\n<td>GET \/todo-items?all=true<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u90a3\u4e48\u6211\u4eec\u5c31\u4e66\u5199\u76f8\u5173\u7684API\u4ee3\u7801<\/p>\n<table>\n<tbody>\n<tr>\n<td>@RestController<\/p>\n<p>@RequestMapping(&#8220;\/todo-items&#8221;)<\/p>\n<p>public class TodoItemResource {<\/p>\n<p>private TodoItemService service;<\/p>\n<p>@Autowired<\/p>\n<p>public TodoItemResource(final TodoItemService service) {<\/p>\n<p>this.service = service;<\/p>\n<p>}<\/p>\n<p>@PostMapping<\/p>\n<p>public ResponseEntity addTodoItem(@RequestBody final AddTodoItemRequest request) {<\/p>\n<p>if (Strings.isNullOrEmpty(request.getContent())) {<\/p>\n<p>return ResponseEntity.badRequest().build();<\/p>\n<p>}<\/p>\n<p>final TodoParameter parameter = TodoParameter.of(request.getContent());<\/p>\n<p>final TodoItem todoItem = this.service.addTodoItem(parameter);<\/p>\n<p>final URI uri = ServletUriComponentsBuilder<\/p>\n<p>.fromCurrentRequest()<\/p>\n<p>.path(&#8220;\/{id}&#8221;)<\/p>\n<p>.buildAndExpand(todoItem.getIndex())<\/p>\n<p>.toUri();<\/p>\n<p>return ResponseEntity.created(uri).build();<\/p>\n<p>}<\/p>\n<p>&#8230;<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u901a\u8fc7RestController\uff0c\u6765\u5728Spring\u4e2d\u58f0\u660e\u8fd9\u662f\u4e00\u4e2a\u5165\u53e3\u7c7b<\/p>\n<p>\u7136\u540e\u5229\u7528RequestMapping\u548cPostMapping\u6765\u58f0\u660e\u670d\u52a1\u8def\u5f84\u3002<\/p>\n<p>\u5176\u4e2d\u6211\u4eec\u8fd8\u5229\u7528RequestBody\u6765\u58f0\u660e\u4e86\u4e00\u4e2a\u8bf7\u6c42\u4f53\u3002<\/p>\n<p>\u90a3\u4e48\u6211\u4eec\u9700\u8981\u8fdb\u884c\u76f8\u5173\u7684\u6d4b\u8bd5<\/p>\n<table>\n<tbody>\n<tr>\n<td>@SpringBootTest<\/p>\n<p>@AutoConfigureMockMvc<\/p>\n<p>@Transactional<\/p>\n<p>public class TodoItemResourceTest {<\/p>\n<p>@Autowired<\/p>\n<p>private MockMvc mockMvc;<\/p>\n<p>@Autowired<\/p>\n<p>private TodoItemRepository repository;<\/p>\n<p>&#8230;<\/p>\n<p>.<\/p>\n<p>@Test<\/p>\n<p>public void should_add_item() throws Exception {<\/p>\n<p>String todoItem = &#8220;{ &#8221; +<\/p>\n<p>&#8220;\\&#8221;content\\&#8221;: \\&#8221;foo\\&#8221;&#8221; +<\/p>\n<p>&#8220;}&#8221;;<\/p>\n<p>mockMvc.perform(MockMvcRequestBuilders.post(&#8220;\/todo-items&#8221;)<\/p>\n<p>.contentType(MediaType.APPLICATION_JSON)<\/p>\n<p>.content(todoItem))<\/p>\n<p>.andExpect(status().isCreated());<\/p>\n<p>assertThat(repository.findAll()).anyMatch(item -&gt; item.getContent().equals(&#8220;foo&#8221;));<\/p>\n<p>}<\/p>\n<p>&#8230;<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5176\u4e2d\u6211\u4eec\u4ecd\u7136\u662f\u5229\u7528SpringBootTest\u6ce8\u89e3\uff0c\u6765\u58f0\u660e\u8fd9\u4e00\u6b21\u5c06\u6240\u6709\u7684\u7ec4\u4ef6\u7ec4\u5408\u7684\u96c6\u6210\u6d4b\u8bd5<\/p>\n<p>\u4e4b\u540e\u662fAutoConfigureMockMvc\uff0c\u58f0\u660e\u5185\u90e8\u5229\u7528\u4e86\u6a21\u62df\u7684\u7f51\u7edc\u73af\u5883<\/p>\n<p>\u7136\u540e\u662fTransactional\uff0c\u8bf4\u660e\u6d4b\u8bd5\u662f\u4e8b\u52a1\u6027\u7684\uff0c\u5176\u4e2d\u751f\u6210\u7684\u6570\u636e\u5728\u65e5\u540e\u8981\u8fdb\u884c\u56de\u6eda\u3002<\/p>\n<p>\u518d\u4e4b\u540e\u5229\u7528mockMvc\u6765\u8fdb\u884c\u76f8\u5173\u7684\u8bf7\u6c42\u6a21\u62df\uff0c\u58f0\u660e\u4e86\u8bf7\u6c42\u7684\u8def\u5f84\uff0c\u53c2\u6570\u7c7b\u578b\u548c\u53c2\u6570\u3002<\/p>\n<p>\u5e76\u4e14\u5728\u67e5\u8be2\u6570\u636e\u5e93\u5224\u65ad\u662f\u5426\u6709\u4f20\u5165\u7684todo\u9879\u3002<\/p>\n<p>\u90a3\u4e48\u603b\u7ed3\u4e00\u4e0b\uff0c\u6211\u4eec\u5c06\u539f\u672c\u7684Todo\u5e94\u7528\u4ece\u4e00\u4e2a\u547d\u4ee4\u884c\u5e94\u7528\u6269\u5c55\u4e3a\u4e00\u4e2aREST\u670d\u52a1\uff0c\u5176\u4e2d\u589e\u52a0\u4e00\u4e2aRepository\u548c\u4e00\u4e2aController\u5c42\u3002<\/p>\n<p>\u4e4b\u540e\u5c06\u4e24\u8005\u8fdb\u884c\u4e86\u96c6\u6210\u6d4b\u8bd5\uff0c\u5e76\u4e14\u5728\u5176\u4e2d\u4fdd\u8bc1\u4e86\u6570\u636e\u56de\u6eda\u3002\u4fdd\u8bc1\u4e86\u6570\u636e\u7684\u53ef\u6d4b\u8bd5\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5199\u6d4b\u8bd5 \u4e03 \u5728\u8fd9\u4e00\u7ae0\u4e2d\uff0c\u6211 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[],"_links":{"self":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5203"}],"collection":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5203"}],"version-history":[{"count":1,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5203\/revisions"}],"predecessor-version":[{"id":5204,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5203\/revisions\/5204"}],"wp:attachment":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5203"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}