IDEA + Maven + SpringBoot + Mybatis + Thymeleaf + Swagger實現一個web項目

前言

Project創建

創建項目,從New Project開始,圖文教程優於文案教程,所以創建過程已圖文爲主
項目創建圖文步驟一項目創建圖文步驟一:項目的創建方式
項目創建圖文步驟二
項目創建圖文步驟二項目創建圖文步驟三項目創建圖文步驟三:項目的依賴配置
項目創建圖文步驟四項目創建圖文步驟四:項目的本地配置

初始項目結構說明

初始項目結構介紹圖
到此,以上內容爲整個項目個創建過程。
接下來就是啓動項目驗證,本地的啓動方式有兩種,如下圖
項目啓動圖文

啓動方式一:項目創建時自動配置好的啓動類,也可通過配置Edit Configurations進行配置啓動類爲SpringbootApiApplication
啓動方式二:值得注意的是SpringbootApiApplication.java中有main方法,所以可直接在SpringbootApiApplication類中右鍵任何位置進行run

初始項目配置

到此,項目雖然創建起來了,但只是創建過程中添加了部分基礎組件,IDEA卻無法在項目中進行自動生成配置,比如沒有配置訪問時的項目名、沒有配置端口號、沒有配置數據庫等等,這些都是需要手動配置的,那麼以下則爲個人總結的初始項目需要配置的內容

個人習慣配置

對於SpringBoot項目的配置,有兩種文件格式的配置方式,均可使用,所以遵循個人喜好即可
1、在application.properties中進行配置(默認生成的配置文件)
2、在application.yml中進行配置(層級更簡潔,個人偏向)

Maven配置

下圖僅爲個人習慣配置,因爲我個人本地有自己的Maven庫,所以指定使用我自己的
項目個人配置內容圖文一:本地Maven倉庫的指定

數據庫連接驅動JDBC的配置

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/itsdf07_db?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password:
    driver-class-name: com.mysql.jdbc.Driver

以上爲個人平時的默認配置,useUnicode=true&characterEncoding=utf-8&useSSL=false這些爲JDBC所攜帶的配置參數,請根據需要進行修改即可
如不配置上述JDBC的驅動內容,則無法啓動項目,且出現異常信息如下

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-03-12 10:00:02.943 ERROR 7796 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

Disconnected from the target VM, address: '127.0.0.1:61130', transport: 'socket'

Process finished with exit code 1

異常原因其實很清楚,我們創建項目過程,只添加了組件,並沒有配置組件的原因。到此,項目便可正常啓動了,啓動日誌如下

2020-03-12 10:04:00.469  INFO 4196 --- [           main] com.itsdf07.SpringbootApiApplication     : Starting SpringbootApiApplication on DESKTOP-JQDVDVV with PID 4196 (D:\Workspaces\api\springboot-api\target\classes started by itsdf07 in D:\Workspaces\api\springboot-api)
2020-03-12 10:04:00.482  INFO 4196 --- [           main] com.itsdf07.SpringbootApiApplication     : No active profile set, falling back to default profiles: default
2020-03-12 10:04:01.774  WARN 4196 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.itsdf07]' package. Please check your configuration.
2020-03-12 10:04:02.318  INFO 4196 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-03-12 10:04:02.328  INFO 4196 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-03-12 10:04:02.329  INFO 4196 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-03-12 10:04:02.422  INFO 4196 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-03-12 10:04:02.422  INFO 4196 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1808 ms
2020-03-12 10:04:02.661  INFO 4196 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-12 10:04:02.801  WARN 4196 --- [           main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
2020-03-12 10:04:03.088  INFO 4196 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-03-12 10:04:03.093  INFO 4196 --- [           main] com.itsdf07.SpringbootApiApplication     : Started SpringbootApiApplication in 3.603 seconds (JVM running for 5.773)

經常帶着疑問以及有看log習慣的人總能發現一寫細節問題,如在啓動過程的日誌中,出現了JDBC的驅動警告提示Loading classcom.mysql.jdbc.Driver’. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary
其實這個因爲是JDBC連接Mysql不同版本時使用的驅動不匹配
com.mysql.jdbc.Driver 是 mysql-connector-java 5中的
com.mysql.cj.jdbc.Driver 是 mysql-connector-java 6中的
而項目在創建過程中添加組件時,均是默認最新版本,所有可在下圖中查看當前項目中使用的Mysql版本就明白了
查看當前項目使用的mysql組件版本
解決方案就是驅動與Mysql版本對應即可,而修改方式有如下兩種
1、Mysql6(含6)以上使用JDBC的com.mysql.cj.jdbc.Driver驅動
2、Mysql6以下使用JDBC的com.mysql.jdbc.Driver驅動
poc.xml中指定Mysql組件的版本配置如下

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <!--<scope>runtime</scope>-->
    <version>5.1.47</version><!--指定版本號-->
</dependency>

Tomcat配置

由於SpringBoot的打包特性是jar包,且使用默認集成的Tomcat服務器進行部署,而我們的項目部署之後訪問規範中是根據項目名稱以及接口名稱進行訪問,且要保證端口不與其他項目衝突,那麼我這邊也遵循其優點,進行配置自己項目名稱以及端口號

server:
  port: 17081 #項目端口號
  servlet:
    context-path: /itsdf07 #訪問的項目名

備註:SpringBoot項目同樣也可以打成war包,並且使用自行配置的Tomcat服務器進行部署,但是本文不做這方面的說明!

Mybatis數據庫映射配置

mybatis:
  mapper-locations: classpath:mapper/*.xml  #注意:一定要對應mapper映射xml文件的所在路徑
  type-aliases-package: com.aso.store.entity  # 注意:對應實體類的路徑
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl  # 配置控制檯打印sql查詢語句並附帶查詢結果數據

配置Mybatis Generator自動生成代碼

pom.xml中增加mybatis generator 自動生成代碼插件

<!-- mybatis generator 自動生成代碼插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.1</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
            </plugin>

resources中增加mybatis generator配置文件
增加generatorConfig.xml
配置mybatis generator文件的啓動方式
配置mybatis generator的啓動方式
在這裏插入圖片描述
啓動方式
驗證自動生成結果
驗證自動生成結果

Thymeleaf配置

#  ###ThymeLeaf配置
thymeleaf:
  ##配置模板引擎:是讓controller層到templates文件夾尋找xx.html(src/main/resources/templates)
  prefix: classpath:/templates/ #配置前綴,可以不配置,因爲靜態頁面存放的默認位置就是該templates
  suffix: .html #配置後綴,也是可以不用配置,也是默認html
  cache: false #啓用模板緩存。
  encoding: UTF-8 #模板編碼
  content-type: text/html; charse=utf-8 #Content-Type值
  check-template: true #在呈現模板之前檢查模板是否存在。
  check-template-location: true #檢查模板位置是否存在。
  enabled: true #啓用MVC Thymeleaf視圖分辨率。
  excluded-view-names: #應該從解決方案中排除的視圖名稱的逗號分隔列表。
  mode: HTML5 #應用於模板的模板模式。另請參見StandardTemplateModeHandlers。
  template-resolver-order: #鏈中模板解析器的順序。
  view-names: #可以解析的視圖名稱的逗號分隔列表。

Thymeleaf模板引擎的配置,如果只是簡單的頁面跳轉,則可以不用配置,因爲靜態頁面的默認存放路徑就是在classpath:/templates/路徑下

解決跨域問題

出於安全原因,瀏覽器禁止Ajax調用駐留在當前原點之外的資源

使用區分不同開發環境下的配置文件

在一個規範的項目運作中,從項目的研發到最後的上線生成過程中,應該要經過四種項目環境
1、本地開發環境,即dev版本
2、測試環境,即test版本
3、預生產環境,即pre版本
4、生產環境,即prod,且一般只有運維可以操作
在以上4中環境中,如果只用一個配置文件來達到四種環境下的不同配置,那麼真的是一件不小的苦力活,而且還無法保證其改動的百分百正確性,那麼java的面向對象封裝思想就又體現出了其優勢:即把共性的部分提取出來,不同環境的差異配置到不同環境的配置文件中,然後通過spring.profiles.active進行切換,切換規則爲根據application-*.yml命名中*的內容進行配置
不同開發環境下的配置文件
從上圖,可以看出,原本在application.yml中配置的端口號以及JDBC連接數據庫的驅動配置已經看不見了,那麼如果項目最起碼的不配置JDBC連接數據庫的驅動配置,項目肯定是啓動不了的,那麼配置到哪裏去了呢?這裏已開發環境和預生產環境的配置文件作爲例子進行截圖對比
在這裏插入圖片描述

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章