SpringBoot+Redis實(shí)現(xiàn)數(shù)據(jù)字典的方法
前言
我們在日常的開發(fā)過程中針對一些字段采用整型的方式去代替某些具體的含義,比如性別0代表男,1代表女。如果只是一些不會(huì)變更的轉(zhuǎn)譯我們可以采用常量或者枚舉類的方式來實(shí)現(xiàn),但是事實(shí)上我們也會(huì)遇到那種可能需要變更的,顯然這種場景下使用枚舉類這種方式是不合理的,那么如何動(dòng)態(tài)地去進(jìn)行轉(zhuǎn)譯呢?
正文
數(shù)據(jù)字典
數(shù)據(jù)字典(Data dictionary)是一種用戶可以訪問的記錄數(shù)據(jù)庫和應(yīng)用程序元數(shù)據(jù)的目錄。主動(dòng)數(shù)據(jù)字典是指在對數(shù)據(jù)庫或應(yīng)用程序結(jié)構(gòu)進(jìn)行修改時(shí),其內(nèi)容可以由DBMS自動(dòng)更新的數(shù)據(jù)字典。被動(dòng)數(shù)據(jù)字典是指修改時(shí)必須手工更新其內(nèi)容的數(shù)據(jù)字典。
我們通常會(huì)結(jié)合數(shù)據(jù)庫來實(shí)現(xiàn)數(shù)據(jù)字典,但事實(shí)上數(shù)據(jù)字典經(jīng)常會(huì)被使用到,如果頻繁地去訪問數(shù)據(jù)庫,將會(huì)對數(shù)據(jù)庫造成性能壓力,事實(shí)上我們經(jīng)常會(huì)采用Redis對數(shù)據(jù)字典進(jìn)行緩存來提升系統(tǒng)性能。
使用Redis的優(yōu)勢:
1.絕大數(shù)的請求操作都是純粹的內(nèi)存操作。
2.采用了單線模式,避免了不必要的上下文切換和競爭條件這里的單線程指的是網(wǎng)絡(luò)請求模塊只使用了一個(gè)線程(所以不必考慮并發(fā)安全性),即一個(gè)請求處理所有網(wǎng)絡(luò)請求,其他模塊仍使用了多個(gè)線程。
3.采用了動(dòng)態(tài)字符串(SDS),對于字符串會(huì)預(yù)留一定的空間,避免了字符串在做拼接和截取引起內(nèi)存重新分配導(dǎo)致性能的損耗。
SpringBoot+Redis實(shí)現(xiàn)數(shù)據(jù)字典
依賴
<!--redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.2</version> <optional>true</optional> </dependency>
application.properties:配置類
#redis spring.redis.host=127.0.0.1 spring.redis.port=6379
字典表:SYS_DICT
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for SYS_DICT -- ---------------------------- DROP TABLE IF EXISTS `SYS_DICT`; CREATE TABLE `SYS_DICT` ( `code` varchar(36) NOT NULL COMMENT '主鍵', `type_code` varchar(36) DEFAULT NULL COMMENT '類型code', `name` varchar(50) DEFAULT NULL COMMENT '展示值', `value` int(20) DEFAULT NULL COMMENT '使用值', `fixed` int(2) DEFAULT NULL COMMENT 'default 0不固定,固定的話用1', `creater` varchar(20) DEFAULT NULL COMMENT '新建人', `create_time` datetime DEFAULT NULL COMMENT '新建時(shí)間', `updater` varchar(20) DEFAULT NULL COMMENT '編輯人', `update_time` datetime DEFAULT NULL COMMENT '編輯時(shí)間', PRIMARY KEY (`code`), KEY `sys_type` (`type_code`), CONSTRAINT `sys_type` FOREIGN KEY (`type_code`) REFERENCES `SYS_DICT_TYPE` (`code`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of SYS_DICT -- ---------------------------- INSERT INTO `SYS_DICT` VALUES ('182d4db6-aa50-11ea-aa1b-00163e08c9ed', '9ed92c7e-aa4f-11ea-aa1b-00163e08c9ed', '男', '0', '1', null, null, null, null); INSERT INTO `SYS_DICT` VALUES ('222cf983-aa50-11ea-aa1b-00163e08c9ed', '9ed92c7e-aa4f-11ea-aa1b-00163e08c9ed', '女', '1', '1', null, null, null, null);
字典類型表SYS_DICT_TYPE
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for SYS_DICT_TYPE -- ---------------------------- DROP TABLE IF EXISTS `SYS_DICT_TYPE`; CREATE TABLE `SYS_DICT_TYPE` ( `code` varchar(36) NOT NULL, `name` varchar(50) DEFAULT NULL COMMENT '用于展示', `value` varchar(50) DEFAULT NULL COMMENT '用于前段(建立唯一索引)', `creater` varchar(20) DEFAULT NULL COMMENT '新建人', `create_time` datetime DEFAULT NULL COMMENT '新建時(shí)間', `updater` varchar(20) DEFAULT NULL COMMENT '編輯人', `updater_time` datetime DEFAULT NULL COMMENT '編輯時(shí)間', PRIMARY KEY (`code`), UNIQUE KEY `key_value` (`value`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of SYS_DICT_TYPE -- ---------------------------- INSERT INTO `SYS_DICT_TYPE` VALUES ('9ed92c7e-aa4f-11ea-aa1b-00163e08c9ed', '性別', 'sex', null, null, null, null);
RedisConfigurtion:Redis配置類,解決Redis數(shù)據(jù)同步時(shí)字符串格式問題
@Configuration public class RedisConfigurtion { @Autowired private RedisTemplate redisTemplate; @Bean public RedisTemplate<String, Object> stringSerializerRedisTemplate() { RedisSerializer<String> stringSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringSerializer); redisTemplate.setValueSerializer(stringSerializer); redisTemplate.setHashKeySerializer(stringSerializer); redisTemplate.setHashValueSerializer(stringSerializer); return redisTemplate; } }
SpringUtil:用于加載Spring容器
@Component public class SpringUtil implements ApplicationContextAware { private static ApplicationContext applicationContext = null; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if(SpringUtil.applicationContext == null){ SpringUtil.applicationContext = applicationContext; } } //獲取applicationContext public static ApplicationContext getApplicationContext() { return applicationContext; } //通過name獲取 Bean. public static Object getBean(String name){ return getApplicationContext().getBean(name); } //通過class獲取Bean. public static <T> T getBean(Class<T> clazz){ return getApplicationContext().getBean(clazz); } //通過name,以及Clazz返回指定的Bean public static <T> T getBean(String name,Class<T> clazz){ return getApplicationContext().getBean(name, clazz); } }
RedisDistUtil:Spring容器不允許普通Pojo調(diào)用Service,所以采用以下方式手動(dòng)通過Spring容器去加載Bean.
public class RedisDistUtil { private static ApplicationContext context; /** * 轉(zhuǎn)化碼值 * @param distname * @param value * @return * @throws Exception */ public static String transformStr(String distname, int value) { ApplicationContext context = SpringUtil.getApplicationContext(); ISysDictService iSysDictService =context.getBean(ISysDictService.class); return iSysDictService.transformStr(distname,value); } }
SysDictService
- transformStr:從Redis中獲取字典值。
- refreshCache:用于將數(shù)據(jù)庫數(shù)據(jù)字典表數(shù)據(jù)同步到Redis中。
@Transactional @Service @Slf4j public class SysDictService implements ISysDictService { @Autowired SysDictPojoMapper sysDictPojoMapper; @Autowired RedisTemplate redisTemplate; /** * 轉(zhuǎn)化碼值 * * @param distname * @param value * @return * @throws Exception */ @Override public String transformStr(String distname, int value) { return redisTemplate.opsForValue().get(distname + "_" + value) != null ? redisTemplate.opsForValue().get(distname + "_" + value).toString() : String.valueOf(value); } /** * 刷新緩存 */ @Override public void refreshCache() { log.info("start 刷新碼表緩存"); List<SysDictPojo> sysDictPojoList = sysDictPojoMapper.getall(); long startTime = System.currentTimeMillis(); for (SysDictPojo sysDictPojo : sysDictPojoList) { redisTemplate.opsForValue().set(sysDictPojo.getTypeCodeValue() + "_" + sysDictPojo.getValue(), sysDictPojo.getName()); } long endTime = System.currentTimeMillis(); log.info("end 刷新碼表緩存,總計(jì):" + sysDictPojoList.size() + "條,用時(shí):" + (endTime - startTime) + "毫秒"); } }
SysDictPojo:數(shù)據(jù)字典的實(shí)體類
@Setter @Getter @ToString public class SysDictPojo implements Serializable { private static final long serialVersionUID = 7845051152365224116L; private String code; private String typeCode; private String typeCodeValue; private String name; private Integer value; private Integer fixed; private String creater; private Date createTime; private String updater; private Date updateTime; }
getall:查詢數(shù)據(jù)庫的所有數(shù)據(jù)字典值
<select id="getall" resultType="com.luo.dao.entity.SysDictPojo"> select t1.name, t1.value, t2.value typeCodeValue from SYS_DICT t1 left join SYS_DICT_TYPE t2 on t2.code =t1.type_code </select>
驗(yàn)證
UserPojoRes :在get方法里進(jìn)行數(shù)據(jù)字典值的替換
@Setter @Getter @ToString public class UserPojoRes implements Serializable { private static final long serialVersionUID = -2145503717390503506L; /** * 主鍵 */ private String id; /** * 姓名 */ private String name; /** * 性別 */ private int sex; /** * 性別展示 */ private String sexStr; /** * 消息 */ private String msg; public String getSexStr() { return RedisDistUtils.transformStr("sex",this.sex); } }
訪問模擬接口,根據(jù)id查詢用戶信息:
到此這篇關(guān)于SpringBoot+Redis實(shí)現(xiàn)數(shù)據(jù)字典的方法的文章就介紹到這了,更多相關(guān)SpringBoot+Redis數(shù)據(jù)字典內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring boot2基于Mybatis實(shí)現(xiàn)多表關(guān)聯(lián)查詢
這篇文章主要介紹了Spring boot2基于Mybatis實(shí)現(xiàn)多表關(guān)聯(lián)查詢,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04form表單回寫技術(shù)java實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)form表單回寫技術(shù)的相關(guān)資料,需要的朋友可以參考下2016-04-04springboot整合gateway實(shí)現(xiàn)網(wǎng)關(guān)功能的示例代碼
本文主要介紹了springboot整合gateway實(shí)現(xiàn)網(wǎng)關(guān)功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02Java HttpURLConnection超時(shí)和IO異常處理
這篇文章主要介紹了Java HttpURLConnection超時(shí)和IO異常處理的相關(guān)資料,需要的朋友可以參考下2016-09-09springcloud+nacos實(shí)現(xiàn)灰度發(fā)布示例詳解
這篇文章主要介紹了springcloud+nacos實(shí)現(xiàn)灰度發(fā)布,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08