溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringBoot+Redis如何實現(xiàn)數(shù)據(jù)字典

發(fā)布時間:2020-10-27 16:31:56 來源:億速云 閱讀:454 作者:Leah 欄目:開發(fā)技術(shù)

SpringBoot+Redis如何實現(xiàn)數(shù)據(jù)字典?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

前言

我們在日常的開發(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 &#63;
        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查詢用戶信息:

SpringBoot+Redis如何實現(xiàn)數(shù)據(jù)字典

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI