Skip to content

如何开始

Feego edited this page Aug 4, 2019 · 3 revisions

引入依赖

Maven Central

Maven用户

<dependency>
  <groupId>io.github.lvyahui8</groupId>
  <artifactId>spring-boot-data-aggregator-starter</artifactId>
  <version>${latestVersion}</version>
</dependency>

配置

application.properties

# 指名要扫描注解的包
io.github.lvyahui8.spring.base-packages=io.github.lvyahui8.spring.example

开始编码(示例)

开发一个用户汇总数据接口, 包括用户的基础信息和博客列表

1. 定义提供基础数据的"原子"服务

使用@DataProvider定义接口为数据提供者

使用@InvokeParameter指定要传递的用户输入参数

博客列表服务

需要参数userId

@Service
public class PostServiceImpl implements PostService {
    @DataProvider("posts")
    @Override
    public List<Post> getPosts(@InvokeParameter("userId") Long userId) {

用户基础信息查询服务

需要参数userId

@Service
public class UserServiceImpl implements UserService {
    @DataProvider("user")
    @Override
    public User get(@InvokeParameter("userId") Long id) {

2. 调用聚合接口

@Autowired
DataBeanAggregateQueryFacade dataBeanAggregateQueryFacade;

方式一: 函数式调用

User user = dataBeanAggregateQueryFacade.get(
     Collections.singletonMap("userId", 1L), 
     new Function2<User, List<Post>, User>() {
            @Override
            public User apply(@DataConsumer("user") User user, 
                              @DataConsumer("posts") List<Post> posts) {
                user.setPosts(posts);
                return user;
            }
     });
Assert.notNull(user,"user not null");
Assert.notNull(user.getPosts(),"user posts not null");

方式二: 定义聚合层查询

组合@DataProvider \ @DataConsumer \ @InvokeParameter 实现汇聚功能

@Component
public class UserAggregate {
    @DataProvider("userWithPosts")
    public User userWithPosts(
            @DataConsumer("user") User user,
            @DataConsumer("posts") List<Post> posts) {
        user.setPosts(posts);
        return user;
    }
}

指定要查询的data id, 查询参数, 返回值类型, 并调用facade.get方法即可

User user = dataBeanAggregateQueryFacade.get(/*data id*/ "userWithPosts", 
                            /*Invoke Parameters*/
                            Collections.singletonMap("userId",1L), 
                            User.class);
Assert.notNull(user,"user not null");
Assert.notNull(user.getPosts(),"user posts not null");

Intro

Getting started

Advanced usage

Other

Clone this wiki locally