【黑马程序员springboot基础速成课】 https://www.bilibili.com/video/BV1Hz4y127Q3/?share_source=copy_web&vd_source=ae81a614b54897d869c3489065b78660
概述
SpringBooot入门
POM文件内容(添加依赖):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.5.RELEASE</version>
</parent>
<groupId>com.itheima</groupId>
<artifactId>heima-springboot</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
创建启动类:
package com.itheima;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//spring boot工程 都有一个启动引导类,这是工程的入口类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
创建处理器:
package com.itheima.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("hello")
public String hello(){
return "hello Spring Boot!";
}
}
测试:浏览器打开http://localhost:8080/hello
Java代码方式配置
可以使用@value获取配置文件配置项并结合@bean注册组件到spring
需求:使用Java代码配置数据库连接池,并可以在处理器中注入并使用
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/springboot_test
jdbc.username=root
jdbc.password=root
配置类:
package com.itheima.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
@Configuration
@PropertySource("classpath:jdbc.properties")
public class JdbcConfig {
@Value("${jdbc.url}")
String url;
@Value("${jdbc.driverClassName}")
String driverClassName;
@Value("${jdbc.username}")
String username;
@Value("${jdbc.password}")
String password;
@Bean
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(url);
dataSource.setDriverClassName(driverClassName);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
注入数据源:
package com.itheima.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.sql.DataSource;
@RestController
public class HelloController {
@Autowired
private DataSource dataSource;
@GetMapping("hello")
public String hello(){
System.out.println("DataSource="+dataSource);
return "hello Spring Boot!";
}
}
SpringBoot属性注入方式
package com.itheima.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
@Configuration
//@PropertySource("classpath:application.properties")
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfig {
//
// @Value("${jdbc.url}")
// String url;
// @Value("${jdbc.driverClassName}")
// String driverClassName;
// @Value("${jdbc.username}")
// String username;
// @Value("${jdbc.password}")
// String password;
//
// @Bean
// public DataSource dataSource(){
// DruidDataSource dataSource = new DruidDataSource();
// dataSource.setUrl(url);
// dataSource.setDriverClassName(driverClassName);
// dataSource.setUsername(username);
// dataSource.setPassword(password);
// return dataSource;
// }
@Bean
public DataSource dataSource(JdbcProperties jdbcProperties){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(jdbcProperties.getUrl());
dataSource.setDriverClassName(jdbcProperties.getDriverClassName());
dataSource.setUsername(jdbcProperties.getUsername());
dataSource.setPassword(jdbcProperties.getPassword());
return dataSource;
}
}
更优雅的注入
package com.itheima.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
@Configuration
//@PropertySource("classpath:application.properties")
//@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfig {
//
// @Value("${jdbc.url}")
// String url;
// @Value("${jdbc.driverClassName}")
// String driverClassName;
// @Value("${jdbc.username}")
// String username;
// @Value("${jdbc.password}")
// String password;
//
// @Bean
// public DataSource dataSource(){
// DruidDataSource dataSource = new DruidDataSource();
// dataSource.setUrl(url);
// dataSource.setDriverClassName(driverClassName);
// dataSource.setUsername(username);
// dataSource.setPassword(password);
// return dataSource;
// }
// @Bean
// public DataSource dataSource(JdbcProperties jdbcProperties){
// DruidDataSource dataSource = new DruidDataSource();
// dataSource.setUrl(jdbcProperties.getUrl());
// dataSource.setDriverClassName(jdbcProperties.getDriverClassName());
// dataSource.setUsername(jdbcProperties.getUsername());
// dataSource.setPassword(jdbcProperties.getPassword());
// return dataSource;
// }
@Bean
@ConfigurationProperties(prefix = "jdbc")
public DataSource dataSource(){
return new DruidDataSource();
}
}
多个yml文件配置
自动配置原理
lombok应用
IntelliJ IDEA 2020.1.4 版本lombok插件安装问题_idea2020安装lombok插件-CSDN博客
https://www.cnblogs.com/holten/p/5729226.html