您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Spring Boot使用系統(tǒng)參數(shù)表提升系統(tǒng)的靈活性”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Spring Boot使用系統(tǒng)參數(shù)表提升系統(tǒng)的靈活性”吧!
一、使用系統(tǒng)參數(shù)表的好處
二、系統(tǒng)參數(shù)表的表結(jié)構(gòu)
三、系統(tǒng)參數(shù)表在項(xiàng)目中的使用
3.1、Entity類
3.2、Dao類
3.3、Service類
3.4、ServiceImpl類
3.5、全局配置服務(wù)類
3.6、啟動(dòng)時(shí)加載
3.7、在服務(wù)實(shí)現(xiàn)類中訪問系統(tǒng)參數(shù)
以數(shù)據(jù)庫(kù)表形式存儲(chǔ)的系統(tǒng)參數(shù)表比配置文件(.properties文件或.yaml文件)要更靈活,因?yàn)闊o需重啟系統(tǒng)就可以動(dòng)態(tài)更新。
系統(tǒng)參數(shù)表可用于存儲(chǔ)下列數(shù)據(jù):
表字段枚舉值,如下列字段:
`question_type` TINYINT(4) NOT NULL DEFAULT 0 COMMENT '題型,1-單選題,2-多選題,3-問答題',
這個(gè)字段現(xiàn)在有3種取值,但是難保將來有擴(kuò)展的可能,如:是非題、計(jì)算題、應(yīng)用題等。
因此將取值的枚舉值用系統(tǒng)參數(shù)表來配置,可以提高系統(tǒng)擴(kuò)展靈活性。
另一方面,對(duì)于前端而言,就可以通過查詢系統(tǒng)參數(shù)表數(shù)據(jù),用于UI呈現(xiàn),而不必硬編碼。如前端需要用下拉框來顯示所有可能的”題型“,這個(gè)列表就可以查詢系統(tǒng)參數(shù)表來獲取。
因此可以將所有字段枚舉值納入系統(tǒng)參數(shù)表管理。
參數(shù)設(shè)置,如郵件參數(shù),對(duì)接的第三方系統(tǒng)的URL等。
系統(tǒng)參數(shù)表的表結(jié)構(gòu)如下:
DROP TABLE IF EXISTS `sys_parameters`; CREATE TABLE `sys_parameters` ( `class_id` INT(11) NOT NULL DEFAULT 0 COMMENT '參數(shù)大類id', `class_key` VARCHAR(60) NOT NULL DEFAULT '' COMMENT '參數(shù)大類key', `class_name` VARCHAR(60) NOT NULL DEFAULT '' COMMENT '參數(shù)大類名稱', `item_id` INT(11) NOT NULL DEFAULT 0 COMMENT '參數(shù)大類下子項(xiàng)id', `item_key` VARCHAR(200) NOT NULL DEFAULT '' COMMENT '子項(xiàng)key', `item_value` VARCHAR(200) NOT NULL DEFAULT '' COMMENT '子項(xiàng)值', `item_desc` VARCHAR(512) NOT NULL DEFAULT '' COMMENT '子項(xiàng)描述', -- 記錄操作信息 `login_name` VARCHAR(80) NOT NULL DEFAULT '' COMMENT '操作人賬號(hào)', `delete_flag` TINYINT(4) NOT NULL DEFAULT 0 COMMENT '記錄刪除標(biāo)記,1-已刪除', `create_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '創(chuàng)建時(shí)間', `update_time` DATETIME DEFAULT NULL ON UPDATE NOW() COMMENT '更新時(shí)間', PRIMARY KEY (`class_id`, `item_id`) ) ENGINE = InnoDB DEFAULT CHARSET = utf8 COMMENT '系統(tǒng)參數(shù)表';
說明:
class_id字段只要確保一個(gè)參數(shù)大類(如一個(gè)枚舉字段名)使用唯一值。使用class_key和item_key自動(dòng),便于提高記錄數(shù)據(jù)和代碼的可讀性。class_key一般可以取字段名,但如果發(fā)生同名時(shí),需要修改,確保不同表的同名字段,使用不同的class_key。對(duì)于枚舉值類型,item_key可以取item_id相同的值,只是數(shù)據(jù)類型不同,此item_key轉(zhuǎn)換成整型數(shù),就是對(duì)應(yīng)字段的值。
這個(gè)表的數(shù)據(jù)一般可以由開發(fā)人員提供,包括初始或變動(dòng)的SQL腳本,由DBA執(zhí)行,項(xiàng)目無需為此開發(fā)界面來維護(hù)。
下面是初始腳本示例:
INSERT INTO sys_parameters(class_id, class_key, class_name, item_id, item_key, item_value, item_desc) VALUES (11, 'receive_flag', '短信接收標(biāo)志', 0, '0', '未接收', ''); INSERT INTO sys_parameters(class_id, class_key, class_name, item_id, item_key, item_value, item_desc) VALUES (11, 'receive_flag', '短信接收標(biāo)志', 1, '1', '已接收', ''); INSERT INTO sys_parameters(class_id, class_key, class_name, item_id, item_key, item_value, item_desc) VALUES (11, 'receive_flag', '短信接收標(biāo)志', 2, '2', '發(fā)送失敗', ''); INSERT INTO sys_parameters(class_id, class_key, class_name, item_id, item_key, item_value, item_desc) VALUES (12, 'question_type', '題型', 1, '1', '單選題', ''); INSERT INTO sys_parameters(class_id, class_key, class_name, item_id, item_key, item_value, item_desc) VALUES (12, 'question_type', '題型', 2, '2', '多選題', ''); INSERT INTO sys_parameters(class_id, class_key, class_name, item_id, item_key, item_value, item_desc) VALUES (12, 'question_type', '題型', 3, '3', '問答題', ''); INSERT INTO sys_parameters(class_id, class_key, class_name, item_id, item_key, item_value, item_desc) VALUES (101, 'url_param', 'URL參數(shù)', 0, 'url_prefix', 'http://questinvest.abc.com:8880', 'url前綴部分'); INSERT INTO sys_parameters(class_id, class_key, class_name, item_id, item_key, item_value, item_desc) VALUES (101, 'url_param', 'URL參數(shù)', 1, 'url_action', '/questInvest/show', '請(qǐng)求接口方法');
在Spring Boot項(xiàng)目中,系統(tǒng)參數(shù)表一般只需在應(yīng)用啟動(dòng)時(shí)加載一次,并提供更新接口允許管理員來更新數(shù)據(jù)。下面詳細(xì)說明使用方法。
先定義系統(tǒng)參數(shù)表的實(shí)體類,實(shí)體類為SysParameter,代碼如下:
package com.abc.questInvest.entity; import javax.persistence.Column; import lombok.Data; /** * @className : SysParameter * @description : 系統(tǒng)參數(shù)信息對(duì)象類 * */ @Data public class SysParameter { //參數(shù)大類id @Column(name = "class_id") private Integer classId; //參數(shù)大類key @Column(name = "class_key") private String classKey; //參數(shù)大類名稱 @Column(name = "class_name") private String className; //子項(xiàng)id @Column(name = "item_id") private Integer itemId; //子項(xiàng)key @Column(name = "item_key") private String itemKey; //子項(xiàng)值 @Column(name = "item_value") private String itemValue; //子項(xiàng)描述 @Column(name = "item_desc") private String itemDesc; //========記錄操作信息================ // 操作人姓名 @Column(name = "login_name") private String loginName; // 記錄刪除標(biāo)記,保留 @Column(name = "delete_flag") private Byte deleteFlag; // 創(chuàng)建時(shí)間 @Column(name = "create_time") private Date createTime; // 更新時(shí)間 @Column(name = "update_time") private Date updateTime; }
數(shù)據(jù)訪問類為SysParameterDao,代碼如下:
package com.abc.questInvest.dao; import java.util.List; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import com.abc.questInvest.entity.SysParameter; /** * @className : SysParameterDao * @description : sys_parameters表數(shù)據(jù)訪問類 * */ @Mapper public interface SysParameterDao { //查詢所有系統(tǒng)參數(shù),按class_id,item_id排序 @Select("SELECT class_id,class_key,class_name,item_id,item_key,item_value,item_desc" + " FROM sys_parameters WHERE delete_flag = 0" + " ORDER BY class_id,item_id") List<SysParameter> selectAll(); }
SysParameterDao類,使用Mybatis,只需提供查詢接口就行了,因?yàn)樾薷脑跀?shù)據(jù)庫(kù)后臺(tái)執(zhí)行了。當(dāng)然如果項(xiàng)目方認(rèn)為有必要提供界面來維護(hù)該表,則可增加相應(yīng)CRUD的接口。
服務(wù)接口類為SysParameterService,代碼如下:
package com.abc.questInvest.service; import java.util.List; import com.abc.questInvest.entity.SysParameter; /** * @className : SysParameterService * @description : 系統(tǒng)參數(shù)數(shù)據(jù)服務(wù) * */ public interface SysParameterService { /** * * @methodName : loadData * @description : 加載數(shù)據(jù)庫(kù)中數(shù)據(jù),允許重復(fù)調(diào)用 * @return : 成功返回true,否則返回false。 * */ public boolean loadData(); /** * * @methodName : getParameterClass * @description : 獲取指定classKey的參數(shù)類別的子項(xiàng)列表 * @param classKey : 參數(shù)類別key * @return : 指定classKey的參數(shù)類別的子項(xiàng)列表 * */ public List<SysParameter> getParameterClass(String classKey); /** * * @methodName : getParameterItemByKey * @description : 根據(jù)classKey和itemKey獲取參數(shù)子項(xiàng) * @param classKey : 參數(shù)類別key * @param itemKey : 子項(xiàng)key * @return : SysParameter對(duì)象 * */ public SysParameter getParameterItemByKey(String classKey,String itemKey); /** * * @methodName : getParameterItemByValue * @description : 根據(jù)classKey和itemValue獲取參數(shù)子項(xiàng) * @param classKey : 參數(shù)類別key * @param itemValue : 子項(xiàng)值 * @return : SysParameter對(duì)象 * */ public SysParameter getParameterItemByValue(String classKey,String itemValue); }
SysParameterService類定義了下列接口方法:
loadData方法,用于初始加載數(shù)據(jù)和更新數(shù)據(jù)。
getParameterClass方法,獲取指定classKey的類別的所有子項(xiàng)列表。此方法調(diào)用會(huì)非常頻繁。
getParameterItemByKey方法,根據(jù)classKey和itemKey獲取參數(shù)子項(xiàng),用于根據(jù)枚舉值顯示物理含義。此方法調(diào)用會(huì)非常頻繁。
getParameterItemByValue方法,根據(jù)classKey和itemValue獲取參數(shù)子項(xiàng),用于根據(jù)物理含義取得枚舉值。此方法調(diào)用會(huì)非常頻繁。
服務(wù)實(shí)現(xiàn)類為SysParameterServiceImpl,代碼如下:
package com.abc.questInvest.service.impl; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.abc.questInvest.dao.SysParameterDao; import com.abc.questInvest.entity.SysParameter; import com.abc.questInvest.service.SysParameterService; import lombok.extern.slf4j.Slf4j; /** * @className : SysParameterServiceImpl * @description : SysParameterService實(shí)現(xiàn)類 * @summary : 實(shí)現(xiàn)對(duì)系統(tǒng)參數(shù)的管理 * */ @Slf4j @Service public class SysParameterServiceImpl implements SysParameterService{ //sys_parameters表數(shù)據(jù)訪問對(duì)象 @Autowired private SysParameterDao sysParameterDao; //管理全部的SysParameter表記錄 private Map<String,Map<String,SysParameter>> sysParameterMap = new HashMap<String,Map<String,SysParameter>>(); /** * * @methodName : loadData * @description : 加載數(shù)據(jù)庫(kù)中數(shù)據(jù) * @return : 成功返回true,否則返回false。 * */ @Override public boolean loadData() { try { //查詢sys_parameters表,獲取全部數(shù)據(jù) List<SysParameter> sysParameterList = sysParameterDao.selectAll(); synchronized(sysParameterMap) { //先清空map,便于刷新調(diào)用 sysParameterMap.clear(); //將查詢結(jié)果放入map對(duì)象中,按每個(gè)類別組織 for(SysParameter item : sysParameterList) { String classKey = item.getClassKey(); String itemKey = item.getItemKey(); Map<String,SysParameter> sysParameterClassMap = null; if (sysParameterMap.containsKey(classKey)) { //如果存在該類別,則獲取對(duì)象 sysParameterClassMap = sysParameterMap.get(classKey); }else { //如果不存在該類別,則創(chuàng)建 sysParameterClassMap = new HashMap<String,SysParameter>(); //加入map中 sysParameterMap.put(classKey, sysParameterClassMap); } sysParameterClassMap.put(itemKey,item); } } }catch(Exception e) { log.error(e.getMessage()); e.printStackTrace(); return false; } return true; } /** * * @methodName : getParameterClass * @description : 獲取指定classKey的參數(shù)類別的子項(xiàng)列表 * @param classKey : 參數(shù)類別key * @return : 指定classKey的參數(shù)類別的子項(xiàng)列表 * */ @Override public List<SysParameter> getParameterClass(String classKey){ List<SysParameter> sysParameterList = new ArrayList<SysParameter>(); //獲取classKey對(duì)應(yīng)的子map,將所有子項(xiàng)加入列表中 if (sysParameterMap.containsKey(classKey)) { Map<String,SysParameter> sysParameterClassMap = sysParameterMap.get(classKey); for(SysParameter item : sysParameterClassMap.values()) { sysParameterList.add(item); } } return sysParameterList; } /** * * @methodName : getParameterItemByKey * @description : 根據(jù)classKey和itemKey獲取參數(shù)子項(xiàng) * @param classKey : 參數(shù)類別key * @param itemKey : 子項(xiàng)key * @return : SysParameter對(duì)象 * */ @Override public SysParameter getParameterItemByKey(String classKey,String itemKey) { SysParameter sysParameter = null; if (sysParameterMap.containsKey(classKey)) { //如果classKey存在 Map<String,SysParameter> sysParameterClassMap = sysParameterMap.get(classKey); if (sysParameterClassMap.containsKey(itemKey)) { //如果itemKey存在 sysParameter = sysParameterClassMap.get(itemKey); } } return sysParameter; } /** * * @methodName : getParameterItemByValue * @description : 根據(jù)classKey和itemValue獲取參數(shù)子項(xiàng) * @param classKey : 參數(shù)類別key * @param itemValue : 子項(xiàng)值 * @return : SysParameter對(duì)象 * */ @Override public SysParameter getParameterItemByValue(String classKey,String itemValue) { SysParameter sysParameter = null; if (sysParameterMap.containsKey(classKey)) { //如果classKey存在 Map<String,SysParameter> sysParameterClassMap = sysParameterMap.get(classKey); //遍歷 for (Map.Entry<String,SysParameter> item : sysParameterClassMap.entrySet()) { if(item.getValue().getItemValue().equals(itemValue)) { //如果匹配值 sysParameter = item.getValue(); break; } } } return sysParameter; } }
SysParameterServiceImpl類使用了Map<String,Map<String,SysParameter>>類型的屬性變量sysParameterMap來管理全部的系統(tǒng)參數(shù),外層Map管理classKey到Map<String,SysParameter>的映射關(guān)系,每一項(xiàng)為一個(gè)參數(shù)類別,而里層Map<String,SysParameter>,用于管理itemKey與SysParameter之間的映射關(guān)系,每一項(xiàng)為該類別下的一個(gè)子項(xiàng)。使用sysParameterMap屬性的目的,是將所有系統(tǒng)參數(shù)都加載到內(nèi)存中,從而無需頻繁訪問數(shù)據(jù)庫(kù)。
loadData方法,用于初始加載數(shù)據(jù)和更新時(shí)刷新數(shù)據(jù),為了防止更新時(shí)臟讀數(shù)據(jù),加了同步鎖。這個(gè)方法調(diào)用不頻繁。
全局配置服務(wù)類用于管理全局配置參數(shù),包括系統(tǒng)參數(shù)、權(quán)限樹等。如果只有一種參數(shù),可以不必有此類,因?yàn)檫@樣加了一層殼。
服務(wù)接口類為GlobalConfigService,代碼如下:
package com.abc.questInvest.service; /** * @className : GlobalConfigService * @description : 全局變量管理類 * */ public interface GlobalConfigService { /** * * @methodName : loadData * @description : 加載數(shù)據(jù) * @return : 成功返回true,否則返回false * */ public boolean loadData(); //獲取SysParameterService對(duì)象 public SysParameterService getSysParameterService(); //獲取其它配置數(shù)據(jù)服務(wù)對(duì)象 //public FunctionTreeService getFunctionTreeService(); }
GlobalConfigService提供了下列接口方法:
loadData方法,加載配置對(duì)象數(shù)據(jù),確定多個(gè)配置對(duì)象的加載次序。
getSysParameterService方法,獲取系統(tǒng)參數(shù)服務(wù)類對(duì)象。
獲取其它可能的配置服務(wù)對(duì)象的方法。
服務(wù)實(shí)現(xiàn)類為GlobalConfigServiceImpl,代碼如下:
package com.abc.questInvest.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.abc.questInvest.service.FunctionTreeService; import com.abc.questInvest.service.GlobalConfigService; import com.abc.questInvest.service.RoleFuncRightsService; import com.abc.questInvest.service.SysParameterService; import com.abc.questInvest.service.TableCodeConfigService; /** * @className : GlobalConfigServiceImpl * @description : GlobalConfigService實(shí)現(xiàn)類 * */ @Service public class GlobalConfigServiceImpl implements GlobalConfigService{ //系統(tǒng)參數(shù)表數(shù)據(jù)服務(wù)對(duì)象 @Autowired private SysParameterService sysParameterService; //其它配置數(shù)據(jù)服務(wù)對(duì)象 /** * * @methodName : loadData * @description : 加載數(shù)據(jù) * @return : 成功返回true,否則返回false * */ @Override public boolean loadData() { boolean bRet = false; //加載sys_parameters表記錄 bRet = sysParameterService.loadData(); if (!bRet) { return bRet; } //加載其它配置數(shù)據(jù) return bRet; } //獲取SysParameterService對(duì)象 @Override public SysParameterService getSysParameterService() { return sysParameterService; } //獲取其它配置數(shù)據(jù)服務(wù)對(duì)象方法 }
全局配置服務(wù)類在應(yīng)用啟動(dòng)時(shí)加載到Spring容器中,這樣可實(shí)現(xiàn)共享,減少對(duì)數(shù)據(jù)庫(kù)的訪問壓力。
實(shí)現(xiàn)一個(gè)ApplicationListener類,代碼如下:
package com.abc.questInvest; import javax.servlet.ServletContext; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; import org.springframework.web.context.WebApplicationContext; import com.abc.questInvest.service.GlobalConfigService; /** * @className : ApplicationStartup * @description : 應(yīng)用偵聽器 * */ @Component public class ApplicationStartup implements ApplicationListener<ContextRefreshedEvent>{ //全局變量管理對(duì)象,此處不能自動(dòng)注入 private GlobalConfigService globalConfigService = null; @Override public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { try { if(contextRefreshedEvent.getApplicationContext().getParent() == null){ //root application context 沒有parent. System.out.println("========定義全局變量=================="); // 將 ApplicationContext 轉(zhuǎn)化為 WebApplicationContext WebApplicationContext webApplicationContext = (WebApplicationContext)contextRefreshedEvent.getApplicationContext(); // 從 webApplicationContext 中獲取 servletContext ServletContext servletContext = webApplicationContext.getServletContext(); //加載全局變量管理對(duì)象 globalConfigService = (GlobalConfigService)webApplicationContext.getBean(GlobalConfigService.class); //加載數(shù)據(jù) boolean bRet = globalConfigService.loadData(); if (false == bRet) { System.out.println("加載全局變量失敗"); return; } //====================================================================== // servletContext設(shè)置值 servletContext.setAttribute("GLOBAL_CONFIG_SERVICE", globalConfigService); } } catch (Exception e) { e.printStackTrace(); } } }
注意,globalConfigService不能自動(dòng)注入,否則得到空指針。通過下列代碼來加載bean。
//加載全局變量管理對(duì)象 globalConfigService = (GlobalConfigService)webApplicationContext.getBean(GlobalConfigService.class);
代碼中,將globalConfigService對(duì)象作為全局變量加入ServletContext中,就可以實(shí)現(xiàn)共享了。
在啟動(dòng)類中,加入該應(yīng)用偵聽器ApplicationStartup。
public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(QuestInvestApplication.class); springApplication.addListeners(new ApplicationStartup()); springApplication.run(args); }
HttpServletRequest類型對(duì)象request在控制器方法中可以獲取,可作為參數(shù)傳入服務(wù)實(shí)現(xiàn)類的方法中。下面是服務(wù)實(shí)現(xiàn)類訪問系統(tǒng)參數(shù)的示例代碼:
//獲取ServletContext對(duì)象 ServletContext servletContext = request.getServletContext(); //獲取全部數(shù)據(jù)服務(wù)對(duì)象 GlobalConfigService globalConfigService = (GlobalConfigService)servletContext.getAttribute("GLOBAL_CONFIG_SERVICE"); //獲取系統(tǒng)參數(shù)url_prefix的值 String url_prefix = ""; SysParameter sysParameter = null; sysParameter = globalConfigService.getSysParameterService().getParameterItemByKey("url_param", "url_prefix"); if (sysParameter != null) { url_prefix = sysParameter.getItemValue(); }
到此,相信大家對(duì)“Spring Boot使用系統(tǒng)參數(shù)表提升系統(tǒng)的靈活性”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。