SpringBoot随笔

412 阅读1分钟

yml数据格式

image.png

获取yml中的数据

三种方法
通过@Value注解获取(适用于属性比较少的时候)image.png
使用Environment对象,通过@Autowired注入(注意导入的包,应该导入springframework)image.png image.png
@ConfigurationPropertiesimage.png

profile

在 Spring Boot 中,Profile 是通过配置文件来实现的。在不同的环境下,可以加载不同的配置文件,从而实现不同的配置逻辑。

  • 多profile文件方式 image.png

  • yml多文档方式
    image.png

打包jar包并运行:
image.png

java jar ... image.png

遇到的Bug: 运行jar包时找不到主清单属性,需要把spring-boot-maven-plugin里的skip改为false
image.png

使用profile
image.png

配置文件加载优先级

image.png

SpringBoot整合JUnit

@Service注解的作用:@Service注解有什么用?@Service注解的用法?_service注解的作用-CSDN博客 image.png

SpringBoot整合Redis

依赖选择NoSQL里第一个
image.png
test
image.png

SpringBoot整合Mybatis

  • 依赖: image.png

  • 连接数据库 image.png

  • 构造函数User(getter,setter,toString) image.png

  • UserMapper使用注解编写sql语句 image.png

  • 最后进行测试(记得导入对应的构造函数) image.png