关联字典表key转value

说明:
数据表中有多个字段存的是字典表中的key,显示的时候需要转换成value显示

第一种: 在返回字段关联
在这里插入图片描述

第二种: 关联表查询
在这里插入图片描述


自定义类继承BaseTypeHandler实现方法,进行转换

mapper写法

MultiDictCacheTypeHandler : 为自定义转换类

<result column="YHMMGZ" property="yhmmgz" jdbcType="VARCHAR"/>
<result column="YHMMGZ_NAME" property="yhmmgzName" typeHandler="com.test.base.repository.columnHandler.MultiDictCacheTypeHandler"/>

自定义转换类,自动将字典key,转换成value

package com.hisign.base.repository.columnHandler;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MultiDictCacheTypeHandler extends BaseTypeHandler<String> {

    public MultiDictCacheTypeHandler() {
    }

    public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, parameter);
    }

    public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String rootDictKey = rs.getString(columnName);
        String value = this.getDictValueFromCache(rootDictKey);
        return value;
    }

    public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String rootDictKey = rs.getString(columnIndex);
        String value = this.getDictValueFromCache(rootDictKey);
        return value;
    }

    public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String rootDictKey = cs.getString(columnIndex);
        String value = this.getDictValueFromCache(rootDictKey);
        return value;
    }

    private String getDictValueFromCache(String rootDictKey) {
        if (StringUtils.isEmpty(rootDictKey)) {
            return null;
        } else {
        //系统id,可在启动项目时候设置进行,获取当前系统的字典缓存 redis中存的是hash格式
            String systemId = System.getProperty("system");
            if (StringUtils.isEmpty(systemId)) {
                systemId = "UNKOWN";
            }
            String dictValue = this.getDictValueFromCache(systemId, rootDictKey);
            if (StringUtils.isEmpty(dictValue)) {
                dictValue = this.getDictValueFromCache("system", rootDictKey);
            }
            return dictValue;
        }
    }
//字典缓存  hash :  系统代码, rootKey, key   ,通过hash的key, key获取值
    private String getDictValueFromCache(String systemId, String rootDictKey) {
        DictCache instance = DictCache.getInstance();
        String value = instance.getZdxxByXtdmAndZddm(systemId, rootDictKey);
        return value;
    }
}

根据系统代码, key,获取字典值

public class DictCache {

    /**
     * 字典缓存  hash :  xtdm , gzdxj , zdxx
     */
    private Map<String, Map<String, Zdxx>> dictMap = new ConcurrentHashMap<String, Map<String, Zdxx>>();

    private static DictCache instance = null;

    public DictCache() {
    }

    public static DictCache getInstance() {
        if (instance == null) {
            instance = new DictCache();
        }
        return instance;
    }

    public DictCache(Map<String, Map<String, Zdxx>> dictMap) {
        this.dictMap = dictMap;
    }


    /**
     * 获取字典集合 通过系统代码 和 字典代码
     *
     * @param xtdm    系统代码
     * @param rootKey 字典代码
     * @return
     */
    public String getZdxxByXtdmAndZddm(String xtdm, String rootKey) {

        RedisTemplate redisTemplate = (RedisTemplate) ApplicationContextUtils.getBean("redisTemplate");

        if (xtdm.length() == 0 || rootKey.length() == 0) {
            throw new HisignException("系统代码 和 字典代码 不能为空!");
        }

        String[] split = rootKey.split(",");
        StringBuilder builder = new StringBuilder();
        String zddm = split[0];
        //从第二位开始循环是key,第一位是rootkey
        for (int i = 1; i < split.length; i++) {
            String key = split[i];
            Object result = redisTemplate.opsForHash().get(BaseFrameConstant.DICT_PREFIX + xtdm, zddm);
            String str = JSON.toJSONString(result, SerializerFeature.WriteMapNullValue);
            Zdxx redis_zdxx = JSONObject.parseObject(str, Zdxx.class);
            if (redis_zdxx != null) {
                List<Zdxx> childList = redis_zdxx.getChildList();
                if (childList.size() > 0) {
                    for (Zdxx zdxx : childList) {
                        String zdxj = zdxx.getZdxj();
                        if (zdxj.equals(key)) {
                            if (builder.length() > 0) {
                                builder.append(",");
                            }
                            builder.append(zdxx.getZdz1());
                        }
                    }
                }
            }
        }
        return builder.toString();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值