AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

一招教你解決頁面中關聯id的轉換

來源: 責編: 時間:2024-05-17 09:02:59 152觀看
導讀在工作中,我們經常有這樣的業務情況,實體間通過id實現數據業務上的關聯,比如訂單和用戶,訂單的創建人id、商品id等,在頁面查詢時我們需要將對應的id信息轉換成對應的中文描述,比如用戶中文名稱,商品中文名稱等。如果是單條數

在工作中,我們經常有這樣的業務情況,實體間通過id實現數據業務上的關聯,比如訂單和用戶,訂單的創建人id、商品id等,在頁面查詢時我們需要將對應的id信息轉換成對應的中文描述,比如用戶中文名稱,商品中文名稱等。如果是單條數據的展示還好,但是設計到列表查詢,如何高效、優雅地實現這個效果呢?OQR28資訊網——每日最新資訊28at.com

現在接口返回的數據基本都是JSON格式,比如spring中使用了jackson,在controller層對結果進行json序列化,而我們要做的就是在序列化的過程中,實現id的轉換OQR28資訊網——每日最新資訊28at.com

使用方式

定義實體轉換接口

由于需要對訂單實體中的創建人id進行轉換OQR28資訊網——每日最新資訊28at.com

public interface UserConvert {    String USER_CACHE = "USER_CACHE";    String userId();    default ConvertItem getUserConvert(){        if( userId() == null ){            return null;        }        return new ConvertItem(userId(), USER_CACHE);    }}

定義接口轉換適配器

基于上面UserConvert的處理,基于緩存實現,同時支持一個實體中多個,比如商品名稱、商品分類等OQR28資訊網——每日最新資訊28at.com

public class UserConvertProvider extends CacheItemConvertAdapter {    private static String name = UserConvert.USER_CACHE;    public UserConvertProvider() {        super(name, User.class);    }    @Override    public boolean support(ConvertItem convertItem) {        return convertItem != null && convertItem.getName().equals(name);    }    @Override    public String convert(ConvertItem convertItem) {        if( convertItem == null ){            return null;        }        User user = (User) fromCache(convertItem.getId());        return user != null ? user.getCaption() : null;    }}

需要轉換的數據緩存

該實現依賴緩存,需要優先對需要轉換的數據進行緩存,因此示例中添加了緩存示例OQR28資訊網——每日最新資訊28at.com

public void init(){    Cache cache = cacheManager.getCache(UserConvert.USER_CACHE);    if( cache != null ){        cache.put("u1", new User("u1","Tom"));    }}

實體定義

實體中需要通過實現接口UserConvert,這樣對多個數據項轉換時可以繼續擴展OQR28資訊網——每日最新資訊28at.com

public class Order implements UserConvert {    private String id;    private String name;    private LocalDateTime createTime = LocalDateTime.now();    /**     * 創建用戶     */    private String creator;    @Override    public String userId() {        return creator;    }}

實現效果

可以看到,在輸出json中,多了一列userConvert,也就是接口中定義的get*方法OQR28資訊網——每日最新資訊28at.com

{    "id": "1",    "name": "測試訂單",    "createTime": "2024-05-08T21:55:51.5747507",    "creator": "u1",    "userConvert": "Tom"}

實現原理

上面說的,主要實現基于緩存,在web查詢結果進行json序列化時,依賴于jackson的擴展,對輸出結果匹配的類型進行轉換。OQR28資訊網——每日最新資訊28at.com

@EnableCaching@Configurationpublic class JacksonCustomConfiguration{    @Bean    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(){        return jacksonObjectMapperBuilder -> configureMapperBuilder(jacksonObjectMapperBuilder);    }    private void configureMapperBuilder(Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder) {        jackson2ObjectMapperBuilder.serializers(convertSerializer());    }    @Bean    public ItemConvertSerializer convertSerializer(){        return new ItemConvertSerializer(ConvertItem.class);    }}
  1. 在配置文件中基于Jackson2ObjectMapperBuilderCustomizer對jackson進行擴展
  2. 定義ItemConvertSerializer對ConvertItem類型的屬性進行處理,該類主要繼承于StdSerializer
  3. 在ItemConvertSerializer中基于ConvertItem的name屬性來匹配對應的緩存并進行轉換
  4. 注意開啟spring緩存*@EnableCaching*
  5. 最后基于spring特性,定義*/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports*來實現自動注入配置
  • ConvertItem示例
@Getter@Setterpublic class ConvertItem {    private String id;    private String text;    private String name;    public ConvertItem() {    }    public ConvertItem(String id, String name) {        this.id = id;        this.name = name;    }}
  • ItemConvertAdapter擴展適配器,主要于ConvertItem搭配擴展
public interface ItemConvertAdapter {    /**     * @param convertItem     * @return     */    boolean support(ConvertItem convertItem);    /**     *     * @param convertItem     * @return     */    String convert(ConvertItem convertItem);}
  • ItemConvertSerializer示例
public class ItemConvertSerializer extends StdSerializer<ConvertItem> implements ApplicationContextAware {    private List<ItemConvertAdapter> itemConvertAdapters;    public ItemConvertSerializer(Class<ConvertItem> t) {        super(t);    }    @Override    public void serialize(ConvertItem value, JsonGenerator gen, SerializerProvider provider) throws IOException {        String text = "";        if(!CollectionUtils.isEmpty(itemConvertAdapters)){            for (ItemConvertAdapter itemConvertAdapter : itemConvertAdapters) {                if( itemConvertAdapter.support(value) ){                    text = itemConvertAdapter.convert(value);                    break;                }            }        }        gen.writeString(text);    }    @Override    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {        Map<String, ItemConvertAdapter> itemConvertAdapterMap                = BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ItemConvertAdapter.class, true, false);        if( !itemConvertAdapterMap.isEmpty() ){            itemConvertAdapters = new ArrayList<>(itemConvertAdapterMap.values());            itemConvertAdapters.sort(OrderComparator.INSTANCE);        }    }}

優缺點

  • 使用了jackson序列化的擴展,如果使用其他序列化工具,需要單獨支持。
  • 依賴于數據緩存,一般針對通用數據才有數據轉換的需要,比如用戶、部門數據等,一般這些數據更適合緩存。

本文鏈接:http://www.tebozhan.com/showinfo-26-88739-0.html一招教你解決頁面中關聯id的轉換

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: React 中的國際化優秀實踐

下一篇: 怪不得這么多人學 React!

標簽:
  • 熱門焦點
Top