SpringBoot工程中測(cè)試連接DaMeng數(shù)據(jù)庫實(shí)踐
一、環(huán)境準(zhǔn)備與依賴配置
SpringBoot工程:
達(dá)夢(mèng)數(shù)據(jù)庫
二、數(shù)據(jù)庫配置信息
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
primary:
driver-class-name: dm.jdbc.driver.DmDriver
url: jdbc:dm://xxxxx.5:5236/sacoa_business
username: SYSDBA
password: SYSDBA
second:
url: jdbc:mysql://xxxxx:3306/testdb?characterEncoding=UTF-8&useUnicode=true&serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: chen
third:
url: jdbc:dm://xxxxx:5236/sacoa_platform
username: SYSDBA
password: SYSDBA
driver-class-name: dm.jdbc.driver.DmDriver
#初始連接數(shù)量
initial-size: 20
#最小連接數(shù)量
min-idle: 20
#最大連接數(shù)量
max-active: 1000
#獲取連接等待超時(shí)的時(shí)間 單位是毫秒,這里配置60秒
max-wait: 30000三、添加相關(guān)依賴
如果是微服務(wù)結(jié)構(gòu),首先在項(xiàng)目的pom.xml文件中添加相關(guān)依賴
<!--達(dá)夢(mèng)-->
<dependency>
<groupId>com.dameng</groupId>
<artifactId>DmJdbcDriver18</artifactId>
<version>8.1.3.140</version>
</dependency>其次在使用連接數(shù)據(jù)庫的模塊中,再次引入依賴!
如果是SpringBoot項(xiàng)目只需要引入一次即可
四、controller測(cè)試
@RequestMapping(HttpConstants.REST_URL_PREFIX+"/test")
@Api(value = "TestController", tags = "測(cè)試")
@RestController
public class TestController {
@Resource
private RedisUtil redisUtil;
@Autowired
private JdbcTemplate jdbcTemplate;
@ApiOperation(value = "測(cè)試dataSource" , httpMethod = "GET")
@GetMapping("/testConnection")
public String testDatabaseConnection() {
List<String> result = jdbcTemplate.queryForList("SELECT 'Connected to DM database successfully!' FROM dual", String.class);
return result.isEmpty() ? "Connection failed." : result.get(0);
}
@ApiOperation(value = "測(cè)試" , httpMethod = "GET")
@GetMapping("{name}")
public ResponseEntity<String> test(@PathVariable("name")String name) {
return ResponseEntity.ok("hello + " + name);
}
@ApiOperation(value = "測(cè)試Redis" , httpMethod = "GET")
@GetMapping("/redis/{name}")
public ResponseEntity<String> testRedis(@PathVariable("name") String name) {
redisUtil.set(name, "Redis: " + name);
Object nameObj = redisUtil.get(name);
return ResponseEntity.ok("hello + " + nameObj);
}
}
五、測(cè)試

測(cè)試連接成功!
六、總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- springboot配置HikariDataSource連接池的兩種方法
- Springboot實(shí)現(xiàn)Clickhouse連接池的配置和接口查詢
- 解決springboot連接數(shù)據(jù)庫異常問題(Could?not?obtain?connection?metadata)
- java-springboot3.2+多數(shù)據(jù)源+hikari連接池的配置使用
- SpringBoot整合mongoDB并自定義連接池實(shí)現(xiàn)多數(shù)據(jù)源配置教程
- SpringBoot中Redis連接超時(shí)的解決全過程
- Springboot中redis使用lettuce連接池經(jīng)常連接超時(shí)問題分析及解決
- SpringBoot實(shí)現(xiàn)多數(shù)據(jù)源連接和切換的完整方案
相關(guān)文章
10張圖總結(jié)出并發(fā)編程最佳學(xué)習(xí)路線
這篇文章主要介紹了并發(fā)編程的最佳學(xué)習(xí)路線,文中通過圖片介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08
SpringBoot整合chatGPT的項(xiàng)目實(shí)踐
本文主要介紹了SpringBoot整合chatGPT的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
Java數(shù)據(jù)結(jié)構(gòu)之稀疏數(shù)組的實(shí)現(xiàn)與應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Java數(shù)據(jù)結(jié)構(gòu)中稀疏數(shù)組的實(shí)現(xiàn)與應(yīng)用,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的可以了解一下2022-10-10
SpringBoot下無節(jié)制和數(shù)據(jù)庫建立連接的問題及解決方法
本文介紹了無節(jié)制建立MySQL連接的危害,包括數(shù)據(jù)庫服務(wù)端資源耗盡、應(yīng)用端性能劣化和監(jiān)控與運(yùn)維困境,提出了系統(tǒng)性解決方案,包括連接池標(biāo)準(zhǔn)化配置、代碼規(guī)范與防御式編程、全鏈路監(jiān)控體系和架構(gòu)級(jí)優(yōu)化,感興趣的朋友一起看看吧2025-03-03
Java中如何快速構(gòu)建項(xiàng)目腳手架的實(shí)現(xiàn)
這篇文章主要介紹了Java中如何快速構(gòu)建項(xiàng)目腳手架,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05

