你們好,我是小菜,一個渴望在互聯網行業作到蔡不菜的小菜。可柔可剛,點贊則柔,白嫖則剛!
死鬼~看完記得給我來個三連哦!java
「本文主要介紹
SprinBoot
若有須要,能夠參考
若有幫助,不忘 點贊 ❥mysql
法1:修改和server有關的配置web
properties server.tomcat.uri-encoding=UTF-8 //通用的Servlet容器設置 server.xxx //Tomcat的設置 server.tomcat.xxx
spring
法2:編寫一個EmbeddedServletContainerCustomizer:嵌入式的Servlet容器的定製器;來修改Servlet容器的配置
sql
Servlet
Filter
Listener
因爲 SpringBoot 默認是以jar包的方式啓動嵌入式的Servlet容器來啓動SpringBoot的web應用,因此沒有web.xml
文件數據庫
註冊三大組件用如下方式:apache
ServletRegistrationBean
:
tomcat
FilterRegistrationBean
:
ServletListenerRegistrationBean
:
默認支持如下容器springboot
Tomcat
<!-- 引入web模塊默認就是使用嵌入式的Tomcat做爲Servlet容器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
複製代碼
Jetty
<!-- 先排除內置默認容器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!--引入其餘的Servlet容器-->
<dependency>
<artifactId>spring-boot-starter-jetty</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
複製代碼
Undertow
<!-- 先排除內置默認容器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!--引入其餘的Servlet容器-->
<dependency>
<artifactId>spring-boot-starter-undertow</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
複製代碼
EmbeddedServletContainerAutoConfiguration
:
服務器
EmbeddedServletContainerFactory
(嵌入式Servlet容器工廠)
public interface EmbeddedServletContainerFactory {
//獲取嵌入式的Servlet容器
EmbeddedServletContainer getEmbeddedServletContainer(
ServletContextInitializer... initializers);
}
}
複製代碼
EmbeddedServletContainer
:(嵌入式的Servlet容器)
以TomcatEmbeddedServletContainerFactory
爲例
嵌入式容器的配置修改怎麼生效
方法1:ServerProperties
方法2: EmbeddedServletContainerCustomizer
(定製器幫咱們修改了Servlet容器的配置)
修改原理:
容器中導入了EmbeddedServletContainerCustomizerBeanPostProcessor
ServerProperties
:也是定製器
EmbeddedServletContainerFactory【TomcatEmbeddedServletContainerFactory】
EmbeddedServletContainerCustomizerBeanPostProcessor
(只要是嵌入式的Servlet容器工廠,後置處理器就工做)EmbeddedServletContainerCustomizer
,調用定製器的定製方法嵌入式Servlet容器:應用打成可執行的jar
優勢: 簡單、便攜
缺點:默認不支持JSP、優化定製比較複雜
步驟:
建立一個war項目
將嵌入式的Tomcat指定爲provided
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
複製代碼
編寫一個SpringBootServletInitializer
的子類,並調用configure()
方法
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
//傳入SpringBoot應用的主程序
return application.sources(SpringBoot04WebJspApplication.class);
}
}
複製代碼
啓動服務器就可使用
原理:
jar包
:執行SpringBoot主類的main方法,啓動 Ioc
容器,建立嵌入式的Servlet容器
war包
:啓動服務器,服務器啓動SpringBoot應用SpringBootServletInitializer
,啓動 Ioc
容器
對於數據訪問層,不管是SQL
仍是NOSQL
,Spring Boot
默認採用整合Spring Data
的方式進行統一處理,添加大量自動配置,屏蔽了不少設置。引入各類xxxTemplate
,xxxRepository
來簡化咱們對數據訪問層的操做。對咱們來講只須要進行簡單的設置便可。
spring-boot-starter-jdbc
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
複製代碼
application.yml
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://192.168.15.22:3306/test
driver-class-name: com.mysql.jdbc.Driver
複製代碼
結論:
org.apache.tomcat.jdbc.pool.DataSource
做爲數據源DataSourceProperties
裏面自動配置原理:
參考DataSourceConfiguration
,根據配置建立數據源,默認使用Tomcat鏈接池。可使用spring.datasource.type
指定自定義的數據源類型
SpringBoot默承認以支持:
org.apache.tomcat.jdbc.pool.DataSource
HikariDataSource
BasicDataSource
自定義數據源類型
@ConditionalOnMissingBean(DataSource.class)
@ConditionalOnProperty(name = "spring.datasource.type")
static class Generic {
@Bean
public DataSource dataSource(DataSourceProperties properties) {
//使用DataSourceBuilder建立數據源,利用反射建立響應type的數據源,而且綁定相關屬性
return properties.initializeDataSourceBuilder().build();
}
}
複製代碼
DataSourceInitializer
:ApplicationListener
做用:
runSchemaScripts()
:運行建表語句
runDataScripts()
:運行插入數據的sql語句
默認只須要將文件命名爲:
schema-*.sql
、data-*.sql
操做數據庫:自動配置了JdbcTemplate操做數據庫
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.8</version>
</dependency>
複製代碼
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
複製代碼
步驟:
1)配置數據源相關屬性
2)給數據庫建表
3)建立JavaBean
4)註解使用
在容器中添加一個ConfigurationCustomizer
在啓動類中添加MapperScan註解批量掃描全部的Mapper接口
5)配置文件使用
mybatis:
#指定全局配置文件的位置
config-location: classpath:mybatis/mybatis-config.xml
#指定sql映射文件的位置
mapper-locations: classpath:mybatis/mapper/*.xml
複製代碼
SpringData簡介
「Spring Data是一個用於簡化數據庫訪問,並支持雲服務的開源框架。其主要目標是使得對數據的訪問變得方便快捷。
它能夠極大的簡化JPA的寫法,能夠在幾乎不用寫實現的狀況下,實現對數據的訪問和操做。除了CRUD外,還包括如分頁、排序等一些經常使用的功能。
SpringData整合
spring:
jpa:
hibernate:
#更新或者建立數據表結構
ddl-auto: update
#控制檯顯示SQL
show-sql: true
複製代碼
如下文件是配置在META-INF/spring.factories
ApplicationContextInitializer
SpringApplicationRunListener
以上兩個須要配置在(META-INF/spring.factories
)
org.springframework.context.ApplicationContextInitializer=\
com.atguigu.springboot.listener.HelloApplicationContextInitializer
org.springframework.boot.SpringApplicationRunListener=\
com.atguigu.springboot.listener.HelloSpringApplicationRunListener
複製代碼
如下兩個只須要放在ioc容器中
ApplicationRunner
CommandLineRunner
「今天的你多努力一點,明天的你就能少說一句求人的話!
我是小菜,一個和你一塊兒學習的男人。
💋