Skip to content

Commit

Permalink
doc: fix example (GitHub #322)
Browse files Browse the repository at this point in the history
  • Loading branch information
Createsequence committed Jul 30, 2024
1 parent 0ad9a9c commit 46bcdb5
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package cn.crane4j.spring.boot.example;

import cn.crane4j.annotation.ArgAutoOperate;
import cn.crane4j.annotation.AssembleEnum;
import cn.crane4j.annotation.AutoOperate;
import cn.crane4j.annotation.ContainerEnum;
import cn.crane4j.annotation.Mapping;
import cn.crane4j.spring.boot.config.Crane4jAutoConfiguration;
import lombok.Data;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
* 示例如何使用{@link AutoOperate}以及{@link ArgAutoOperate}使用自动填充
*
* @author huangchengxing
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Crane4jSpringBootStarterExampleApplication.class, EnumExampleTest.ServiceImpl.class, Crane4jAutoConfiguration.class})
@TestPropertySource(properties = {
"crane4j.enable-method-result-auto-operate=true",
"crane4j.enable-method-argument-auto-operate=true",
})
@EnableAspectJAutoProxy
public class EnumExampleTest {

@Autowired
private ServiceImpl service;

@Test
public void test() {
List<Student> students = service.getStudents(Arrays.asList(1, 2));
System.out.println(students);
}

@Component
public static class ServiceImpl { // 你的 service 接口,确保 Spring 可以代理它

@AutoOperate(type = Student.class) // 声明自动填充该方法的返回值
public List<Student> getStudents(Collection<Integer> ids) {
return ids.stream() // 模拟返回数据
.map(id -> {
int genderCode = id % 2; // 随机设置一个性别编码
return new Student().setGenderCode(genderCode);
})
.collect(Collectors.toList());
}
}

@Accessors(chain = true)
@Data
public static class Student {

@AssembleEnum(
type = Gender.class, // 指定数据源为 Gender 枚举类
props = @Mapping(ref = "genderName") // 将 value 映射到 genderName 字段上
)
private Integer genderCode;
private String genderName;
}

@ContainerEnum(
key = "code", // key 为 Gender.code 字段值
value = "value" // value 为 Gender.value 字段值
)
@Getter
@RequiredArgsConstructor
public enum Gender {
FEMALE(0, "女"),
MALE(1, "男");

private final Integer code;
private final String value;
}
}
36 changes: 15 additions & 21 deletions website/docs/use_case/example_fill_enum.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,20 @@ public class Student {

## 2.使用

首先,你需要在枚举类上通过 `@ContainerEnum` 配置枚举的 key 和 value:

~~~java
@ContainerEnum(
key = "code", // key 为 Gender.code 字段值
value = "value" // value 为 Gender.value 字段值
)
@Acccessor(chain = true)
@Data
public class Student {

// 其他属性......

private Integer genderCode;
private String genderName;
}
~~~

然后,你需要在 `genderCode` 字段上通过 `@AssembleEnum` 注解声明一个装配操作:
你需要在目标类的 `genderCode` 字段上通过 `@AssembleEnum` 注解声明一个装配操作,并在里面指定将哪个枚举类作为数据源,并且指定在填充时字段需要以什么样的方式进行映射:

~~~java
@Acccessor(chain = true)
@Data
public class Student {
@AssembleEnum(
type = Gender.class, // 指定数据源为 Gender 枚举类
props = @Mapping(ref = "genderName") // 将 value 映射到 genderName 字段上
props = @Mapping(ref = "genderName"), // 将 value 映射到 genderName 字段上
followTypeConfig = false,
enums = @ContainerEnum(
key = "code", // key 为 Gender.code 字段值
value = "value" // value 为 Gender.value 字段值
)
)
private Integer genderCode;
private String genderName;
Expand Down Expand Up @@ -108,6 +95,7 @@ service.getStudents(Arrays.asList(1, 2));

~~~java
@ContainerEnum(
namespace = "gender" // 指定枚举容器的 namespace
key = "code", // key 为 Gender.code 字段值
value = "value" // value 为 Gender.value 字段值
)
Expand All @@ -130,11 +118,17 @@ private enum Gender {
public class Student {
@AssembleEnum(
type = Gender.class, // 指定数据源为 Gender 枚举类
props = @Mapping(ref = "genderName") // 将 value 映射到 genderName 字段上
props = @Mapping(ref = "genderName") // 将 value 映射到 genderName 字段上
)
private Integer genderCode;
private String genderName;
}
~~~

其他部分与之前完全一样。

:::tip

更多内容请参见 [枚举数据源容器](./../basic/container/enum_container) 一节。

:::

0 comments on commit 46bcdb5

Please sign in to comment.