在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語(yǔ)句
2. 方式1,通過(guò)實(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)用接口,通過(guò)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中的條件判斷、循環(huán)、跳轉(zhuǎn)語(yǔ)句基礎(chǔ)學(xué)習(xí)筆記
if、for和while循環(huán)、switch等這些基本的程序流程控制語(yǔ)句基本上是每個(gè)編程語(yǔ)言的標(biāo)配,在入門環(huán)節(jié)中,這里對(duì)Swift中的條件判斷、循環(huán)、跳轉(zhuǎn)語(yǔ)句基礎(chǔ)學(xué)習(xí)筆記作了一個(gè)整理:2016-06-06swift如何利用系統(tǒng)庫(kù)將漢字轉(zhuǎn)換為拼音詳解
將漢字轉(zhuǎn)換為拼音更利于我們大家開發(fā)搜索功能,所以這篇文章主要給大家介紹了關(guān)于swift如何利用系統(tǒng)庫(kù)將漢字轉(zhuǎn)換為拼音的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-10-10Swift中風(fēng)味各異的類型擦除實(shí)例詳解
你也許曾聽(tīng)過(guò)類型擦除,甚至也使用過(guò)標(biāo)準(zhǔn)庫(kù)提供的類型擦除類型如 AnySequence,下面這篇文章主要給大家介紹了關(guān)于Swift中風(fēng)味各異的類型擦除的相關(guān)資料,需要的朋友可以參考下2022-04-04Swift?中的?RegexBuilder學(xué)習(xí)指南
這篇文章主要為大家介紹了Swift中的RegexBuilder學(xué)習(xí)指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Swift語(yǔ)言實(shí)現(xiàn)地圖坐標(biāo)彈跳動(dòng)畫
這篇文章主要介紹了用Swift語(yǔ)言實(shí)現(xiàn)地圖坐標(biāo)彈跳動(dòng)畫的方法主要應(yīng)用iOS7來(lái)實(shí)現(xiàn)此功能,需要的朋友可以參考下2015-07-07Swift map和filter函數(shù)原型基礎(chǔ)示例
這篇文章主要為大家介紹了Swift map和filter函數(shù)原型基礎(chǔ)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07