微框架,与 Spring4 一块儿诞生,基于约定、生来为了简化 spring 的配置html
须要的Java版本:1.8+前端
起步依赖实际上就是一个 Maven 项目对象模型,定义了对其余库的传递依赖。这些东西加在一块儿支持某项功能。从必定程度上规避了依赖冲突问题java
对于一些约定的属性,springboot 在 spring-boot-autoconfigure 包下 META-INF/spring-configuration-metadata.json 文件中进行了默认属性配置。若是咱们不经过配置文件覆盖这个配置,在应用程序启动时,若是应用程序启动条件符合注解的要求,就会采用这些默认配置来完成应用的初始化配置。若是咱们覆盖这个配置,就会采用咱们定义的配置mysql
原理分析:web
@SpringBootConfiguration // 至关于 @Configuration @EnableAutoConfiguration // 开启自动配置 @ComponentScan( // 配置注解扫描。扫描该包及其子包的注解 excludeFilters = {@Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class} ), @Filter( type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class} )} ) public @interface SpringBootApplication {}
使用IDEA能够快速的建立 springboot 项目,固然也能够经过建立 Maven 工程并导入依赖来新建 springboot 项目redis
快速建立的工程只能选择最新的几个版本,若是想使用老版本能够在工程搭建完成后手动更改版本号spring
SpringBoot使用一个全局的配置文件,而且名称是固定的,配置文件有两种(截图自来自spring-boot-starter-parent-1.5.9.RELEASE.pom):sql
由该 pom 文件也能够得出一个结论,当同时存在 .yml 和 .properties 配置文件且配置了相同的参数时,会由于后加载 properties 而致使 yml 里面的相同配置配覆盖。固然实际开发也几乎不会有人这么作数据库
application.properties 就是常规的 key=value 格式配置文件,当要配置的参数比较多就会发现他的层次不是那么清晰,不便于阅读apache
yml(也叫yaml):是一种以数据为中心的配置文件, 比 json,xml 等更适合作配置文件
key:(空格)value ---> 键和值中间用冒号空格!!!链接,记住是冒号空格,缺一不可
不一样层级的关系用空格表示,只要是左对齐的一列数据,都是同一层级的:
server: port: 8888
默认不用加引号,
若是加上 “” 双引号,双引号内的特殊字符将做为自己的意思展现
若是加上 ‘’ 单引号,单引号内的特殊字符将会被转义
在下一行来写对象的属性和值的关系;注意缩进
user: name: yaya age: 18 address: xian firends: {name: zhangsan, age: 18} # map里面的 冒号后面也得有 空格
用 - 值表示数组中的一个元素
arr: - 1 - 2 - 3
person: # 前缀名 name: yaya age: 18 address: 西安 arr: - 1 - 2 - 3 friend: {name: zs,age: 13} son: name: 张三 age: 13
@Component // 配置 Bean 被 Spring 容器管理 @ConfigurationProperties(prefix = "person") // 配置文件和实体进行映射,配置前缀,这里对应 yml 文件中的对象名 public class User { private String name; private int age; private String address; private List<Integer> arr; private Map<String, Object> friend; private Son son; // 引入一个外部类 setter/getter ... }
public class Son{ // 该类不用加任何注解,框架仍是会将 yml 中的属性映射到该类的属性上 private String name; private int age; }
@Value 获取值和 @ConfigurationProperties 获取值的比较:
@ConfigurationProperties | @Value | |
---|---|---|
功能 | 批量注入配置文件中的属性 | 一个个指定 |
松散绑定(松散语法) | 支持 | 不支持 |
SpEL | 不支持 | 支持 |
JSR303数据校验 | 支持 | 不支持 |
复杂类型封装 | 支持 | 不支持 |
public class Person { private String name; @JsonIgnore // 转换时忽略该字段 private Integer age; // Json格式化 @JsonFormat(pattern = "yyyy年MM月dd日 HH:mm:ss", locale = "zh", timezone = "GMT+8") private Date birthday; }
@RequestMapping("/person") public Person person(){ Person p = new Person(); p.setName("张三"); p.setAge(23); p.setBirthday(new Date()); System.out.println(p); return p; }
这时,返回的JSON数据中就不会出现 age 属性,而且对 birthday 进行了格式化
{"name":"张三","birthday":"2019年08月07日 15:34:45"}
忽略null属性
若是前端须要将 null 返回为空串/不返回,咱们可使用。
@JsonInclude(content = JsonInclude.Include.NON_NULL) // 若是该属性为 null,则它不参与序列化
注意:在 spring-boot 1.5.9 版本中, @JsonInclude 注解没有对 value 和 content 关联(没有在 content 上配置 @AliasFor 注解),因此刚刚上面的配置是无效的。采用下面的配置:
@JsonInclude(JsonInclude.Include.NON_NULL)
也能够在 yml 文件中配置全局忽略,配置方法以下:
spring: jackson: default-property-inclusion: non_null
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <!-- 关闭此项的依赖传递,即别的项目依赖该项目时,不会传递依赖此jar --> <optional>true</optional> </dependency>
spring: freemarker: cache: true # 关闭thymeleaf的页面缓存 devtools: remote: restart: enabled: false # 热部署开关 restart: additional-paths: springboot-demo/src/main/java # 设置重启的目录,对那个目录的文件进行修改后须要重启 exclude: static/** # 设置classpath下 static 目录内容修改后不重启。通常设置为静态资源目录
有时咱们采用一些本身定义的资源文件(非 application.xxx )想要获取里面的属性值时,须要采用如下配置
<!-- 配置文件处理器依赖,配置后能够进行资源配置文件的加载 --> <!-- 配置这个依赖后,书写yml文件时自定义的属性会有提示 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
@Configuration // 也是一个 @Component 语义化注解 @ConfigurationProperties(value = "admin") // 配置文件中共有的前缀名 @PropertySource(value = "classpath:user.properties") // 资源文件的位置 public class Admin implements Serializable { private String username; private String password; }
server: port: 8888 # 端口号 session-timeout: 60 # session 超时时间/分钟,默认是30 context-path: /demo # 全局虚拟路径 error: path: /error.html # 错误跳转页 tomcat: uri-encoding: utf-8 # 设置tomcat编码
导入FreeMarker启动器
<!-- freemarker --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
配置 freemarker
spring: freemarker: cache: false # 关闭freemarker缓存。即时刷新。上线环境建议修改成 true template-loader-path: classpath:/template # 模板文件的路径 charset: UTF-8 # 编码,默认也是u8 check-template-location: true # 检查模板路径 content-type: text/html # 默认也是 text/html
添加 mybatis 起步依赖,mysql 数据库依赖
<!-- mybatis起步依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!-- mysql数据库依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
在 yml 文件中配置数据源
spring: datasource: username: keats password: 521 url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC driver-class-name: com.mysql.jdbc.Driver # 对于mysql数据库链接版本 v6+ 这个驱动要修改为 com.mysql.cj.jdbc.Driver。固然也能够什么都不写用默认的
MyBatis相关配置
mybatis: mapper-locations: classpath:mapping/*Mapping.xml # 配置 mapper 文件所在的路径 type-aliases-package: cn.keats.mybatisdemo.pojo # 配置这个包下的全部类起别名
建立实体类
public class User implements Serializable { private Integer id; private String username; private String password; private Date birthday; setter/getter ... }
建立映射关系接口
接口要添加 @Mapper 注解,这样容器中才会有接口的实现类
@Mapper // public interface UserMapper { @Select("select * from user where id = #{id}") User selectById(Integer id); // 采用注解的方式书写SQL语句 void insert(User user); // 采用mapper配置文件的方式书写SQL语句 }
Mapper映射文件,四个要求
namespace 等于 UserMapper 接口的全限定名
id 等于 方法名
parameterType 等于方法的参数类型
resaultType 等于方法的返回值类型
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="cn.keats.mybatisdemo.mapper.UserMapper"> <insert id="insert" parameterType="user"> insert into user values (null , #{username}, #{password}, #{birthday}) </insert> </mapper>
导入Redis启动器
<!-- redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
配置host和端口
其实从图中能够看出,springboot默认对Redis的配置就是 localhost:6379 因此若是Redis也是这个路径,能够不用自行配置