1.springboot简介
SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程
2.springboot特点
1.创建独立的 Spring 应用程序
2.嵌入的 Tomcat,无需部署 WAR 文件
3.简化 Maven 配置
4.自动配置 Spring
5.提供生产就绪型功能,如指标,健康检查和外部配置
6.开箱即用,没有代码生成,也无需 XML 配置。
3.创建一个springboot工程
3.1版本可以根据自己需要进行选择
3.2在controller层书写一个控制类进行测试
要注意创建的类要放在主类所在包或其子包下才能被扫描到
4.springboot中常用的配置文件类型
4.1properties文件类型
server.port=8888
4.2yal文件类型
server:
port: 8888
两种文件的最大的区别就是书写的格式不同,两者的文件名一致,但是后缀不同
-
配置文件间的加载优先级 properties(最高)> yml
-
不同配置文件中相同配置按照加载优先级相互覆盖,不同配置文件中不同配置全部保留
5.读取配置文件内容的方式
5.1方式一 在类上@ConfigurationProperties(prefix="")
@Data
@Component
@ConfigurationProperties(prefix = "student")
public class Student {
private String name;
private int age;
private String address;
private List<String> hobby;
private Map<String,Object> map;
}
properties配置文件
student.name=liming
student.age=22
student.address=zhumadian
student.hobby[0]=swing
student.hobby[1]=reading
student.hobby[2]=singing
student.map.a=1
student.map.b=2
student.map.c=3
controllerceng进行测试
@RestController
public class HelloController {
@Autowired
private Student student;
@GetMapping("info")
public Student info(){
return student;
}
}
5.2方式二 使用@Value读取属性 但需注意该种方式只能读取基本类型和string类型
controllerceng进行测试
@RestController
public class HelloController {
@Autowired
private Student student;
@GetMapping("info")
public Student info(){
return student;
}
@Value("${student.name}")//只能读取基本类型和string类型 加在属性上
private String name;
@GetMapping("stu")
public String stu(){
return name;
}
}
6. springboot整合数据源
6.1引入相关依赖
<!-- mysql驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
6.2配置数据源
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://localhost:3306/homework?serverTimezone=Asia/Shanghai
spring.datasource.druid.username=root
spring.datasource.druid.password=123456
spring.datasource.druid.initial-size=5
6.3测试类测试
@SpringBootTest
class Springboot1ApplicationTests {
@Autowired
private DataSource dataSource;
@Test
void contextLoads() {
System.out.println(dataSource);
}
}
7springboot整合mybatis
7.1引入相关依赖
<!-- mybatis 和 springboot整合-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
7.2properties配置文件
#配置数据源
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://localhost:3306/homework?serverTimezone=Asia/Shanghai
spring.datasource.druid.username=root
spring.datasource.druid.password=123456
spring.datasource.druid.initial-size=5
#指定映射文件路径
mybatis.mapper-locations=classpath:mapper/*.xml
#日志
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#??
7.3mapper接口
public interface StudentMapper {
void findById();
}
7.4为mapper接口生成代理实现类
@SpringBootApplication
@MapperScan(basePackages = "com.aaa.springboot1.mapper")//为mapper接口生成代理实现类
public class Springboot1Application {
public static void main(String[] args) {
SpringApplication.run(Springboot1Application.class, args);
}
}
7.4测试
@SpringBootTest
class Springboot1ApplicationTests {
@Autowired
private StudentMapper studentmapper;
@Test
void contextLoads() {
System.out.println(studentmapper.findById(2));
}
}
类测试