安装好了以后重启
pom.xml添加依赖
org.projectlombok
lombok
compile
还是上一节的Cat类,去掉所有的setter和getter方法,还有toString方法。改成这样
@Component
@ConfigurationProperties(prefix = “cat” )
@Data
public class Cat {
private String name;
private int sex;
private List hobbies;
private String nameAndSex;
}
多环境切换
=====
实际开发项目的时候,分为多套环境,比如开发环境,测试环境,生产环境。不同环境的配置是不一样的。
spring:
profiles:
active: dev
cat:
name: 机器猫${random.int[1,100]}
#sex:
hobbies:
-
吃铜锣烧
-
和小咪聊天
-
用竹蜻蜓飞天
nameAndSex: c a t . n a m e , 性别是 {cat.name},性别是 cat.name,性别是{cat.sex:男}
spring:
profiles: dev
cat:
name: Hello Kitty
#sex:
hobbies:
- 不知道
nameAndSex: c a t . n a m e , 性别是 {cat.name},性别是 cat.name,性别是{cat.sex:女}
spring:
profiles: test
用—代表配置文件的分组,profiles的意思是当前环境的名称,在最顶上可以设置实际激活的环境名称。
使用SpringBoot进行web开发
===================
彻底搞定静态资源
先找到这个类:WebMvcAutoConfiguration,在这个jar包里面,还是自动配置
WebMvcAutoConfiguration中有一个 addResourceHandlers 方法,这是自动配置静态资源目录的。
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug(“Default resource handling disabled”);
} else {
this.addResourceHandler(registry, “/webjars/**”, “classpath:/META-INF/resources/webjars/”);
this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
registration.addResourceLocations(this.resourceProperties.getStaticLocations());
if (this.servletContext != null) {
ServletContextResource resource = new ServletContextResource(this.servletContext, “/”);
registration.addResourceLocations(new Resource[]{resource});
}
});
}
}
先看这个:
if (!this.resourceProperties.isAddMappings()) {
logger.debug(“Default resource handling disabled”);
}
如果isAddMappings为false,就打印默认资源映射路径失效了。isAddMappings方法其实就是返回一个addMappings变量(在WebProperties中)
addMappings的含义就是运行访问静态资源,如果你设置成false,就是禁用所有静态资源映射。
站长在写这篇教程的时候,用的SpringBoot版本为2.5.1,是目前最新的版本。发现和之前的版本比起来,改动还是很大的。翻源码很麻烦,我就直接告诉你结论就行了。
默认的静态资源目录是:
new String[]{“classpath:/META-INF/resources/”, “classpath:/resources/”, “classpath:/static/”, “classpath:/public/”};
这点和以前是一样的,测试一下
访问什么路径能返回public.js呢?
在WebMvcProperties中,有一个staticPathPattern属性。
默认就是/**,所以,无论你访问什么,都可以被静态资源处理器接受。
启动项目,访问http://localhost:8888/public.js
成功访问,现在我们手动添加一个静态目录。
spring:
profiles: test
resources:
static-locations: [classpath:/my/]
访问http://localhost:8888/my.txt,也成功了。
最后,把静态资源开关关掉。
spring:
profiles: test
resources:
static-locations: [classpath:/my/]
add-mappings: false
这样就访问不到任何静态资源了,不过一般来说不用关。
再谈static-locations
static-locations表示自定义静态目录,如果配置了这个属性,默认的静态资源目录就会失效!
也可以用这个配置来访问本地资源
spring:
profiles: test
resources:
static-locations: file:C:/upload/article/
add-mappings: true
file:表示访问本地文件。
比如我现在要访问1.jpg
重启项目,访问 http://localhost:8888/20210415/1.jpg ,可以成功访问。
但是,其他静态资源就访问不了了。因为我们设置了static-locations,是一个覆盖操作,默认的目录都没了。
像访问这种本地文件的情况,一般是文件上传和下载的目录,这种的都是放在本地磁盘的,不会和项目绑定。
如果原来的目录我们也想要获取,又要访问本地文件该咋办呢?这个时候,就不要去配置static-locations,我们可以额外配置一个资源映射。
注释掉,正常情况下,用默认的资源映射绰绰有余了。难道那么多文件夹还不够你放资源吗。。。
(注意:上图中的resources也要注释掉,不然会报错)
然后,新建一个配置类。
package com.java18.vipmgr.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyWebResourcesHandler implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(“/upload/**”)
.addResourceLocations(“file:C:/upload/article/”);
}
}
重启,访问http://localhost:8888/upload/20210415/1.jpg就能看到图片了,并且也不会影响原来的资源目录。这才是项目中推荐的做法。
SpringBoot整合JDBC
================
添加jdbc依赖:
org.springframework.boot
spring-boot-starter-jdbc
mysql依赖本来就有:
mysql
mysql-connector-java
runtime
修改yml配置:
spring:
profiles: test
#resources:
#static-locations: file:C:/upload/article/
#add-mappings: true
#配置数据源
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/idea?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username: root
password:
默认用的是mysql8.x的驱动包,url要注意加上时区,不然会报错的。
测试数据源是否加载成功:
@SpringBootTest
class DataTests {
@Autowired
DataSource dataSource;
@Test
void dataSource() {
System.out.println(dataSource.getClass());
}
}
打印:class com.zaxxer.hikari.HikariDataSource
这是springboot默认的数据源。
测试sql语句查询
@Test
void dataSource() {
//System.out.println(dataSource.getClass());
List<Map<String, Object>> users = jdbcTemplate.queryForList(“select * from users”);
System.out.println(users);
}
结果:[{id=1, username=zhangsan, password=123, points=1000}]
数据库:
举一反三:
1.为什么jdbcTemplate能直接用?
因为SpringBoot有自动配置,条件是:
@ConditionalOnClass({DataSource.class, JdbcTemplate.class})
@ConditionalOnSingleCandidate(DataSource.class)
DataSource已经配好了,JdbcTemplate因为引入了spring-boot-starter-jdbc,肯定也是有的。所以就直接自动配置了。
再看JdbcTemplateConfiguration: