- 服务层代码
package com.itheima.controller;import com.itheima.domain.Book;import com.itheima.service.BookService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import java.util.List;// 标记为Bean// 采用REST书写方式@RestController// 设置总体路径前缀@RequestMapping("/books")public class BookController { // 自动装配 @Autowired private BookService bookService; // 新添采用POST请求 // 数据位于请求体,采用@RequestBody @PostMapping public boolean save(@RequestBody Book book) { return bookService.save(book); } // 更新采用PUT请求 // 数据位于请求体,采用@RequestBody @PutMapping public boolean update(@RequestBody Book book) { return bookService.update(book); } // 删除采用DELETE请求 // 数据位于请求路径,采用@PathVariable,并在路径采用{}表示 @DeleteMapping("/{id}") public boolean delete(@PathVariable Integer id) { return bookService.delete(id); } // 访问采用GET请求 // 数据位于请求路径,采用@PathVariable,并在路径采用{}表示 @GetMapping("/{id}") public Book getById(@PathVariable Integer id) { return bookService.getById(id); } // 访问采用GET请求 @GetMapping public List<Book> getAll() { return bookService.getAll(); }}案例测试阶段
- Java内部代码测试
// 测试均实现于test文件夹下package com.itheima.service;import com.itheima.config.SpringConfig;import com.itheima.domain.Book;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.util.List;// 测试采用的junit@RunWith(SpringJUnit4ClassRunner.class)// 测试采用的配置文件@ContextConfiguration(classes = SpringConfig.class)public class BookServiceTest { // 自动装配 @Autowired private BookService bookService; // 查询id=1的值 @Test public void testGetById(){ Book book = bookService.getById(1); System.out.println(book); } // 查询所有数据 @Test public void testGetAll(){ List<Book> all = bookService.getAll(); System.out.println(all); }}
经验总结扩展阅读
-
不入虎穴焉得虎子是什么意思 不入虎穴焉得虎子的解释
-
-
保湿|这些保湿修复乳液补水保湿、细致毛孔、提亮肤色,你喜欢哪款?
-
李渊驾崩前留给李世民的遗言是什么,为什么李世民要当耳旁风呢?
-
-
202010月10日流星雨几点 是什麽星座的流星雨
-
-
-
-
哥哥按时打钱给老人养老|哥哥按时打钱给老人养老,弟弟赖在家不出门,老人却埋怨哥哥不孝
-
-
-
-
-
-
-
-
-
仲念念|#情感#观点系列插图:电视剧《微微一笑很倾城》剧照01.|你爱的人和爱你的人,你更愿意和谁结婚?
-