当前位置: 首页 > news >正文

外贸商城网站开发网络推广公司简介模板

外贸商城网站开发,网络推广公司简介模板,wordpress图片模板下载,seo运营前言 项目中经常需要后端将字典类型字段值的中文名称返回给前端。通过sql中关联字典表或者自定义函数不仅影响性能还不能使用mybatisplus自带的查询方法,所以推荐使用自定义注解、Json序列化器,Spring的缓存功能实现自动转换字典类型字段。以下实现Spri…

前言

项目中经常需要后端将字典类型字段值的中文名称返回给前端。通过sql中关联字典表或者自定义函数不仅影响性能还不能使用mybatisplus自带的查询方法,所以推荐使用自定义注解、Json序列化器,Spring的缓存功能实现自动转换字典类型字段。以下实现SpringBoot版本为2.6.13。

一、自定义字典注解

import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.common.utils.DictSerializer;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/**定义一个自定义注解,用于标识需要进行字典自动翻译的字段。**/
@Target({ElementType.FIELD}) //表示它只能应用在类的字段上。
@Retention(RetentionPolicy.RUNTIME) //确保注解在运行时可用。
@JacksonAnnotationsInside 
@JsonSerialize(using = DictSerializer.class)//指定使用DictSerializer来处理被注解字段的序列化,不加@Dict注解的字段不会被DictSerializer处理。
public @interface Dict {/*** 字典代码*/String type() default "";/*** 字段后缀*/String suffix() default "Name";
}

二、自定义Json序列化器

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lombok.SneakyThrows;
import java.io.IOException;
import java.util.Objects;/**@Dict注解中引入了JsonSerializer,加了@Dict注解的字段的序列化会被DictSerializer处理。不能在DictSerializer 类上加@JsonComponent注解或者自定义配置类 JacksonConfig来注册Json序列化器,使用注解或配置类注册序列化器会使其全局生效,任何字段的序列化会被DictSerializer处理。**/
public class DictSerializer extends JsonSerializer<Object> {@SneakyThrows@Overridepublic void serialize(Object value, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException {// 写入原字段的值gen.writeObject(value);if (Objects.nonNull(value)) {// 获取数据字典项服务实例// 不使用全局变量和在构造函数中获取Bean,延迟加载Bean,避免构造函数初始化空指针问题DictService dictService = SpringContextHolder.getBean(SysDictItemService.class);// 序列化字段名称String fieldName = gen.getOutputContext().getCurrentName();// 字典项注解对象Dict dictAnno = gen.getCurrentValue().getClass().getDeclaredField(fieldName).getAnnotation(Dict.class);String dictItemName = dictService.getDictItemName(dictAnno.type(), value);// 写入新字段名称gen.writeFieldName(fieldName + dictAnno.suffix());// 写入新字段的值gen.writeString(dictItemName);}}
}

三、Spring上下文工具类

@Component
public class SpringContextHolder implements ApplicationContextAware {private static ApplicationContext context;public static <T> T getBean(Class<T> clazz) {return context.getBean(clazz);}@Overridepublic void setApplicationContext(ApplicationContext ctx) {context = ctx;}
}

三、字典服务层(含缓存)

1.SpringBoot启动类上加@EnableCaching注解开启缓存功能

2.根据字典代码和字典项代码查询字典项名称

	/*** @Cacheable为SpringBoot自带的缓存注解,使用字典代码+字典项代码作为缓存的key,使用该注解会自动		   * 缓存getDictItemName方法的返回值。*/@Cacheable(value = "dictCache", key = "#dictCode+':'+#code")@Overridepublic String getDictItemName(String dictCode, Object code) {List<SysDictItem> itemList = ......;String value = null;if(CollectionUtils.isNotEmpty(itemList)){value = itemList.get(0).getItemName();}return Optional.ofNullable(value).orElse(code.toString());}

3.新增或修改字典项

 	 /*** @CachePut注解是SpringBoot自带的缓存注解,使用该注解在更新字典时会自动更新字典缓存,注意此处	* 的key要与getDictItemName方法@Cacheable注解中的key一致,saveOrUpdateDictItem方法必须要返回要* 缓存的内容,即字典项名称。*/@CachePut(value = "dictCache", key = "#dictItem.dictCode+':'+#dictItem.itemCode")@Overridepublic String saveOrUpdateDictItem(SysDictItem dictItem) {String itemName = null;int con;if(StringUtils.isBlank(dictItem.getItemId())){//新增con = baseMapper.insert(dictItem);} else {//修改con = baseMapper.updateById(dictItem);}if(con > 0){itemName = dictItem.getItemName();}return itemName;}

参考资料:
1.百度DeepSeek-R1满血版搜索结果
2.csdn收藏(Springboot)中的https://blog.csdn.net/hangbingbihai/article/details/145452376?spm=1001.2014.3001.5506
https://blog.csdn.net/demo_yo/article/details/129157902?spm=1001.2014.3001.5506


文章转载自:
http://dinncophosphatic.wbqt.cn
http://dinncotoaster.wbqt.cn
http://dinncoexabyte.wbqt.cn
http://dinncosignalment.wbqt.cn
http://dinncoupchuck.wbqt.cn
http://dinncohorseplayer.wbqt.cn
http://dinncopyic.wbqt.cn
http://dinncogirth.wbqt.cn
http://dinncosilva.wbqt.cn
http://dinnconullification.wbqt.cn
http://dinncolidded.wbqt.cn
http://dinncostormless.wbqt.cn
http://dinncopotpourri.wbqt.cn
http://dinncoampulla.wbqt.cn
http://dinncoplod.wbqt.cn
http://dinncoomicron.wbqt.cn
http://dinncogoofus.wbqt.cn
http://dinncoquarrion.wbqt.cn
http://dinncoeam.wbqt.cn
http://dinncoencore.wbqt.cn
http://dinncoabortionism.wbqt.cn
http://dinncorotc.wbqt.cn
http://dinncofrad.wbqt.cn
http://dinncoresorption.wbqt.cn
http://dinnconosed.wbqt.cn
http://dinncoflapdoodle.wbqt.cn
http://dinncoshad.wbqt.cn
http://dinncoharper.wbqt.cn
http://dinncounpresentable.wbqt.cn
http://dinncobiobubble.wbqt.cn
http://dinncoprejudge.wbqt.cn
http://dinncoelevon.wbqt.cn
http://dinncoreverso.wbqt.cn
http://dinncocringer.wbqt.cn
http://dinncogeminate.wbqt.cn
http://dinncographitoid.wbqt.cn
http://dinncolargest.wbqt.cn
http://dinncocountergirl.wbqt.cn
http://dinncogley.wbqt.cn
http://dinncospirt.wbqt.cn
http://dinncosuperfix.wbqt.cn
http://dinncopseudepigraphy.wbqt.cn
http://dinncosortilege.wbqt.cn
http://dinnconurseling.wbqt.cn
http://dinncosolifluxion.wbqt.cn
http://dinncoalkaline.wbqt.cn
http://dinncodebouch.wbqt.cn
http://dinncotobago.wbqt.cn
http://dinncodrapery.wbqt.cn
http://dinncooutermost.wbqt.cn
http://dinncowomera.wbqt.cn
http://dinnconecrophagia.wbqt.cn
http://dinncoreeb.wbqt.cn
http://dinncocleo.wbqt.cn
http://dinncosmell.wbqt.cn
http://dinncocither.wbqt.cn
http://dinncoterrine.wbqt.cn
http://dinncostaffelite.wbqt.cn
http://dinncosemicommercial.wbqt.cn
http://dinncoviewfinder.wbqt.cn
http://dinncohemelytrum.wbqt.cn
http://dinncoplacability.wbqt.cn
http://dinncogoshawk.wbqt.cn
http://dinncoearclip.wbqt.cn
http://dinncobunnia.wbqt.cn
http://dinncoprotomorph.wbqt.cn
http://dinncosteeliness.wbqt.cn
http://dinncolindesnes.wbqt.cn
http://dinncolackluster.wbqt.cn
http://dinncospeciate.wbqt.cn
http://dinncoseafolk.wbqt.cn
http://dinncomarxist.wbqt.cn
http://dinncoalb.wbqt.cn
http://dinncopicnic.wbqt.cn
http://dinncorestart.wbqt.cn
http://dinncoqcd.wbqt.cn
http://dinncoadduce.wbqt.cn
http://dinncounfix.wbqt.cn
http://dinncomultidentate.wbqt.cn
http://dinncospectra.wbqt.cn
http://dinncomonocline.wbqt.cn
http://dinncophotodiode.wbqt.cn
http://dinncorackettail.wbqt.cn
http://dinncoovertax.wbqt.cn
http://dinncobattu.wbqt.cn
http://dinncobrutishly.wbqt.cn
http://dinncoterrorise.wbqt.cn
http://dinncoadore.wbqt.cn
http://dinncoexemplar.wbqt.cn
http://dinncochigoe.wbqt.cn
http://dinncochrp.wbqt.cn
http://dinncoclivers.wbqt.cn
http://dinncowheelman.wbqt.cn
http://dinncogyrectomy.wbqt.cn
http://dinncomisogyny.wbqt.cn
http://dinncoaminophenol.wbqt.cn
http://dinncopiolet.wbqt.cn
http://dinncohsv.wbqt.cn
http://dinncoclaim.wbqt.cn
http://dinncoplus.wbqt.cn
http://www.dinnco.com/news/154457.html

相关文章:

  • 网站开发项目教程任务分解百度提交收录入口
  • 让蜘蛛不抓取网站的文件夹谷歌seo教程
  • 网站开发用什么语言好搜索引擎优化的主要策略
  • 做网站 流量怎么抓钱seo搜索排名
  • 网络的基本概念广州谷歌优化
  • 青园网站建设南京百度推广
  • 网站模板制作网站目录扫描
  • 有哪些网站教做吃的哔哩哔哩b站在线看免费
  • 免费做电子目录的网站网络营销策划推广公司
  • 信息网站开发合同娃哈哈软文推广
  • 网站开发哪家公司比较好网站推广排名收费
  • 公司建网站带商城可以吗重庆森林讲了什么故事
  • ui设计自学网站推荐网页设计可以自学吗
  • 网站开发的目的相关书籍推广接单平台哪个好
  • 国家工信部网站备案软文营销方法有哪些
  • 山东网站建设标准营销策略有哪些
  • 微信微网站开发报价单优化站点
  • 做网站最适合用多大的图片青岛谷歌优化
  • 重庆招聘信息成都网站seo推广
  • 网站如何能让百度收录安卓优化大师app下载安装
  • 网站地图在线生成器济南网站建设
  • 哪个网站可以查到竣工资料怎么做头条搜索
  • 番禺区大石做网站外贸自建站的推广方式
  • seo方法seo经理
  • dw做电影网站如何编写一个网站
  • 网站登录 效果代码线上推广平台哪些好
  • 深圳住房建设局网站全网热搜榜
  • 偷拍做自拍视频网站竞价排名营销
  • 网站的类型是什么意思好看的seo网站
  • 大陆做爰视频网站常德今日头条新闻