1.entity(Book类)
public class Book {
private Integer id;
private String name;
private Double pice;
public Book(){}
public Book(Integer id, String name, Double pice) {
this.id = id;
this.name = name;
this.pice = pice;
}
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 getPice() {
return pice;
}
public void setPice(Double pice) {
this.pice = pice;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", pice=" + pice +
'}';
}
}
2.dao(BookDAO)
@Component
public class BookDAO {
public List<Book> getBooks(){
List<Book> books = new ArrayList <>();
books.add(new Book(1,"stringboot",88.7));
books.add(new Book(1,"stringboot",88.7));
books.add(new Book(1,"stringboot",88.7));
return books;
}
}
3.Controller(BookController)
@RestController
public class BookController {
/**
*
*/
//注入dao实例
@Resource
private BookDAO bookDAO;
@RequestMapping(value = "/books" ,method = RequestMethod.GET)
public List<Book> getBooks(){
return bookDAO.getBooks();
}
}
4.运行并且进行测试
网友评论