Skip to content

Commit

Permalink
添加aop功能和完善测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhan committed Apr 27, 2020
1 parent 3a274f9 commit ca22316
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 95 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# 翻译工具Translator
批量数据字典翻译,字典数据源支持静态字典表
业务中有没有遇到查询结果中经常需要进行数据字典翻译的情况,如果还在用连表查询或者for循环处理,可以试试这个!
## 快速开始
用一个简单的Demo来阐述Translator的功能:
现有一张`student`表,其结构如下:

### 1. 在具有数据字典功能的Entity类上标识注解@Dictionary
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
<relativePath/>
</parent>
<groupId>com.robot</groupId>
<artifactId>translator</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>translator</name>
<description>Demo project for Spring Boot</description>
<description>Translator with Springboot</description>

<properties>
<java.version>1.8</java.version>
Expand All @@ -23,14 +23,20 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/robot/translator/core/TranslateAspect.java
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());
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/robot/translator/core/Translator.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public static <T> List<T> parse(List<T> 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);
Expand Down
25 changes: 21 additions & 4 deletions src/test/java/com/robot/translator/SampleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,45 @@ public class SampleTest {
JdbcTemplate template;

@Test
public void testSelect() {
// List<Map>翻译
public void testTranslate() {

// 1.List<Map>翻译
List<Map<String, Object>> maps = template.queryForList("select * from student");
Translator.parse(maps, Student.class);
maps.forEach(System.out::println);
// {id=1, name=张三, class_id=1, sex=0, age=18, class_name=三年一班, sex_name=男}
// {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<entity>翻译
// 2.List<entity>翻译
List<Student> students = template.query("select * from student", new BeanPropertyRowMapper<>(Student.class));
Translator.parse(students);
students.forEach(System.out::println);
// Student(id=1, name=张三, classId=1, className=三年一班, sex=0, sexName=男, age=18)
// 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<String, Object> 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<Student> queryAllStudents() {
return template.query("select * from student", new BeanPropertyRowMapper<>(Student.class));

}

@Test
public void testAop() {
//
}
}
39 changes: 39 additions & 0 deletions src/test/java/com/robot/translator/dict/MyDict.java
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);
}
}

}
31 changes: 0 additions & 31 deletions src/test/java/com/robot/translator/dict/ResDict.java

This file was deleted.

23 changes: 4 additions & 19 deletions src/test/java/com/robot/translator/entity/Class.java
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;
Expand All @@ -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;
}

}
33 changes: 2 additions & 31 deletions src/test/java/com/robot/translator/entity/StaticDict.java
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;

Expand All @@ -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;
Expand All @@ -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;
}

}
15 changes: 11 additions & 4 deletions src/test/java/com/robot/translator/entity/Student.java
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
Expand All @@ -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 src/test/java/com/robot/translator/translator/MyAgeTranslator.java
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 "壮年";
}
}
}

0 comments on commit ca22316

Please sign in to comment.