================================
©Copyright 蕃薯耀 2022-08-31
https://www.cnblogs.com/fanshuyao/
一、引入Maven依赖
基于<mybatisPlus.version>3.4.3.3</mybatisPlus.version>实现
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatisPlus.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>${mybatisPlusGenerator.version}</version> <scope>provided</scope> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>com.fhs-opensource</groupId> <artifactId>easy-trans-spring-boot-starter</artifactId> <version>2.0.2</version> <exclusions> <exclusion> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-annotation</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.fhs-opensource</groupId> <artifactId>easy-trans-mybatis-plus-extend</artifactId> <version>2.0.2</version> <exclusions> <exclusion> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-core</artifactId> </exclusion> <exclusion> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-extension</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.4</version> </dependency>
不要漏了:commons-beanutils
解决Mybatis-Plus版本不一致的注解冲突问题
Caused by: java.lang.ClassNotFoundException: com.baomidou.mybatisplus.annotation.OrderBy at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 101 common frames omitted
排除 mybatis-plus-annotation
<dependency> <groupId>com.fhs-opensource</groupId> <artifactId>easy-trans-spring-boot-starter</artifactId> <version>2.0.2</version> <exclusions> <exclusion> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-annotation</artifactId> </exclusion> </exclusions> </dependency>
二、application.properties 配置
1、Redis配置
spring.redis.host=192.168.11.210 spring.redis.port=6379 spring.redis.database=0 #spring.redis.password= spring.redis.timeout=3000
2、easy-trans配置
#启用redis缓存 如果不用redis请设置为false #如果不使用redis,请在启动类加禁用掉redis的自动配置类 #@SpringBootApplication(exclude = { RedisAutoConfiguration.class }) easy-trans.is-enable-redis: true #方式1:启用全局翻译(拦截所有responseBody进行自动翻译),如果对于性能要求很高可关闭此配置 #方式2:标记为 @TransMethodResult 的方法返回值会自动翻译,基于AOP实现,实现效果同全局翻译。 #方式3:翻译服务的核心是TransService 把此类@Autowired到你的controller或者service中, #调用 transOne/transBatch 方法可以翻译单个/多个 pojo,为了性能,TransService 不处理嵌套, #比如User里套了个Org org里面的数据不会被翻译。 easy-trans.is-enable-global: false #启用平铺模式 easy-trans.is-enable-tile: true #如果你项目框架使用map的子类包装vo(比如 ruoyi的ajaxResult类)请设置为true否则请不设置或者设置为false #easy-trans.is-enable-map-result: true
三、实体类继承和注解
1、实体类继承 TransPojo 接口
public class WarehousePrincipal implements Serializable,TransPojo
2、属性添加 @Trans 注解
@Trans(type = TransType.DICTIONARY,key = "warehouse_principal", ref = "typeName")
3、完整示例
@TableName("warehouse_principal") @ApiModel(value = "WarehousePrincipal对象", description = "") public class WarehousePrincipal implements Serializable,TransPojo { private static final long serialVersionUID = 1L; @ApiModelProperty("主键") @TableId(value = "id", type = IdType.AUTO) private Long id; @Trans(type = TransType.DICTIONARY,key = "warehouse_principal", ref = "typeName") @ApiModelProperty("负责人类型,默认为0,0:仓库管理员,1:仓库责任人") private Integer type; }
4、其它使用情形
//实现TransPojo 接口,代表这个类需要被翻译或者被当作翻译的数据源 public class Student implements TransPojo { // 字典翻译 ref为非必填 @Trans(type = TransType.DICTIONARY,key = "sex",ref = "sexName") private Integer sex; //这个字段可以不写,实现了TransPojo接口后有一个getTransMap方法,sexName可以让前端去transMap取 private String sexName; //SIMPLE 翻译,用于关联其他的表进行翻译 schoolName 为 School 的一个字段 @Trans(type = TransType.SIMPLE,target = School.class,fields = "schoolName") private String schoolId; //远程翻译,调用其他微服务的数据源进行翻译 @Trans(type = TransType.RPC,targetClassName = "com.fhs.test.pojo.School",fields = "schoolName",serviceName = "easyTrans",alias = "middle") private String middleSchoolId; // 枚举翻译,返回文科还是理科给前端 @Trans(type=TransType.ENUM,key = "desc") private StudentType studentType = StudentType.ARTS; public static enum StudentType{ ARTS("文科"), SCIENCES("理科"); private String desc; StudentType(String desc){ this.desc = desc; } } }
四、Controller方法添加 @TransMethodResult 注解
因为不是全局配置,所以在需要转换的方法增加注解。
@TransMethodResult @RequestMapping("/queryForPageVO") public Result queryForPage(@RequestBody Page<WarehousePrincipalVO> page, @RequestBody WarehousePrincipalVO params) { return Result.ok(warehousePrincipalService.queryForPageVO(page, params)); }
如果要开启全局注解,则需要在配置文件中修改配置项,但开启全局会影响性能。
#方式1:启用全局翻译(拦截所有responseBody进行自动翻译),如果对于性能要求很高可关闭此配置 #方式2:标记为 @TransMethodResult 的方法返回值会自动翻译,基于AOP实现,实现效果同全局翻译。 #方式3:翻译服务的核心是TransService 把此类@Autowired到你的controller或者service中, #调用 transOne/transBatch 方法可以翻译单个/多个 pojo,为了性能,TransService 不处理嵌套, #比如User里套了个Org org里面的数据不会被翻译。 easy-trans.is-enable-global: true
五、字典数据初始化
import java.util.Map; import javax.annotation.Resource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import com.fhs.trans.service.impl.DictionaryTransService; @Component public class TransJob { private static final Logger log = LoggerFactory.getLogger(TransJob.class); @Resource private SysDictService sysDictService; //注入字典翻译服务 @Resource private DictionaryTransService dictionaryTransService; @Scheduled(initialDelay = 3000, fixedRate = 5 * 60 * 1000) public void execute() { Long startTime = System.currentTimeMillis(); try { dictionaryTransService.makeUseRedis(); Map<String, Map<String, String>> datas = sysDictService.getDictMaps(); if(datas != null && !datas.isEmpty()) { for (String key : datas.keySet()) { dictionaryTransService.refreshCache(key, datas.get(key)); } } } catch (Exception e) { BizException.error("TransJob更新字典缓存失败", e); } Long endTime = System.currentTimeMillis(); log.info("TransJob更新字典缓存消耗时间为:{}", (endTime - startTime) + " ms"); } }
(时间宝贵,分享不易,捐赠回馈,^_^)
================================
©Copyright 蕃薯耀 2022-08-31
https://www.cnblogs.com/fanshuyao/
标签:翻译,Java,绑定,redis,private,easy,trans,com,字典 From: https://www.cnblogs.com/fanshuyao/p/16643674.html