1.新建一個(gè)quickStart模塊
2.在com.springboot.quickstart包下建entity,dao,controller包如圖:
3.在entity包下建立book類(lèi)
package com.springboot.quickstart.entity;
public class Book {
private Integer id;
private String name;
private Double price;
public Book() {
}
public Book(Integer id, String name, Double price) {
this.id = id;
this.name = name;
this.price = price;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
'}';
}
}
4.在dao包內(nèi)建BookDAO類(lèi)
package com.springboot.quickstart.dao;
import com.springboot.quickstart.entity.Book;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class BookDAO {
public List<Book> getBook() {
List<Book> books = new ArrayList<>();
books.add(new Book(1, "SpringBoot實(shí)戰(zhàn)", 88.7));
books.add(new Book(2, "SpringMVC", 98.7));
books.add(new Book(3, "Spring", 80.7));
books.add(new Book(4, "SpringBoot", 100.2));
return books;
}
}
5.在controller內(nèi)建bookController類(lèi)
package com.springboot.quickstart.controller;
import com.springboot.quickstart.dao.BookDAO;
import com.springboot.quickstart.entity.Book;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
//注入一個(gè)springdao
@RestController
public class BookController {
@Resource
private BookDAO bookDAO;
@RequestMapping(value = "/books",method = RequestMethod.GET)
public List<Book> getBooks(){
return bookDAO.getBook();
}
}
6.運(yùn)行主類(lèi)quickstartApplication(一定要先運(yùn)行主類(lèi),才能用postman測(cè)試)
出現(xiàn)最后兩行字說(shuō)明運(yùn)行成功了叮趴,然后測(cè)試
出現(xiàn)200就說(shuō)明運(yùn)行成功