在SpringBoot中實(shí)現(xiàn)適配器模式的兩種方式
1. 場(chǎng)景
當(dāng)我們后臺(tái)有兩個(gè)數(shù)據(jù)庫(kù),分別為mysql和oracle,根據(jù)前端參數(shù)中的數(shù)據(jù)庫(kù)類型字段,去查詢對(duì)應(yīng)sql語句
2. 方式1,通過實(shí)現(xiàn)類定義類型字段實(shí)現(xiàn)
2.1 創(chuàng)建接口
public interface DbService {
/**
* 獲取數(shù)據(jù)庫(kù)類型
* @return
*/
String getDbType();
/**
* 查詢數(shù)據(jù)庫(kù)sql
* @return
*/
String getDbSql();
}
2.2 創(chuàng)建mysql實(shí)現(xiàn)類
@Service
public class MysqlDbService implements DbService{
@Override
public String getDbType() {
return "mysql";
}
@Override
public String getDbSql() {
return "獲取mysql的SQL";
}
}
2.3 創(chuàng)建oracle實(shí)現(xiàn)類
@Service
public class OracleSDbService implements DbService{
@Override
public String getDbType() {
return "oracle";
}
@Override
public String getDbSql() {
return "獲取oracle的SQL";
}
}
2.4 創(chuàng)建接口,在接口中注入service集合,根據(jù)每個(gè)實(shí)現(xiàn)類中定義的dbType進(jìn)行匹配后進(jìn)行調(diào)用
@RestController
@RequestMapping("/test")
public class TestController {
@Resource
private List<DbService> dbServiceList;
@GetMapping("/getDbSql1")
public String getDbSql(@RequestParam String dbtype){
DbService dbService = dbServiceList.stream().filter(item -> dbtype.equals(item.getDbType())).findFirst().get();
return dbService.getDbSql();
}
}
2.5 測(cè)試,瀏覽器輸入

3. 方式2,以動(dòng)態(tài)service名稱的方式實(shí)現(xiàn)
3.1 創(chuàng)建接口
public interface DbService {
/**
* 獲取數(shù)據(jù)庫(kù)類型
* @return
*/
String getDbType();
/**
* 查詢數(shù)據(jù)庫(kù)sql
* @return
*/
String getDbSql();
}
3.2 創(chuàng)建創(chuàng)建mysql實(shí)現(xiàn)類,定義實(shí)現(xiàn)類名稱為mysqlDbService
@Service(value = "mysqlDbService")
public class MysqlDbService implements DbService{
@Override
public String getDbType() {
return "mysql";
}
@Override
public String getDbSql() {
return "獲取mysql的SQL";
}
}
3.3 創(chuàng)建創(chuàng)建oracle實(shí)現(xiàn)類,定義實(shí)現(xiàn)類名稱為oracleDbService
@Service(value = "oracleDbService")
public class OracleSDbService implements DbService{
@Override
public String getDbType() {
return "oracle";
}
@Override
public String getDbSql() {
return "獲取oracle的SQL";
}
}
3.4 引入ApplicationContext,獲取service方法名
@Component("applicationContextHelper")
public class ApplicationContextHelper implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public static <T> T popBean(String name, Class<T> clazz) {
if (applicationContext == null) {
return null;
}
return applicationContext.getBean(name, clazz);
}
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
applicationContext = context;
}
public ApplicationContext getInstance() {
return applicationContext;
}
}
3.5 調(diào)用接口,通過ApplicationContextHelper根據(jù)service名稱動(dòng)態(tài)獲取實(shí)現(xiàn)類,調(diào)用方法
@RestController
@RequestMapping("/test")
public class TestController {
@Resource
private List<DbService> dbServiceList;
@GetMapping("/getDbSql2")
public String getDbSql2(@RequestParam String dbtype){
DbService dbService = ApplicationContextHelper.popBean(dbtype + "DbService", DbService.class);
return dbService.getDbSql();
}
}
3.6 測(cè)試

到此這篇關(guān)于在SpringBoot中實(shí)現(xiàn)適配器模式的兩種方式的文章就介紹到這了,更多相關(guān)SpringBoot適配器模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Swift中的數(shù)據(jù)類型類型轉(zhuǎn)換
Swift中的類型轉(zhuǎn)換可以結(jié)合類的繼承等面向?qū)ο蟮木幊烫匦詠磉M(jìn)行,本文中我們就來詳解Swift中的數(shù)據(jù)類型類型轉(zhuǎn)換,需要的朋友可以參考下2016-07-07
Swift實(shí)現(xiàn)倒計(jì)時(shí)5秒功能
這篇文章主要為大家詳細(xì)介紹了Swift實(shí)現(xiàn)倒計(jì)時(shí)5秒功能,在“登錄”和“注冊(cè)”頁(yè)面也有相似功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
詳解swift中xcworkspace多項(xiàng)目管理
給大家詳細(xì)講解了IOS開發(fā)中swift語言xcworkspace多項(xiàng)目管理的方法和介紹,一起參考一下。2017-11-11
SwiftUI?List在MacOS中的性能優(yōu)化示例
這篇文章主要為大家介紹了SwiftUI?List在MacOS中的性能優(yōu)化示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Swift中動(dòng)態(tài)調(diào)用實(shí)例方法介紹
這篇文章主要介紹了Swift中動(dòng)態(tài)調(diào)用實(shí)例方法介紹,在Swift中有一類很有意思的寫法,可以讓我們不直接使用實(shí)例來調(diào)用這個(gè)實(shí)例上的方法,而是通過類型取出這個(gè)類型的某個(gè)實(shí)例方法的簽名,然后再通過傳遞實(shí)例來拿到實(shí)際需要調(diào)用的方法,需要的朋友可以參考下2015-01-01

