网站建设的主要流程/今天最新新闻
目录
1 YAML配置文件
1.1 书写规则
1.2 代码示例
1.3 用yaml进行复杂数据绑定
2 整合日志
2.1 日志配置
3 整合web
3.1 默认配置
3.2 web应用开发方式
3.2.1 全自动
3.2.2 全手动
3.2.3 手自一体(推荐)
4 整合mybatis
4.1 导包
4.2 application.yaml
4.3 dao接口+mapper.xml
4.4 dao接口包扫描
1 YAML配置文件
SpringBoot采用集中化配置管理,即将所有配置都写在application.properties中,但当配置增多后,会出现"难阅读,层级结构模糊"的问题
于是出现了application.yaml或.yml配置文件,用于替代application.properties配置文件
1.1 书写规则
(1) 区分大小写
(2) K和V之间用 : +空格分隔
(3) 缩进表示层级关系,同一层级有相同的缩进
(4) #表示注释
1.2 代码示例
server:port: 8083 #自定义 端口servlet:context-path: /sb #项目地址spring:web:resources:static-locations: classpath:/a/,classpath:/b/ #废掉4个"静态访问路径",自定义a,b文件夹为新的"静态访问路径"mvc:static-path-pattern: /html/** #访问"静态访问路径"中的静态文件,需要加html前缀(index.html不用加前缀)servlet:multipart:max-file-size: 20MB #(文件上传)最大文件大小max-request-size: 30MB #请求大小限制profiles:active: dev,testdatasource: #mysql数据源driver-class-name: com.mysql.cj.jdbc.Drivertype: com.zaxxer.hikari.HikariDataSource #SpringBoot(默认)数据源,如果使用其他数据源需(dbcp2等)要额外导包url: jdbc:mysql://localhost:3306/sunnerusername: rootpassword: root--- #用三条短横线可以将下面的配置全部缩进
mybatis:mapper-locations: classpath:com/sunner/dao/*Dao.xml #映射配置文件type-aliases-package: xyz.aboluo.pojo #mybatis(包)起别名
1.3 用yaml进行复杂数据绑定
@Component
@ConfigurationProperties(prefix = "person")
public class Person {private String name;private Integer age;private Date birthday;private Boolean manFlag;private Child child; //嵌套对象private List<Dog> dogs; //嵌套数组private Map<String,Cat> cats; //嵌套map
}person:name: 王老五age: 35birthday: 1996/05/02 12:05:06 #(默认)只能用这种格式manFlag: true #对于驼峰命名的属性也可以小写用-分隔 例如man-flagchild:name: 王老六age: 8dogs:- name: 旺财age: 1like: 骨头- name: 小黑age: 3like: 鸡肉cats:cat1:name: 喵桑age: 5like: 猫粮cat2: { name: 咪咪,age: 4,like: 鱼罐头 }
2 整合日志
日志门面(接口)采用slf4j,日志实现采用Logback
这些已经被SpringBoot默认整合了,无需我们再做额外的操作
2.1 日志配置
可以在application.yaml中配置(不推荐)
或者使用logback的配置文件logback.xml(推荐),具体可以看这篇文章
3 整合web
3.1 默认配置
(1) 默认静态资源处理:静态资源放在static文件夹(或resources、public文件夹)下可以被直接访问
4个静态资源访问路径:classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
(2) 支持放在4个静态访问路径中的index.html当做首页展示
(3) 自动注册了Converter(类型转换器),Formatter(格式化)组件,例如在yaml配置文件中可以帮我们将字符串转换为Integer、对象、数组等,也可以将日期字符串(默认只能是yyyy/MM/dd ss:mm:ss:SSS的格式)格式化成Date对象
(4) 支持HttpMessageConverters,用于将返回的对象转为json,即对@ResponseBody注解的支持
(5) 自动使用ConfigurableBuildingInitializer,实现数据校验、类型转换、数据绑定功能,即对@RequestParameter、@RequestBody等注解的支持
3.2 web应用开发方式
3.2.1 全自动
完全按照SpringBoot的默认配置
3.2.2 全手动
在Spring配置类(@Configuration或@SpringBootConfiguration)上添加@EnableWebMvc,禁止所有默认配置
3.2.3 手自一体(推荐)
修改配置可以用yaml配置文件的方式
也可以用Spring配置类的方式: 用Spring配置类实现WebMvcConfigurer,但千万不要标@EnableWebMvc,重写需要修改配置的方法
@SpringBootConfiguration
@EnableConfigurationProperties({Dog.class, Cat.class})
public class SpringConfig implements WebMvcConfigurer {/*** 配置静态资源的方法** @param registry*/@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {// 自定义配置registry.addResourceHandler("/html/**")//所有以html开头的请求都会去"静态访问路径"a和b下面匹配.addResourceLocations("classpath:/a/", "classpath:/b/")//设置静态资源缓存规则(超过1000秒重新获取静态资源).setCacheControl(CacheControl.maxAge(1000, TimeUnit.SECONDS));}
}
还可以用将WebMvcConfigurer注册进SpringIOC容器的方式
@SpringBootConfiguration
@EnableConfigurationProperties({Dog.class, Cat.class})
public class SpringConfig {@Beanpublic WebMvcConfigurer getWebMvcConfigurer() {return new WebMvcConfigurer() {@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {// 自定义配置registry.addResourceHandler("/html/**")//所有以html开头的请求都会去"静态访问路径"a和b下面匹配.addResourceLocations("classpath:/a/", "classpath:/b/")//设置静态资源缓存规则(超过1000秒重新获取静态资源).setCacheControl(CacheControl.maxAge(1000, TimeUnit.SECONDS));}};}
}
4 整合mybatis
4.1 导包
<dependencies>><!--mybatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.2</version></dependency><!--mysql驱动--><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><version>8.0.33</version></dependency>
</dependencies>
mybatis-spring-boot-starter依赖了spring-boot-starter-jdbc(我们知道jdbc是用来操作数据库的),其中为我们自动配置好的:
(1) org.springframework.boot.autoconfigure.jdbc.jdbcTemplateAutoConfiguration是对数据源的自动配置(默认使用HikariDataSource)
(2) org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration是用来操作数据库的,但也可以选择整合mybatis
(3) org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration用于支持事务(支持@Transactional)
4.2 application.yaml
配置mysql数据源
配置mybatis映射配置文件,类(包)别名
4.3 dao接口+mapper.xml
4.4 dao接口包扫描
在Spring配置类用@MapperScan扫描到dao接口