SpringBoot入门学习(含代码)

来源:互联网 发布:2017勇士vs骑士g3数据 编辑:程序博客网 时间:2024/05/17 02:42

- SpringBoot是什么:
是什么并没有具体概念,自我理解像是一种工具,帮助开发者更容易的创建基于Spring的应用程序和服务,简化了开发过程。

- 为什么SpringBoot能简化开发
传统的基于SprngMVC的项目需要添加很多jar包,并且要考略jar包之间的兼容问题,并且要写很多的配置文件。而SpringBoot不再需要添加各种各样的jar包,起步依赖(spring-boot-starter-*)将去管理各种各样的jar包(传递依赖),举个例子比如我们搭建web项目,就需要加入web的起步依赖spring-boot-starter-web,它的生成树:

    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-tomcat</artifactId>        </dependency>        <dependency>            <groupId>org.hibernate</groupId>            <artifactId>hibernate-validator</artifactId>        </dependency>        <dependency>            <groupId>com.fasterxml.jackson.core</groupId>            <artifactId>jackson-databind</artifactId>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>        </dependency>    </dependencies>

里面包括了我们以前不使用SpringBoot开发项目所添加的依赖。并且SpringBoot的自动配置免除了很多spring配置文件,我们不再需要写很多配置文件了。

- 熟悉SpringBoot项目的结构
通过idea可以快速搭建SpringBoot项目,并且它还是一个maven项目(如何快速搭建可以百度,这里就不做描述)。和传统的maven项目类似,只不过多了几个模块和少了几个模块。
具体结构为:
这里写图片描述
和传统的maven项目相比,它没有了webapp,多了static和templates。
static放静态资源,比如css等
templates放html文件
applacation.properties,生成的时候为空,实际上也不需要它,因为SpringBoot将为我们自动配置。它存在的意义是可以帮我们细粒度的调整SpringBoot的自动配置。它将被SpringBoot自动加载,被应用程序所应用
SpringBootApplication:每个SpringBoot项目都会生成一个项目名Application这样一个文件(可以说是一个启动类,通过这句
SpringApplication.run(SpringbootApplication.class, args);启动应用程序。也是一个配置类,spring的简单配置可以以java代码的形式写在这里),它的作用为开启自动扫描和自动配置功能。实现这个功能是因为@SpringBootApplication这个注解,这个注解集成了三个注解,分别是@Configuration,@ComponentScan(开启组件扫描,使各种组件被自动发现并注册为Spring应用程序上下文里的Bean),@EnableAutoConfiguration(开启自动配置)。
test:测试类和测试类所需要的资源文件

- 一个简单的springboot项目:
Controller:

    @Controller    @RequestMapping("/readingList")    public class ReadingListController {        @Autowired        private ReadingListRepository readingListRepository;        @RequestMapping(value = "/{reader}", method = RequestMethod.GET)        public String readersBooks(@PathVariable("reader") String reader, Model model) {            List<Book> readingList = readingListRepository.findByReader(reader);            if(readingList != null) {                model.addAttribute("books", readingList);            }            return "readingList";        }        @RequestMapping(value = "/{reader}/saveBook", method = RequestMethod.POST)        public String addToReadingList(@PathVariable("reader") String reader, Book book) {            book.setReader(reader);            readingListRepository.save(book);            return "redirect:/readingList/{reader}";        }    }

语法注解什么和SpringBoot和传统的SpringMVC项目一样,不多说

Entity:

@Entitypublic class Book {    @Id    @GeneratedValue(strategy = GenerationType.AUTO)    private Long id;    private String reader;    private String isbn;    private String title;    private String author;    private String description;    public Book(Long id, String reader, String isbn,                String title, String author, String description) {        this.id = id;        this.reader = reader;        this.isbn = isbn;        this.title = title;        this.author = author;        this.description = description;    }    public Long getId() {        return id;    }    public void setId(Long id) {        this.id = id;    }    public String getReader() {        return reader;    }    public void setReader(String reader) {        this.reader = reader;    }    public String getIsbn() {        return isbn;    }    public void setIsbn(String isbn) {        this.isbn = isbn;    }    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public String getAuthor() {        return author;    }    public void setAuthor(String author) {        this.author = author;    }    public String getDescription() {        return description;    }    public void setDescription(String description) {        this.description = description;    }    @Override    public String toString() {        return "Book{" +                "id=" + id +                ", reader='" + reader + '\'' +                ", isbn='" + isbn + '\'' +                ", title='" + title + '\'' +                ", author='" + author + '\'' +                ", description='" + description + '\'' +                '}';    }}

简单的javabean,因为用了jpa,所以用@Entity表示,正式做项目的时候应该是不会有这些注解的

Repository:

public interface ReadingListRepository extends JpaRepository<Book, Long> {    List<Book> findByReader(String reader);}

就相当于我们以前做的项目中的Service,定义了接口。

Web界面省略:

到这里我们基本熟悉了SpringBoot是如何简化开发的,差不多熟悉了项目结构以及如何自己写一个简单的SpringBoot项目了

附:github地址
https://github.com/TiantianUpup/SpringBootSourceCode/tree/master/ch02

原创粉丝点击