Easy_Trans can easily reduce the amount of SQL code in your project by 30%

What is Easy_Trans

Easy Trans is a code-assisted plug-in for data translation. It uses the capabilities of ORM frameworks such as MyBatis Plus/JPA/BeetlSQL to automatically look up tables, allowing developers to quickly translate ID/dictionary codes into data that needs to be displayed on the front end.

Advantages of easy trans

Versatile

  1. cache support

  2. Cross-microservice translation support (User and Order are two different microservices, order contains userId and requires userName)

  3. International support

  4. Multiple ORM framework adaptation

  5. Multiple data source support

  6. Collection support (userIds [1,2,3] translated as Zhang San, Li Si, Wang Wu)

  7. Reverse translation supports male->gender 0 Zhang San->user id id Shaanxi Branch Finance Department-> org_id 1

Applicable to various scenarios

  1. I have an ID but need to show his title/name to the client but don't want to manually do a table join query.

  2. I have a dictionary code "sex" and the corresponding dictionary value 0, and I want to translate it to "male" so that it can be displayed to customers.

  3. I have a set of user IDs, such as 1, 2, and 3, and I want to display them as "Zhang San", "Li Si", "Wang Wu" for customers to view.

  4. My enum contains a "title" field and I want to display the value of this field to the frontend for the client to view.

  5. I have a unique key (e.g. mobile phone number, ID number) but need to show the customer their title/name without manually performing a table join query.

Simple configuration

For the springboot project, you only need to introduce the easy-trans starter

<!-- 目前最新版本就是 2.2.1-M1 -->
<dependency>
    <groupId>com.fhs-opensource</groupId>
    <artifactId>easy-trans-spring-boot-starter</artifactId>
    <version>2.2.1-M1</version>
</dependency>

Then introduce the extension of the corresponding ORM framework in the project, taking mybatisPlus as an example

<dependency>
    <groupId>com.fhs-opensource</groupId>
    <artifactId>easy-trans-mybatis-plus-extend</artifactId>
    <version>2.2.1-M1</version>
</dependency>

Then yml configuration

# 翻译配置
easy-trans:
  # 平铺模式
  is-enable-tile: true
  # 开启redis支持
  is-enable-redis: true
  # 开启responseBody 全局拦截后自动翻译
  is-enable-global: true
  # 字典缓存是否放到redis中 做二级缓存,微服务模式推荐开启
  dict-use-redis: true
  db-type: mysql

Flexible and easy to use

easy trans supports five types

1. Dictionary translation (TransType.DICTIONARY)

The user needs to refresh the dictionary information into DictionaryTransService for caching. When using dictionary translation, the cached data source is obtained.

2. Simple translation (TransType.SIMPLE)

For example, if userId requires userName or userPo to be given to the front end, the principle is that the component uses the API of MybatisPlus/JPA to automatically query and put the results in transMap.

3. Cross-microservice translation (TransType.RPC)

For example, order and user are two microservices, but if I want to display the username of the order creator in the order details, I need to use RP translation. The principle is that the order microservice uses restTemplate to call a unified interface of the user service, and the needs are The translated ID is passed in, and then the user microservice uses the API of MybatisPlus/JPA to automatically query and send the results to the order microservice, and then the order microservice gets the data and translates it. Of course, the user only needs an annotation. These things are all done by Components are done automatically.

4、AutoTrans(TransType.AUTO)

It is still the id translation name scenario, but if the user wants the component to call a method written by himself without performing data query through the API of Mybatis Plus/JPA, he can use AutoTrans

5. Enumeration translation (TransType.ENUM)

For example, if I want to translate SEX.BOY into male, I can use enumeration to translate it.

Refer to the official website for details:

Detailed explanation of @Trans annotation · EasyTrans official documentation · Kanyun

Guess you like

Origin blog.csdn.net/WXF_Sir/article/details/131241299