-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
luozhan
committed
Apr 27, 2020
1 parent
3a274f9
commit ca22316
Showing
11 changed files
with
149 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
# 翻译工具Translator | ||
批量数据字典翻译,字典数据源支持静态字典表 | ||
业务中有没有遇到查询结果中经常需要进行数据字典翻译的情况,如果还在用连表查询或者for循环处理,可以试试这个! | ||
## 快速开始 | ||
用一个简单的Demo来阐述Translator的功能: | ||
现有一张`student`表,其结构如下: | ||
|
||
### 1. 在具有数据字典功能的Entity类上标识注解@Dictionary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/com/robot/translator/core/TranslateAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.robot.translator.core; | ||
|
||
import org.aspectj.lang.JoinPoint; | ||
import org.aspectj.lang.annotation.AfterReturning; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Pointcut; | ||
import org.aspectj.lang.reflect.MethodSignature; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* 字典翻译 | ||
* 在方法上定义@Tanslator注解,对方法返回值进行翻译 | ||
* | ||
* @author luozhan | ||
* @create 2020-04 | ||
* @see Translator | ||
*/ | ||
@Aspect | ||
@Component | ||
public class TranslateAspect { | ||
@Pointcut("@annotation(com.robot.translator.core.annotation.Translator)") | ||
public void pointCut() { | ||
} | ||
|
||
@AfterReturning(pointcut = "pointCut()", returning = "object") | ||
public void doAfter(JoinPoint joinPoint, Object object) { | ||
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); | ||
com.robot.translator.core.annotation.Translator config = signature.getMethod().getAnnotation(com.robot.translator.core.annotation.Translator.class); | ||
Translator.parse(object, config.value()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.robot.translator.dict; | ||
|
||
import com.robot.translator.core.dict.IDict; | ||
|
||
/** | ||
* 枚举值常量 | ||
* 真实应用中,可以将同一个业务的字典枚举放在一个接口中方便维护 | ||
* | ||
* @author luozhan | ||
* @date 2019-03 | ||
*/ | ||
|
||
public interface MyDict { | ||
/** | ||
* 示例1:性别枚举 | ||
*/ | ||
enum SexDict implements IDict { | ||
MALE("0", "男"), | ||
FEMALE("1", "女"); | ||
|
||
SexDict(String code, String text) { | ||
// 构造方法中只需要调用接口的init方法即可,省略了属性的定义和赋值,也不用定义累赘的get方法 | ||
init(code, text); | ||
} | ||
} | ||
|
||
/** | ||
* 示例2:是、否枚举 | ||
*/ | ||
enum YesNoDict implements IDict { | ||
YES("1", "是"), | ||
NO("0", "否"); | ||
|
||
YesNoDict(String code, String text) { | ||
init(code, text); | ||
} | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
package com.robot.translator.entity; | ||
|
||
import com.robot.translator.core.annotation.Dictionary; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* (Class)班级 | ||
* | ||
* @author [email protected] | ||
* @since 2020-04-27 10:25:42 | ||
*/ | ||
@Data | ||
@NoArgsConstructor | ||
@Dictionary(codeColumn = "id", textColumn = "name") | ||
public class Class implements Serializable { | ||
private static final long serialVersionUID = -40477702922137712L; | ||
|
@@ -22,21 +24,4 @@ public class Class implements Serializable { | |
*/ | ||
private String name; | ||
|
||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package com.robot.translator.entity; | ||
|
||
import com.robot.translator.core.annotation.Dictionary; | ||
import lombok.Data; | ||
|
||
import java.io.Serializable; | ||
|
||
|
@@ -10,6 +11,7 @@ | |
* @author [email protected] | ||
* @since 2020-04-27 10:25:42 | ||
*/ | ||
@Data | ||
@Dictionary(codeColumn = "dict_code", textColumn = "dict_text", groupColumn = "group_code") | ||
public class StaticDict implements Serializable { | ||
private static final long serialVersionUID = 837652642519567677L; | ||
|
@@ -31,36 +33,5 @@ public class StaticDict implements Serializable { | |
private String dictText; | ||
|
||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getGroupCode() { | ||
return groupCode; | ||
} | ||
|
||
public void setGroupCode(String groupCode) { | ||
this.groupCode = groupCode; | ||
} | ||
|
||
public String getDictCode() { | ||
return dictCode; | ||
} | ||
|
||
public void setDictCode(String dictCode) { | ||
this.dictCode = dictCode; | ||
} | ||
|
||
public String getDictText() { | ||
return dictText; | ||
} | ||
|
||
public void setDictText(String dictText) { | ||
this.dictText = dictText; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
package com.robot.translator.entity; | ||
|
||
import com.robot.translator.core.annotation.Dictionary; | ||
import com.robot.translator.core.annotation.Translate; | ||
import com.robot.translator.dict.MyDict; | ||
import com.robot.translator.translator.MyAgeTranslator; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* (Student)学生 | ||
* | ||
* @author [email protected] | ||
* @since 2020-04-27 10:24:38 | ||
*/ | ||
@Data | ||
@NoArgsConstructor | ||
|
@@ -34,13 +34,20 @@ public class Student implements Serializable { | |
/** | ||
* 性别 | ||
*/ | ||
@Translate(dictClass = StaticDict.class, groupValue = "sex") | ||
// 静态字典翻译和枚举翻译,都可以支持 | ||
// @Translate(dictClass = StaticDict.class, groupValue = "sex") | ||
@Translate(MyDict.SexDict.class) | ||
private String sex; | ||
private String sexName; | ||
/** | ||
* 年龄 | ||
*/ | ||
@Translate(dictionary = @Dictionary(translator = MyAgeTranslator.class), translateField = "tag") | ||
private Integer age; | ||
/** | ||
* 年龄标签,由年龄决定 | ||
*/ | ||
private String tag; | ||
|
||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/test/java/com/robot/translator/translator/MyAgeTranslator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.robot.translator.translator; | ||
|
||
import com.robot.translator.core.annotation.Dictionary; | ||
import com.robot.translator.core.translator.Translatable; | ||
|
||
/** | ||
* 年龄标签翻译,供示例 | ||
* | ||
* @author luozhan | ||
* @create 2020-04 | ||
*/ | ||
public class MyAgeTranslator implements Translatable { | ||
@Override | ||
public String translate(String groupValue, String origin, Dictionary dicConfig, Class dictClass) { | ||
int age = Integer.parseInt(origin); | ||
if (age < 10) { | ||
return "小孩"; | ||
} else if (age >= 10 && age < 20) { | ||
return "少年"; | ||
} else { | ||
return "壮年"; | ||
} | ||
} | ||
} |