SpringBoot整合說明

燕子去了發表於2024-04-07

整合說明

jetty伺服器

內嵌Tomcat是SpringBoot的輔助功能之一,將Tomcat伺服器作為物件執行,並將該物件交給Spring容器管理

jetty伺服器:更輕量級,負載效能低

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactedId>spring-boot-starter-web</artifactedId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactedId>spring-boot-starter-tomcat</artifactedId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactedId>spring-boot-starter-jetty</artifactedId>
</dependency>
SpringBoot整合Mybatis-Plus

idea建立boot專案時換一個網址:https://start.aliyun.com或maven工程匯入對應依賴

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.3</version>
</dependency>
#配置mp相關配置,指定表名開頭
mybatis-plus:
	global-config:
		db-config:
			table-prefix: tb1_
SpringBoot整合Druid
druid-spring-boot-starter
# 方法1
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/your_database
    username: your_username
    password: your_password
# 方法2(推薦)
spring:
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/your_database
      username: your_username
      password: your_password

配置具體含義可以參考Druid的官方文件:Druid配置屬性官方文件

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/your_database
    username: your_username
    password: your_password
    driver-class-name: com.mysql.cj.jdbc.Driver
    druid:
      initial-size: 5
      min-idle: 5
      max-active: 20
      max-wait: 60000
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 1
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      filters: stat,wall,log4j
      connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
SpringBoot整合JUnit
<dependency>
    <groupId>org.springframewor.bootk</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

啟動類就起到了配置類的作用,他會把他所在的包及子包掃描一遍;測試類會預設載入引導類,初始化spring的環境

測試類會載入同層包下的帶有@SpringBootApplication裡的@SpringBootConfiguration裡的配置資訊

如果測試類在SpringBoot啟動類的包或子包中,可以省略啟動類的設定,也就是省略classes的設定,如果不在一個包下就指定引數@SpringBootTest(classes = 引導類名.class)

@SpringBootTest
class test{
    @Autowired
    private A a;
    @Test
    public void save(){
        a.save();
    }
}
SpringBoot整合mybatis

SpringBoot版本低於2.4.3(不含),Mysql驅動版本大於8.0時,需要在url連線串中配置時區或在MySQL資料庫端配置時區

驅動類過時:driver-class-name: com.mysql.cj.jdbc.Driver

加個@Mapper就可以

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.0</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url:jdbc:mysql: //localhost:3306/ssm_db?serverTimezone=UTC
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource

相關文章