- 服务层代码
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); }}
经验总结扩展阅读
-
居家艺术 迷迭香不好养活,也不见开花?选这个品种试试,它香味浓好养活最重要的是花期还长
-
-
皮鞋二哈拆家不小心翻出男主人的皮鞋,里面竟藏2万块:看你干的好事
-
-
-
-
|大富豪“的士招”:为何10亿遗产都归二房,原配9个子女一分没有
-
-
-
-
-
-
文/七月长安-网上有句话说得好:“男人是重情重义的|让男人动情的女人,多半做到了这3点
-
2022年农历九月初七宜挖坟墓吗 2022年10月2日是挖坟墓吉日吗
-
星座不求人|12星座下周运势分析(7月13日~7月18日)
-
-
-
-
-