Giter VIP home page Giter VIP logo

zzemptypoint / crane4j Goto Github PK

View Code? Open in Web Editor NEW

This project forked from opengoofy/crane4j

0.0 0.0 0.0 18.59 MB

一个简单易用的关联字段填充框架,通过简单的注解配置快速根据外键/编码值填充相关字段,支持字典,枚举,方法等多种数据源。 A simple and easy-to-use data mapping framework, quickly populate relevant fields according to foreign keys/encoded values through simple annotation configuration, supports dictionaries, enumerations, methods and other data sources.

Home Page: https://opengoofy.github.io/crane4j/

License: Apache License 2.0

Shell 0.13% Java 99.38% TypeScript 0.50%

crane4j's Introduction

English | 中文

image-20230220150040070

codecov codecov star stars maven-central

Crane4j

一个简单易用的数据映射框架,通过简单的注解配置快速根据外键/编码值填充相关字段,支持字典,枚举,方法等多种数据源。

image-20230810233647099

它是什么?

在日常的开发工作中,我们经常面临着繁琐的数据组装任务:根据一个对象的某个属性值,获取相关联的数据,并将其映射到另一个对象的属性中。这种需求经常涉及到字典项、配置项、枚举常量,甚至需要进行关联数据库表的查询。这样的数据填充任务往往耗费大量时间和精力,而且容易产生重复的样板代码,让人感到心烦。

crane4j 旨在为了解决这种烦恼而生,它是一套注解驱动的数据填充框架。通过简单的注解配置,crane4j 可以优雅高效地完成不同数据源、不同类型、不同命名的字段填充任务,让你专注于核心业务逻辑而不再被繁琐的数据组装工作所困扰。

它有哪些特性?

  • 多样的数据源支持:支持枚举、键值对缓存和方法作为数据源,也可通过简单的自定义扩展兼容更多类型的数据源,并提供对所有数据源的缓存支持;
  • 强大的字段映射能力:通过注解即可完成不同类型字段的自动映射转换,还支持包括模板、排序、分组和嵌套对象填充等功能;
  • 高度可扩展:用户可以自由替换所有主要组件,结合 Spring 的依赖注入可实现轻松优雅的自定义扩展;
  • 丰富的可选功能:提供额外的自动填充方法返回值和方法入参参数,多线程填充,自定义注解和表达式,数据库框架插件等可选功能;
  • 开箱即用:简单配置即可与 spring/springboot 快速集成,也支持在非 spring 环境中使用;

文档

项目文档 document:GitHub / Gitee

快速开始

引入依赖

<dependency>
    <groupId>cn.crane4j</groupId>
    <artifactId>crane4j-spring-boot-starter</artifactId>
    <version>${last-version}</version>
</dependency>

声明填充操作

通过在类、字段或 getter 方法上添加注解,即可基于上文配置的数据源声明填充操作,支持一对一、一对多甚至多对多的属性映射:

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

    // 根据从 getter 方法获取的值,从指定的数据源中获取对应的对象,然后将其name映射到当前的name中
    @Assemble(container = "method", props = @Mapping("name"))
    public Integer getId() {
        // return value
    }
    private String name;

    // 根据 gender 获得对应的枚举对象,然后将其 name 属性映射到 genderName 中
    @AssembleEnum(
        type = Gender.class, 
        enums = @ContainerEnum(key = "code"), 
        props = @Mapping(ref = "genderName")
    )
    private Integer gender;
    private String genderName;
    
    // 根据部门 ID 查询员工列表,并取列表中员工对象的名称映射到 empNames
    @AssembleMethod(
        handlerType = OneToManyAssembleHandler.class,
        targetType = empService.class,
        method = @ContainerMethod(bindMethod = "listByDeptId", resultType = Emp.class, resultKey = "id"),
        props = @Mapping(src = "name", ref = "empNames")
    )
    private Integer deptId;
    private List<String> empNames;
    
    // 根据常量值获取对应的常量名称,并映射到 letterName
    @AssembleConstant(
        type = LetterConstant.class,
        constant = @ConstantContainer(onlyPublic = true, reverse = true)
    )
    private String letter;
    private String letterName;

    // 将自己的 name 属性映射到 fooName
    @Assemble(props = @Mapping(src = "name", ref = "fooName"))
    private String fooName;
    
    // 将 phone 字段根据自定义策略进行处理后回填
    @AssembleKey(mapper = "phone_number_desensitization")
    private String phone;
    
    // 填充嵌套对象
    @Disassemble(type = Foo.class)
    private List<Foo> nestedBeans;
}

执行填充

在方法上添加 @AutoOperate 注解即可自动对方法返回值进行填充:

@AutoOperate(type = Foo.class)
public List<Foo> doOperate() {
    // return something
}

结果:

[
    {
        "id": 1,
        "name": "foo1",
        "fooName": "foo1",
        "gender": 1,
        "genderName": "男性",
        "keys": ["1", "2", "3"],
        "values": ["A", "B", "C"]
    },
    {
        "id": 2,
        "name": "foo2",
        "fooName": "foo2",
        "gender": 0,
        "genderName": "女性",
        "keys": ["1", "2", "3"],
        "values": ["A", "B", "C"]
    }
]

这就是在 springboot 环境中使用 crane4j 的最简单步骤,更多玩法请参见官方文档。

友情链接

  • [ hippo4j ]:强大的动态线程池框架,附带监控报警功能;

参与贡献和技术支持

如果在使用中遇到了问题、发现了 bug ,又或者是有什么好点子,欢迎提出你的 issues ,或者加入社区交流群 讨论!

若无法访问连接,或者微信群二维码失效,也可以联系作者加群:

联系作者

鸣谢

感谢 JetBrains 提供的 Licenses !

JetBrains Logo (Main) logo

crane4j's People

Contributors

createsequence avatar tangcent avatar iko233 avatar lhzsdnu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.