1. 程式人生 > 實用技巧 >Spring Boot整合多種資料庫

Spring Boot整合多種資料庫

Spring Boot整合資料庫


使用MySQL資料庫

資料庫配置
application.properties
# mysql
# 資料庫驅動
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
# 資料庫地址
spring.datasource.url=jdbc:mysql://101.200.42.195:3306/easydb?useUnicode=true&characterEncoding=utf8&useSSL=false
# 資料庫使用者名稱
spring.datasource.username=root
# 資料庫密碼
spring.datasource.password=root
依賴
        <!-- mysql需要新增的依賴 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

使用SQL Server資料庫

資料庫配置
application.properties
# SQL Server
# 資料庫地址
spring.datasource.url=jdbc:sqlserver://192.168.16.218:1433;databaseName=dev_btrpawn
# 資料庫使用者名稱
spring.datasource.username=sa
# 資料庫密碼
spring.datasource.password=sa
# 資料庫驅動
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServer
依賴
        <!-- SQL Service需要新增的依賴 
--> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <scope>runtime</scope> </dependency>

使用Orcale資料庫

配置
application.properties
# Oracle
# 資料庫驅動
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
# 資料庫地址
spring.datasource.url=jdbc:oracle:thin:@172.17.112.249:1521:orcl
# 資料庫使用者名稱
spring.datasource.username=sde
# 資料庫密碼
spring.datasource.password=sde
依賴
        <!-- Orcale資料庫需要新增的依賴 -->
        <dependency>
            <groupId>com.oracle.ojdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <scope>runtime</scope>
        </dependency>

使用MongoDB資料庫

配置
# MongoDB
# 無密碼
#spring.data.mongodb.uri=mongodb://localhost:27017/test
# 有密碼
spring.data.mongodb.uri=mongodb://root(username):root(password)@localhost(ip地址):27017(埠號)/test(collections/資料庫)
依賴
        <!-- MongoDB資料庫依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

使用Neo4j資料庫

配置
# Neo4j資料庫
spring.data.neo4j.uri=http://localhost:7474
spring.data.neo4j.username=root
spring.data.neo4j.password=root
依賴
        <!-- neo4j資料庫依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-neo4j</artifactId>
        </dependency>

使用redis資料庫

配置
# Redis資料庫配置
# Redis資料庫索引,預設為0
spring.redis.database=0
# Redis伺服器地址
spring.redis.host=localhost
# Redis伺服器連線埠
spring.redis.port=6379
# Redis伺服器連線密碼
spring.redis.password=
# 連線池最大連線數,使用負值則表示沒有任何限制
spring.redis.pool.max-active=8
# 連線池最大阻塞等待時間,使用負值則表示沒有任何限制
spring.redis.pool.max-wait=-1
# 連線池中得最大空閒連線
spring.redis.pool.max-idle=8
# 連線池中得最小空閒連線
spring.redis.pool.min-idle=0
# 連線超時時間(毫秒)
spring.redis.timeout=0
依賴
        <!-- redis資料庫依賴 -->        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

使用Mybatis資料庫

配置
# MyBatis配置
# 檢查Mybatis配置檔案是否存在
mybatis.check-config-location=true
# 配置檔案位置:一般用於配置別名等資訊
mybatis.config-location=classpath://mybatis/mybatis-config.xml
# mapper xml檔案地址
mybatis.mapper-locations=classpath*:/mapper/*.xml
# 日誌級別
logging.level.com.springboot.dao.UserMapper=debug
依賴
        <!-- Mybatis依賴 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>

使用Memcached資料庫

資料庫外掛

Mybatis-Generator

PageHelper外掛

Mybatis-Plus外掛

配置多資料來源

使用Druid資料庫連線池

配置類設定:略

還需要新增三個配置類

配置
# 使用Druid時需要配置這一屬性
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 連線池的配置資訊
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
# 配置獲取連線等待超時時間
spring.datasource.druid.max-evictable-idle-time-millis=60000
# 配置連線池中最小連線生成時間(毫秒)
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.validation-query=SELECT 1 FROM DUAL
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
# 開啟PSCache,並且指定每個連線上PSCache的大小
spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
# 配置監控攔截的filters,去掉後監控介面sql無法統計,wall用於防火牆
spring.datasource.druid.filters=stat,wall,log4j
依賴
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.16</version>
        </dependency>