SpringBoot整合Web資源

qq_42317239發表於2020-10-15

編輯POM.xml檔案

在這<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.jt</groupId>
	<artifactId>springboot_jsp</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<!--parent標籤作用: 定義了SpringBoot中所有關聯專案的版本號資訊.-->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>


	<properties>
		<java.version>1.8</java.version>
		<!--專案打包時,跳過測試類打包-->
		<skipTests>true</skipTests>
	</properties>

	<!--開箱即用:SpringBoot專案只需要引入少量的jar包及配置,即可擁有其功能.
        spring-boot-starter 擁有開箱即用的能力.
        maven專案中依賴具有傳遞性.
            A 依賴  B  依賴 C專案   匯入A bc會自動依賴
    -->
	<dependencies>
		<!--直接依賴web springMVC配置-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<!--springBoot-start SpringBoot啟動項  -->
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!--SpringBoot重構了測試方式 可以在測試類中 直接引入依賴物件-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<!--支援熱部署 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>

		<!--引入外掛lombok 自動的set/get/構造方法外掛  -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>

		<!--引入資料庫驅動 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>

		<!--springBoot資料庫連線  -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>

		<!--spring整合mybatis-plus 只匯入MP包,刪除mybatis包 -->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.2.0</version>
		</dependency>

		<!--springBoot整合JSP新增依賴  -->
		<!--servlet依賴 -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
		</dependency>

		<!--jstl依賴 -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
		</dependency>

		<!--使jsp頁面生效 -->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
		</dependency>

	</dependencies>

	<!--在專案打包部署時生效,如果不新增build,則程式釋出時不然會報
        專案中沒有main方法.
    -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

編輯YML配置檔案

server:
  port: 8090
  servlet:
    context-path: /
spring:
  datasource:
    #driver-class-name: com.mysql.cj.jdbc.Driver  #驅動註釋,採用預設的方式
    url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
    username: root
    password: root

  #引入mvc配置
  mvc:         #引入mvn配置
    view:
      prefix: /WEB-INF/     # /預設代表根目錄 src/main/webapp
      suffix: .jsp

#Mybatisplus整合
mybatis-plus:
  #定義別名包 將實體物件的包路徑進行封裝.
  type-aliases-package: com.jt.pojo
  #新增xml檔案的依賴
  mapper-locations: classpath:/mybatis/mappers/*.xml
  #開啟駝峰對映
  configuration:
    map-underscore-to-camel-case: true

# 配置資料庫日誌
logging:
  level:
    #列印哪個包下的日誌資訊.
    com.jt.mapper: debug
  

相關文章