From ca223169f3ce79c243a285a7d6d5ae73350edc29 Mon Sep 17 00:00:00 2001 From: luozhan Date: Mon, 27 Apr 2020 19:05:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0aop=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E5=92=8C=E5=AE=8C=E5=96=84=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 +++- pom.xml | 10 ++++- .../translator/core/TranslateAspect.java | 31 +++++++++++++++ .../com/robot/translator/core/Translator.java | 6 +-- .../java/com/robot/translator/SampleTest.java | 25 ++++++++++-- .../com/robot/translator/dict/MyDict.java | 39 +++++++++++++++++++ .../com/robot/translator/dict/ResDict.java | 31 --------------- .../com/robot/translator/entity/Class.java | 23 ++--------- .../robot/translator/entity/StaticDict.java | 33 +--------------- .../com/robot/translator/entity/Student.java | 15 +++++-- .../translator/MyAgeTranslator.java | 24 ++++++++++++ 11 files changed, 149 insertions(+), 95 deletions(-) create mode 100644 src/main/java/com/robot/translator/core/TranslateAspect.java create mode 100644 src/test/java/com/robot/translator/dict/MyDict.java delete mode 100644 src/test/java/com/robot/translator/dict/ResDict.java create mode 100644 src/test/java/com/robot/translator/translator/MyAgeTranslator.java diff --git a/README.md b/README.md index 2949fea..9d19498 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,7 @@ # 翻译工具Translator -批量数据字典翻译,字典数据源支持静态字典表 +业务中有没有遇到查询结果中经常需要进行数据字典翻译的情况,如果还在用连表查询或者for循环处理,可以试试这个! +## 快速开始 +用一个简单的Demo来阐述Translator的功能: +现有一张`student`表,其结构如下: + +### 1. 在具有数据字典功能的Entity类上标识注解@Dictionary diff --git a/pom.xml b/pom.xml index de06082..1cb0092 100644 --- a/pom.xml +++ b/pom.xml @@ -6,13 +6,13 @@ org.springframework.boot spring-boot-starter-parent 2.2.6.RELEASE - + com.robot translator 1.0.0-SNAPSHOT translator - Demo project for Spring Boot + Translator with Springboot 1.8 @@ -23,14 +23,20 @@ org.springframework.boot spring-boot-starter + + org.springframework.boot + spring-boot-starter-aop + org.projectlombok lombok + test mysql mysql-connector-java 8.0.11 + test org.springframework.boot diff --git a/src/main/java/com/robot/translator/core/TranslateAspect.java b/src/main/java/com/robot/translator/core/TranslateAspect.java new file mode 100644 index 0000000..0ce7265 --- /dev/null +++ b/src/main/java/com/robot/translator/core/TranslateAspect.java @@ -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()); + } +} diff --git a/src/main/java/com/robot/translator/core/Translator.java b/src/main/java/com/robot/translator/core/Translator.java index bbde34c..f8d1350 100644 --- a/src/main/java/com/robot/translator/core/Translator.java +++ b/src/main/java/com/robot/translator/core/Translator.java @@ -84,9 +84,9 @@ public static List parse(List origins, Class... classes) { Translate translateConfig = field.getAnnotation(Translate.class); // 配置的字典class Class dictClass = getDictClass(translateConfig); - if (dictClass == void.class) { - throw new IllegalArgumentException(String.format("属性名%s上的@Translate注解找不到dictClass或value配置", fieldName)); - } + // if (dictClass == void.class) { + // throw new IllegalArgumentException(String.format("属性名%s上的@Translate注解找不到dictClass或value配置", fieldName)); + // } // 获取翻译值写入的字段名 String translateField = StringUtil.parseCamelTo(getTranslateFieldName(translateConfig, field.getName()), fieldFormatType); diff --git a/src/test/java/com/robot/translator/SampleTest.java b/src/test/java/com/robot/translator/SampleTest.java index 786a6f5..32ba29f 100644 --- a/src/test/java/com/robot/translator/SampleTest.java +++ b/src/test/java/com/robot/translator/SampleTest.java @@ -21,8 +21,9 @@ public class SampleTest { JdbcTemplate template; @Test - public void testSelect() { - // List翻译 + public void testTranslate() { + + // 1.List翻译 List> maps = template.queryForList("select * from student"); Translator.parse(maps, Student.class); maps.forEach(System.out::println); @@ -30,7 +31,7 @@ public void testSelect() { // {id=2, name=李四, class_id=3, sex=1, age=20, class_name=三年三班, sex_name=女} // {id=3, name=周杰伦, class_id=2, sex=0, age=38, class_name=三年二班, sex_name=男} - // List翻译 + // 2.List翻译 List students = template.query("select * from student", new BeanPropertyRowMapper<>(Student.class)); Translator.parse(students); students.forEach(System.out::println); @@ -38,11 +39,27 @@ public void testSelect() { // Student(id=2, name=李四, classId=3, className=三年三班, sex=1, sexName=女, age=20) // Student(id=3, name=周杰伦, classId=2, className=三年二班, sex=0, sexName=男, age=38) - // 单个翻译 + // 3.单个Entity翻译 Student student = template.queryForObject("select * from student where id=3", new BeanPropertyRowMapper<>(Student.class)); Translator.parse(student); System.out.println(student); + // 4.单个Map翻译 + Map studentMap = template.queryForMap("select * from student where id=3"); + Translator.parse(studentMap, Student.class); + System.out.println(studentMap); + + + } + + @com.robot.translator.core.annotation.Translator + public List queryAllStudents() { + return template.query("select * from student", new BeanPropertyRowMapper<>(Student.class)); + } + @Test + public void testAop() { + // + } } \ No newline at end of file diff --git a/src/test/java/com/robot/translator/dict/MyDict.java b/src/test/java/com/robot/translator/dict/MyDict.java new file mode 100644 index 0000000..a77a184 --- /dev/null +++ b/src/test/java/com/robot/translator/dict/MyDict.java @@ -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); + } + } + +} diff --git a/src/test/java/com/robot/translator/dict/ResDict.java b/src/test/java/com/robot/translator/dict/ResDict.java deleted file mode 100644 index 6377305..0000000 --- a/src/test/java/com/robot/translator/dict/ResDict.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.robot.translator.dict; - -import com.robot.translator.core.dict.IDict; - -/** - * 资源枚举值定义 - * - * @author luozhan - * @date 2019-03 - */ - -public interface ResDict { - /** - * "是/否"枚举 - */ - enum YesOrNoDict implements IDict { - /** - * 是 - */ - YES("1", "是"), - /** - * 否 - */ - NO("0", "否"); - - YesOrNoDict(String code, String text) { - init(code, text); - } - } - -} diff --git a/src/test/java/com/robot/translator/entity/Class.java b/src/test/java/com/robot/translator/entity/Class.java index cd71e7f..967f936 100644 --- a/src/test/java/com/robot/translator/entity/Class.java +++ b/src/test/java/com/robot/translator/entity/Class.java @@ -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 luozhan@asiainfo.com - * @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; - } - } \ No newline at end of file diff --git a/src/test/java/com/robot/translator/entity/StaticDict.java b/src/test/java/com/robot/translator/entity/StaticDict.java index 957af47..73c220e 100644 --- a/src/test/java/com/robot/translator/entity/StaticDict.java +++ b/src/test/java/com/robot/translator/entity/StaticDict.java @@ -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 luozhan@asiainfo.com * @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; - } } \ No newline at end of file diff --git a/src/test/java/com/robot/translator/entity/Student.java b/src/test/java/com/robot/translator/entity/Student.java index 0401c6a..ba37d9e 100644 --- a/src/test/java/com/robot/translator/entity/Student.java +++ b/src/test/java/com/robot/translator/entity/Student.java @@ -1,6 +1,9 @@ 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; @@ -8,9 +11,6 @@ /** * (Student)学生 - * - * @author luozhan@asiainfo.com - * @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; } \ No newline at end of file diff --git a/src/test/java/com/robot/translator/translator/MyAgeTranslator.java b/src/test/java/com/robot/translator/translator/MyAgeTranslator.java new file mode 100644 index 0000000..d641f10 --- /dev/null +++ b/src/test/java/com/robot/translator/translator/MyAgeTranslator.java @@ -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 "壮年"; + } + } +}