SpringBoot学习-Day1
学习目标
- 学会SpringBoot
- 对应项目SpringBoot_Demo中的
day1
学习笔记
1 SpringBoot概述
1.1 概念
- SpringBoot提供了一种快速使用Spring的方式,基于约定优于配置的思想,可以让开发人员不必在配置与逻辑业务之间进行思维的切换,全身心的投入到逻辑业务的代码编写中,从而大大提高了开发的效率,一定程度上缩短了项目周期
1.2 Spring缺点
- 配置繁琐
- 依赖繁琐
1.3 SpringBoot功能
- 自动配置:SpringBoot的自动配置是一个运行时的过程,考虑了众多因素,才决定Spring配置应该用哪个,不该用哪个,该过程是SpringBoot自动完成的
- 起步依赖:起步依赖本质上是一个Maven项目对象模型(POM),定义了对其他库的传递依赖,这些东西加在一起即支持某项功能。简单的说,起步依赖就是将具备某种功能的坐标打包到一起,并提供一些默认的功能
- 辅助功能:提供了一些些大型项目中常见的非功能性特性,如嵌入式服务器、安全、指标、健康检测、外部配置等
2 SpringBoot快速入门
<!-- SpringBoot工程需要继承的父工程-->
<parent><artifactId>spring-boot-starter-parent</artifactId><groupId>org.springframework.boot</groupId><version>2.5.3</version>
</parent>
<!-- web开发的起步依赖-->
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>
pom.xml
@RestController
public class HelloController {@RequestMapping("/hello")public String hello(){return "Hello SpringBoot";}
}
HelloController.java
/*** 引导类.SpringBoot项目的入口*/
@SpringBootApplication
public class HelloApplication {public static void main(String[] args) {SpringApplication.run(HelloApplication.class,args);}
}
HelloApplication.java
- 注意:
SpringBoot在创建项目时,使用jar打包方式
SpringBoot的引导类,是项目入口,运行main方法就可以启动项目
使用SpringBoot和Spring构建的项目,业务代码编写方式完全一样
3 快速构建SpringBoot工程
- 新建Spring Initializr模块
- 使用Web工程模板
4 SpringBoot起步依赖原理分析
4.1 spring-boot-starter-parent和4.2 spring-boot-starter-web
- spring-boot-starter-parent中定义了各种技术的版本信息,组合了一套最优搭配的技术版本
- 在各种starter中,定义了完成该功能需要的坐标合集,其中大部分版本信息来源于父工程
- 自己的工程继承parent,引入starter后,通过依赖传递,就可以简单方便获得需要的jar包,而且不会存在版本冲突等问题
5 SpringBoot配置
5.1 配置文件分类
- SpringBoot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置,则可以使用applicaiton.properties或application.yml进行配置
- 如果配置文件在同一级目录下,优先级最高的是properties,第二高的是yml,最低的是yaml
5.2 读取配置文件内容
5.2.1 @Value
@Value("${name}")
private String name;@Value("${person.name}")
private String name2;@Value("${person.age}")
private String age;@Value("${address[0]}")
private String address1;@Value("${msg1}")
private String msg1;@Value("${msg2}")
private String msg2;
5.2.2 Environment
@Autowired
private Environment env;System.out.println(env.getProperty("person.name"));
System.out.println(env.getProperty("address[0]"));
5.2.3 @ConfigurationProperties
@Component
@ConfigurationProperties(prefix = "person")
public class Person {private String name;private int age;@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +'}';}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
Person.java
@Autowired
private Person person;System.out.println(person);
5.3 profile
5.3.1 作用
- 进行动态配置切换
5.3.2 profile配置方式
- 多profile配置方式
spring.profiles.active = dev #dev对应其他配置文件的后缀,此处就是application-dev.properties
application.properties
server.port=8081
application-dev.properties
- yaml多文档方式
---
server:port: 8081
spring:profiles: dev---
server:port: 8082
spring:profiles: test---
server:port: 8083
spring:profiles: pro---spring:profiles:active: pro
5.3.3 profile激活方式
- 配置文件
- 虚拟机参数
- 命令行参数
5.4 项目内部配置加载顺序
- SpringBoot程序启动时,会从以下位置加载配置文件(优先级由高到低):
当前项目下的/config目录下
当前项目的根目录
classpath的/config目录
classpath的根目录
5.4 项目外部配置加载顺序
- 优先加载外部属性,具体顺序查看官网
5.5 SpringBoot整合其它框架
5.5.1 SpringBoot整合Junit
@SpringBootApplication
public class Day1SpringBootTestApplication {public static void main(String[] args) {SpringApplication.run(Day1SpringBootTestApplication.class, args);}}
Day1SpringBootTestApplication.java
@Service
public class UserService {public void add(){System.out.println("add.");}
}
UserService.java
/*** UserService测试类*/
@SpringBootTest(classes = Day1SpringBootTestApplication.class)
public class UserServiceTest {@Autowiredprivate UserService userService;@Testpublic void testAdd(){userService.add();}
}
UserServiceTest.java
- 注意,新版本的SpringBoot整合Junit已经不需要@RunWith
5.5.2 SpringBoot整合Redis
- 创建新模块选择redis
- 对应pom中的依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
@SpringBootTest
class Day1SpringBootRedisApplicationTests {@Autowiredprivate RedisTemplate redisTemplate;@Testpublic void testSet() {//存入数据redisTemplate.boundValueOps("name").set("zs");}@Testpublic void testGet(){//拿数据Object name = redisTemplate.boundValueOps("name").get();System.out.println(name);}
}
Day1SpringBootRedisApplicationTests.java
- 注意:需要打开本地的redis服务器
- 可以在配置文件中配置redis
5.5.3 SpringBoot整合MyBatis
- 创建新模块选择MyBatis和MySQL
- 对应pom中的依赖
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.0</version>
</dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope>
</dependency>
5.5.3.1 注解方式
# datasource
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql:///testusername: rootpassword: root
application.yml
@Mapper
public interface UserMapper {@Select("select * from user")public List<User> findAll();}
UserMapper.java
@SpringBootTest(classes = Day1SpringBootMybatisApplication.class)
class Day1SpringBootMybatisApplicationTests {@Autowiredprivate UserMapper userMapper;@Testvoid testFindAll() {List<User> userList = userMapper.findAll();System.out.println(userList);}}
Day1SpringBootMybatisApplicationTests.java
- 注意:新版本的SpringBoot对数据库进行配置中的driver-class-name由com.mysql.jdbc.Driver改为com.mysql.cj.jdbc.Driver
5.5.3.1 配置文件方式
# datasource
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql:///testusername: rootpassword: root
# mybatis
mybatis:mapper-locations: classpath:mapper/*Mapper.xml #mapper的映射文件路径type-aliases-package: com.example.day1_springboot_mybatis.domain#config-location: 指定mybatis的核心配置文件
application.yml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.day1_springboot_mybatis.mapper.UserXmlMapper"><select id="findAll" resultType="user">select * from user</select>
</mapper>
UserMapper.xml
@Mapper
public interface UserXmlMapper {public List<User> findAll();
}
UserXmlMapper.java
@SpringBootTest(classes = Day1SpringBootMybatisApplication.class)
class Day1SpringBootMybatisApplicationTests {@Autowiredprivate UserXmlMapper userXmlMapper;@Testvoid testFindAll2() {List<User> userList = userXmlMapper.findAll();System.out.println(userList);}}
Day1SpringBootMybatisApplicationTests.java
SpringBoot学习-Day1相关推荐
- springboot学习笔记:12.解决springboot打成可执行jar在linux上启动慢的问题
springboot学习笔记:12.解决springboot打成可执行jar在linux上启动慢的问题 参考文章: (1)springboot学习笔记:12.解决springboot打成可执行jar在 ...
- SpringBoot学习笔记(3):静态资源处理
SpringBoot学习笔记(3):静态资源处理 在web开发中,静态资源的访问是必不可少的,如:Html.图片.js.css 等资源的访问. Spring Boot 对静态资源访问提供了很好的支持, ...
- 目录:SpringBoot学习目录
SpringBoot配套源码地址:gitee.com/hengboy/spr- SpringCloud配套源码地址:gitee.com/hengboy/spr- SpringBoot相关系列文章请访问 ...
- springboot学习笔记(五)
一丶注值方式 1.在application.properties文件中注值 首先我们将application.yml中的学生名字和年龄给注释掉,来验证在applic.properties的注值方式. ...
- SpringBoot学习笔记(4)----SpringBoot中freemarker、thymeleaf的使用
1. freemarker引擎的使用 如果你使用的是idea或者eclipse中安装了sts插件,那么在新建项目时就可以直接指定试图模板 如图: 勾选freeMarker,此时springboot项目 ...
- SpringBoot学习笔记(16):单元测试
SpringBoot学习笔记(16):单元测试 单元测试 单元测试(英语:Unit Testing)又称为模块测试,是针对程序模块(软件设计的最小单位)来进行正确性检验的测试工作.程序单元是应用的最小 ...
- SpringBoot学习笔记(9)----SpringBoot中使用关系型数据库以及事务处理
在实际的运用开发中,跟数据库之间的交互是必不可少的,SpringBoot也提供了两种跟数据库交互的方式. 1. 使用JdbcTemplate 在SpringBoot中提供了JdbcTemplate模板 ...
- SpringBoot学习笔记(16)----SpringBoot整合Swagger2
Swagger 是一个规范和完整的框架,用于生成,描述,调用和可视化RESTful风格的web服务 http://swagger.io Springfox的前身是swagger-springmvc,是 ...
- SpringBoot学习笔记(8):事物处理
SpringBoot学习笔记(8):事物处理 快速入门 在传统的JDBC事务代码开发过程中,业务代码只有一部分,大部分都是与JDBC有关的功能代码,比如数据库的获取与关闭以及事务的提交与回滚.大量的t ...
最新文章
- [Cocoa]XCode的一些调试技巧
- golang channel 管道
- hdu 5100 n*n棋盘放k*1长方条最多覆盖面积
- 基于嵌入式linux和s32410平台的视频采集
- linux查询内核参数命令,Linux内核启动参数详解
- flask web开发是前端还是后端_Flask Web开发实战:入门、进阶与原理解析 PDF 全格式版...
- VMware vSAN的相关告警处理 2021-01-04
- mysql5.7版本的新特性_MySQL 5.7版本新特性(一)
- java 变量 类之间调用_java内部类与其他类变量之间的调用方式
- 弹窗插件zDialog使用教程
- 在线qq客服的html代码生成器,js生成qq客服在线代码
- 回测系统backtrader(1)参考模板
- 今日头条,即将崛起为中国互联网第三极
- 干货 | 6大商品数据分析模型分享!
- http://zjj2816.cnblogs.com/?Contact=1
- cadence 17.2 入门学习3 allegro
- 《机器学习实战》支持向量机(手稿+代码)
- (Nginx出现403 forbidden)nginx权限问题failed(13:Permission denied)
- 按键消抖及原理(硬件和软件方法详解)
- 如何做好网站的日常优化推广