1、spring-boot-starter-web 作用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency></pre>
在使用 IDEA 每次自動(dòng)創(chuàng)建項(xiàng)目的時(shí)候,都會(huì)自動(dòng)加上以上依賴!但是你有沒(méi)有仔細(xì)想過(guò),為什么?
今天小司機(jī)帶你開(kāi)車,一探究竟。兄弟們,坐穩(wěn)了,小心翻車??!
1.1、加或者不加 spring-boot-starter-web 有沒(méi)有區(qū)別
答案是當(dāng)然有了! SpringBoot 沒(méi)有出現(xiàn)的時(shí)候,做過(guò) web 的朋友都知道,搭建項(xiàng)目時(shí)候需要各種 xml 的配置,進(jìn)行依賴關(guān)系的導(dǎo)入,還需要配置 web.xml 進(jìn)行請(qǐng)求的攔截,在外部配置一個(gè) tomcat 容器,每次啟動(dòng)項(xiàng)目的時(shí)候,使用
tomcat:run,啟動(dòng)完成后,才能訪問(wèn)項(xiàng)目的資源和本地測(cè)試等。 但是當(dāng) SpringBoot 出現(xiàn)的時(shí)候,這一切都大大簡(jiǎn)化了。因?yàn)?Spring Boot 支持容器的自動(dòng)配置,它默認(rèn)是Tomcat容器(如下圖紅色所示),開(kāi)發(fā)者可以可以進(jìn)行修改。如果沒(méi)有添加這個(gè) web 依賴,在啟動(dòng) SpringBoot 項(xiàng)目的時(shí)候,不會(huì)報(bào)錯(cuò),但是啟動(dòng)不了項(xiàng)目!
我們可以這么理解,每次項(xiàng)目的啟動(dòng)都必須在容器中執(zhí)行,所以
spring-boot-starter-web這個(gè)依賴給我們默認(rèn)已經(jīng)配置了容器(tomcat),我們不需要在外部配置 其他容器了。如果沒(méi)有添加這個(gè)依賴,就會(huì)導(dǎo)致項(xiàng)目無(wú)法找到依賴的容器,無(wú)法找到容器,當(dāng)然就不會(huì)啟動(dòng)了。

1.2、 如何排除spring-boot-starter-web 依賴中的 Tomcat ,添加其他容器
排除依賴容器 tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
?
加入Jetty容器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency></pre>
2、com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver 的區(qū)別
com.mysql.jdbc.Driver 是 mysql-connector-java 5 中的。
com.mysql.cj.jdbc.Driver 是 mysql-connector-java 6 中的。
2.1、 JDBC 連接 Mysql5 需用 com.mysql.jdbc.Driver
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=root
2.2、JDBC連接Mysql6需用com.mysql.cj.jdbc.Driver,同時(shí)需要指定時(shí)區(qū)serverTimezone
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=root
2.3、設(shè)定時(shí)區(qū)時(shí),serverTimezone=UTC比中國(guó)時(shí)間早8個(gè)小時(shí),若在中國(guó),可設(shè)置serverTimezone=Shanghai或者serverTimezone=Hongkong
driverClassName=com.mysql.cj.jdbc.Driver
username=root
password=root
2.4、總結(jié): 如果mysql-connector-java用的 6.0 以上的 ,如下:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency></pre>
但是 driver 用的還是 com.mysql.jdbc.Driver 就會(huì)報(bào)錯(cuò),此時(shí)需要把 com.mysql.jdbc.Driver 改為com.mysql.cj.jdbc.Driver。
即可運(yùn)行成功?。?!