天天看點

2021-04-281.SpringBoot 用法

1.SpringBoot 用法

1.1 環境切換

1.1.1 業務需求

1.1.2 多環境編輯

要求: 如果采用多環境測試,則要求每個環境中的資料項都應該保持一緻. 否則缺失可能導緻項目啟動異常.

多環境配置: 關鍵文法"—" 環境分割

定義環境名稱:

2021-04-281.SpringBoot 用法

預設環境名稱:
2021-04-281.SpringBoot 用法

全部配置:

2021-04-281.SpringBoot 用法

1.2 熱部署

1.2.1 需求說明

在開發階段每次修改完源碼都要重新開機伺服器,程式才能生效. 能否讓程式自動的完成監控,重新開機伺服器.

1.2.2 引入jar包

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

1.2.3 配置IDEA環境

組合鍵: ctrl + shift + alt + / 或者 ctrl + alt + a

2021-04-281.SpringBoot 用法

2.勾選自動編譯

2021-04-281.SpringBoot 用法

3).啟動IDEA自動編譯

2021-04-281.SpringBoot 用法

2.1建立SpringBoot項目

2.1.1 建立項目

2021-04-281.SpringBoot 用法

2.2.2 導入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_demo_2</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot_demo_2</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.5</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>

		<!--spring-boot-starter 啟動項  隻要導入jar包 則可以完成自動的配置
			暫時沒有資料庫的連結
		-->
		<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>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.4</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>
           

2.1.3 Mavenjar包作用範圍

1.test範圍是指測試範圍有效,在編譯和打包時都不會使用這個依賴

2.compile範圍是指編譯範圍内有效,在編譯和打包時都會将依賴存儲進去

3.provided依賴,在編譯和測試過程中有效,最後生成的war包時不會加入 例如:

servlet-api,因為servlet-api tomcat伺服器已經存在了,如果再打包會沖突

4.runtime在運作時候依賴,在編譯時候不依賴

預設依賴範圍是compile

2.1.4 資料源配置

#SpringBoot 開箱即用
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/jtadmin?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
    username: root
    password: root
           

關于參數說明:

1.serverTimezone=GMT%2B8 %2B “+” 号 新版本的驅動要求必須配置時區

2.&useUnicode=true&characterEncoding=utf8 使用Unicode編碼 要求字元UTF-8編碼

3.&autoReconnect=true 是否自動重連.

4.&allowMultiQueries=true 是否允許批量操作 同時執行多個sql!

2.1.5 Mybatis相關配置

#SpringBoot整合Mybatis配置
mybatis:
  #定義别名包
  type-aliases-package: com.jt.pojo
  mapper-locations: classpath:/mybatis/mappers/*.xml
  #開啟駝峰映射
  configuration:
    map-underscore-to-camel-case: true
           

2.1.6 編輯Mapper接口/映射檔案

2021-04-281.SpringBoot 用法

2.1.7 将Mapper接口交給容器管理

2021-04-281.SpringBoot 用法

2.2.8 Mybatis測試

編輯測試類,注意包路徑的寫法.

@SpringBootTest
public class TestSpringBoot {

    @Autowired
    private DemoUserMapper userMapper;

    @Test
    public void testFindAll(){

        System.out.println(userMapper.findAll());
    }

}
           

繼續閱讀