SpringBoot+Redis實現(xiàn)數(shù)據(jù)字典的方法
前言
我們在日常的開發(fā)過程中針對一些字段采用整型的方式去代替某些具體的含義,比如性別0代表男,1代表女。如果只是一些不會變更的轉(zhuǎn)譯我們可以采用常量或者枚舉類的方式來實現(xiàn),但是事實上我們也會遇到那種可能需要變更的,顯然這種場景下使用枚舉類這種方式是不合理的,那么如何動態(tài)地去進(jìn)行轉(zhuǎn)譯呢?
正文
數(shù)據(jù)字典
數(shù)據(jù)字典(Data dictionary)是一種用戶可以訪問的記錄數(shù)據(jù)庫和應(yīng)用程序元數(shù)據(jù)的目錄。主動數(shù)據(jù)字典是指在對數(shù)據(jù)庫或應(yīng)用程序結(jié)構(gòu)進(jìn)行修改時,其內(nèi)容可以由DBMS自動更新的數(shù)據(jù)字典。被動數(shù)據(jù)字典是指修改時必須手工更新其內(nèi)容的數(shù)據(jù)字典。
我們通常會結(jié)合數(shù)據(jù)庫來實現(xiàn)數(shù)據(jù)字典,但事實上數(shù)據(jù)字典經(jīng)常會被使用到,如果頻繁地去訪問數(shù)據(jù)庫,將會對數(shù)據(jù)庫造成性能壓力,事實上我們經(jīng)常會采用Redis對數(shù)據(jù)字典進(jìn)行緩存來提升系統(tǒng)性能。
使用Redis的優(yōu)勢:
1.絕大數(shù)的請求操作都是純粹的內(nèi)存操作。
2.采用了單線模式,避免了不必要的上下文切換和競爭條件這里的單線程指的是網(wǎng)絡(luò)請求模塊只使用了一個線程(所以不必考慮并發(fā)安全性),即一個請求處理所有網(wǎng)絡(luò)請求,其他模塊仍使用了多個線程。
3.采用了動態(tài)字符串(SDS),對于字符串會預(yù)留一定的空間,避免了字符串在做拼接和截取引起內(nèi)存重新分配導(dǎo)致性能的損耗。
SpringBoot+Redis實現(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 '新建時間',
`updater` varchar(20) DEFAULT NULL COMMENT '編輯人',
`update_time` datetime DEFAULT NULL COMMENT '編輯時間',
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 '新建時間',
`updater` varchar(20) DEFAULT NULL COMMENT '編輯人',
`updater_time` datetime DEFAULT NULL COMMENT '編輯時間',
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ù)同步時字符串格式問題
@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,所以采用以下方式手動通過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 刷新碼表緩存,總計:" + sysDictPojoList.size() + "條,用時:" + (endTime - startTime) + "毫秒");
}
}
SysDictPojo:數(shù)據(jù)字典的實體類
@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>
驗證
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實現(xiàn)數(shù)據(jù)字典的方法的文章就介紹到這了,更多相關(guān)SpringBoot+Redis數(shù)據(jù)字典內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
簡單剖析Java中動態(tài)線程池的擴(kuò)容以及縮容操作
這篇文章主要為大家詳細(xì)介紹了Java中動態(tài)線程池的擴(kuò)容以及縮容操作的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-01-01
解決Spring Security中AuthenticationEntryPoint不生效相關(guān)問題
這篇文章主要介紹了解決Spring Security中AuthenticationEntryPoint不生效相關(guān)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
Struts2 通過ognl表達(dá)式實現(xiàn)投影
這篇文章主要介紹了Struts2 通過ognl表達(dá)式實現(xiàn)投影,具有一定參考價值,需要的朋友可以了解下。2017-09-09
spring-data-jpa中findOne與getOne的區(qū)別說明
這篇文章主要介紹了spring-data-jpa中findOne與getOne的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11

