SpringBoot 目的是简化Spring应用的初始搭建和开发过程。
特点
- 创建独立的额spring应用程序
- 内嵌tomcat 不用配置war文件
- 简化Maven的配置(可以做到无xml配置)
- 自动配置Spring
- 提供生产就绪功能
用idea搭建springboot环境 web下
- 点击新建项目,如下图, 点击next
- 输入分组和名字,点击next
- 选择这三个依赖,选择next
- 输入项目名和路径 就可建立起来
- 结果:
springboot的代码结构
代码层的结构
根目录:com.springboot
- 工程启动类(ApplicationServer.java)置于com.springboot.build包下
- 实体类(domain)置于com.springboot.domain
- 数据访问层(Dao)置于com.springboot.repository
- 数据服务层(Service)置于com,springboot.service,数据服务的实现接口(serviceImpl)至于com.springboot.service.impl
- 前端控制器(Controller)置于com.springboot.controller
- 工具类(utils)置于com.springboot.utils
- 常量接口类(constant)置于com.springboot.constant
- 配置信息类(config)置于com.springboot.config
- 数据传输类(vo)置于com.springboot.vo
资源文件的结构
根目录:src/main/resources
- 配置文件(.properties/.json等)置于config文件夹下
- spring.xml置于META-INF/spring文件夹下
- 页面以及js/css/image等置于static文件夹下的各自文件下
- templates:放置网页,页面
- application.properties 文件是配置文件
Springboot将很多的配置文件进行了统一的管理。并且配置了默认值。(有默认值 记住默认值并配置即可)
springboot会自动寻找并运用配置,(找的是application.properties 和 application.yml),否则使用默认配置
两种配置文件的区别:
application.properties
server.prot = 8080
application.yml
server:
port:8080
.properties 优先级高于 .yml 文件
springboot的入口类
@SpringBootApplication
public class SpringbootdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootdemoApplication.class, args);
}
}
@SpringBootApplication 是一个组合的注解,包含有
- @EnableAutoConfiguration
- @ComponentScan
- @SpringBootConfiguration
三个注解,springboot会扫描这个类路径下的所有包
pom文件基本的配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springbootdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootdemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- spring-boot-starter-parent : 用于提供Maven默认依赖
- spring-boot-starter-web : 提供web服务
- spring-boot-starter-test:和测试相关的包
- spring-boot-maven-plugin:maven的插件,为springboot提供maven操作,打包等。