美文网首页
1.springBoot起步练习

1.springBoot起步练习

作者: 王杰磊 | 来源:发表于2019-03-18 19:11 被阅读0次

    1.新建一个quickStart模块

    image.png

    2.在com.springboot.quickstart包下建entity,dao,controller包如图:

    image.png

    3.在entity包下建立book类

    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包内建BookDAO类

    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实战", 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内建bookController类

    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;
    
    //注入一个springdao
    @RestController
    public class BookController {
        @Resource
        private BookDAO bookDAO;
    
        @RequestMapping(value = "/books",method = RequestMethod.GET)
        public List<Book> getBooks(){
            return bookDAO.getBook();
        }
    }
    

    6.运行主类quickstartApplication(一定要先运行主类,才能用postman测试)

    image.png

    出现最后两行字说明运行成功了,然后测试


    image.png

    出现200就说明运行成功

    相关文章

      网友评论

          本文标题:1.springBoot起步练习

          本文链接:https://www.haomeiwen.com/subject/kbwbmqtx.html