3 Star 27 Fork 2

easii / easy-relation

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 3.39 KB
一键复制 编辑 原始数据 按行查看 历史
easii 提交于 2023-06-13 11:49 . add v1.1.1 docs

EasyRelation

这是什么?

EasyRelation 是一个简单、高效的自动关联数据框架,可以通过一行代码,自动关联查询并填充需要的数据,对于性能影响极小,且省略了大量冗余代码。

该项目适应于当前对象中的字段需要关联查询,并赋值到当前对象中,数据来源可以是枚举数据库RPC接口 等等任意来源。

如果该项目帮助了您,希望能点个 Star 鼓励一下!

链接地址

特点

  • 不限制关联查询方式,需要关联的数据可以是任意来源
  • 两级缓存支持,可自由选择使用的缓存
  • 执行效率高,对性能影响极小
  • 支持自动关联(since 1.1.1)

快速开始

安装依赖

  • maven
<dependency>
    <groupId>cn.easii</groupId>
    <artifactId>easy-relation-spring-boot-starter</artifactId>
    <version>1.1.0</version>
</dependency>

定义对象

假设有一个订单模型(Order),其只保存了用户名,需要关联查询昵称:

@Data
public class Order {

    private String orderId;

    private String username;

    @Relation(provider = RelationIdentifiers.getUserByUsername, targetField = "nickName",
        condition = {@Condition(field = "username")})
    private String nickName;

}

如上定义中,在需要关联查询的字段,添加@Relation注解,指定关联关系,这里的 targetField 表示当前字段需要查询结果中的指定 nickName 属性。

定义数据提供者

定义一个类,继承 DataProvideService,且实现一个查询用户信息的方法,添加 @DataProvider 注解,并指定其唯一标识,

@Component
public class UserInfoDataProvider implements DataProvideService {

    @DataProvider(RelationIdentifiers.getUserByUsername)
    public User getUserByUsername(UserQueryReq req) {
        // 这里可以从任意来源获取值
        if ("admin".equals(req.getUsername())) {
            final User user = new User();
            user.setUsername("admin");
            user.setNickName("管理员");
            return user;
        }
        return null;
    }

}

使用

@SpringBootTest
class InjectRelationTest {

    @Autowired
    private InjectRelation injectRelation;

    @Test
    void quickStart() {
        Order order = new Order();
        order.setOrderId("2f453910375641648ab3a2fc6e3328ef");
        order.setUsername("admin");
        injectRelation.injectRelation(order);
        System.out.println(order);  // Order(orderId=2f453910375641648ab3a2fc6e3328ef, username=admin, nickName=管理员)
        Assert.equals(order.getNickName(), "管理员");
    }

}

总结

使用 EasyRelation 主要有三步:

  1. 在类结构中配置关联关系
  2. 定义关联查询数据源
  3. 获取 InjectRelation 实例,调用其 injectRelation 方法,自动注入关联数据

联系我

个人网站:代码笔耕

vx : Clue8a796d01

Clue8a796d01

公众号:代码笔耕

代码笔耕

Java
1
https://gitee.com/easii/easy-relation.git
git@gitee.com:easii/easy-relation.git
easii
easy-relation
easy-relation
main

搜索帮助