SpringBoot
特性
- 起步依赖
- 自动配置
- 其他特性
- 内置 Tomcat,无需打包成 war
- 外部化配置
- 无需 xml 配置
yml 配置文件
@Value @ConfigurationProperties(prefix="email")
SpringBoot 整合 Mybatis
- 引入依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
- 配置文件
spring:
datasource:
url: jdbc:mysql://localhost:33066/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
mybatis:
type-aliases-package: com.example.demo.entity
mapper-locations: classpath:mapper/*.xml
注入第三方 jar 包里的对象
- @Configuration 注解的类下
- @Bean 如果需要用到 SpringIOC 管理的 Bean 直接给方法添加参数,Spring 会自动注入
- @Import 注解引入其他配置类
在线初始化项目
注意事项
- controller 必须写在与 XXXApplication.java 同级文件夹的里面
- @RestController 等同于 @Controller + @ResponseBody 组合使用
- 在 Spring Boot 中多环境配置文件名需要满足 application-{profile}.properties 的格式,其中{profile}对应你的环境标识,比如:
- application-dev.properties:开发环境
- application-test.properties:测试环境
- application-prod.properties:生产环境
- 在 application.properties 文件中通过 spring.profiles.active 属性来设置,其值对应配置文件中的{profile}值。如:spring.profiles.active=test 就会加载 application-test.properties 配置文件内容。
- java -jar xxx.jar --server.port=8888 --spring.profiles.active=test
- 命令里的--配置会覆盖 application.properties 里的配置
- Spring Boot 为了能够更合理的重写各属性的值,使用了下面这种较为特别的属性加载顺序(优先级从高到低):
- 命令行中传入的参数。
- SPRING_APPLICATION_JSON 中的属性。SPRING_APPLICATION_JSON 是以 JSON 格式配置在系统环境变量中的内容。
- java:comp/env 中的 JNDI 属性。
- Java 的系统属性,可以通过 System.getProperties()获得的内容。
- 操作系统的环境变量
- 通过 random.*配置的随机属性
- 位于当前应用 jar 包之外,针对不同{profile}环境的配置文件内容,例如:application-{profile}.properties 或是 YAML 定义的配置文件
- 位于当前应用 jar 包之内,针对不同{profile}环境的配置文件内容,例如:application-{profile}.properties 或是 YAML 定义的配置文件
- 位于当前应用 jar 包之外的 application.properties 和 YAML 配置内容
- 位于当前应用 jar 包之内的 application.properties 和 YAML 配置内容
- 在@Configuration 注解修改的类中,通过@PropertySource 注解定义的属性
- 应用默认属性,使用 SpringApplication.setDefaultProperties 定义的内容
pom 里有的依赖为什么可以不写版本号?
springboot 如果不写版本号默认是 2.2.4;如果依赖了 spring-boot-starter-parent,里面会定义各种常见包的版本号。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
React + RestFul 应用向导学习总结
- 教程网址
- spring-boot-starter-data-rest , 提供 rest 风格操作存储数据,get-获取 post-创建 put-修改 delete-删除,总得来说是根据 http 动作而不是接口名称来区分不同操作。
- spring-boot-starter-data-jpa(java persistence api) , 实现了数据增删改查的标准接口(不需要自己写 sql),底层 ORM(Object Relation Mapping) 框架是 Hibernate,定义实体类会自动创建数据库表,(不像 MyBatis 自己写 sql),底层支持各种数据库。
- com.h2database.h2 纯 java 写的开源的支持 sql 查询的运行时的嵌入式内存数据库,非常适合开发时使用,不用改代码就能无缝切换成其他数据库。
- thymeleaf 模板引擎 , jsp 和 freemarker 的替代品,有点像写 Vue,语法不一样,嵌入的模板代码逻辑就算没被引擎处理也不影响 html 标签本身的渲染。
- spring-boot-starter-jdbc java 连接数据库的接口标准,mysql-connector-java 实现了接口
- spring-boot-starter-websocket 数据发生变化了可以通过 websocket 通知浏览器端
- spring-boot-starter-security 安全框架,提供认证和授权,保护 web 应用的数据
- thymeleaf-extras-springsecurity6 thymeleaf 模板语法拓展,方便 security 控制页面
- spring-boot-devtools 自动热部署工具
属性配置方式(优先 级从低到高)
- 项目 resources 下的 application.yml 文件
- jar 包下的 application.yml 文件
- 系统环境变量 server.port
- 命令行参数 --server.port=8989
待实践知识点
- 数据库连接池
- Auth2.0 认证
- JWT 认证
- jpa 复杂查询
- typescript 项目
- 用户注册(短信验证、邮箱验证)
- 单元测试
部分注解解析
- @CrossOrigin 加在 controller 上可支持跨域