You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Elide.io实现多表联合查询的过滤排序等功能方案咨询

基于Elide 6.1的原生聚合查询方案

不用自己实现过滤、排序、分页,完全复用Elide原生能力的方案如下:

1. 配置实体关联与Elide注解

给三张表的实体类添加Elide核心注解,明确关联关系:

  • Table3实体关联Table1、Table2(多对一),注意映射table1Id到Table1的refId:
@Entity
@Include(name = "table3")
public class Table3 {
    @Id
    private Long id;
    private LocalDate date;
    @Column(name = "table1_id")
    private Long table1Id;

    // 关联Table1,仅用于查询,不参与增改
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "table1_id", referencedColumnName = "ref_id", insertable = false, updatable = false)
    private Table1 table1;

    // 关联Table2
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "table2_id")
    private Table2 table2;

    // getter/setter
}
  • Table1、Table2添加基础Elide注解:
@Entity
@Include(name = "table1")
public class Table1 {
    @Id
    @Column(name = "ref_id")
    private Long refId;
    // 其他业务字段及getter/setter
}

@Entity
@Include(name = "table2")
public class Table2 {
    @Id
    private Long id;
    // 其他业务字段及getter/setter
}

2. 用Elide Projection实现数据聚合

无需自定义DTO,通过@Projection直接聚合三张表的字段:

@Projection(name = "table3Aggregate", type = Table3.class)
public interface Table3AggregateProjection {
    // Table3自身字段
    Long getId();
    LocalDate getDate();

    // Table1关联字段,可自定义返回字段名
    @JsonProperty("table1Id")
    default Long getTable1RefId() {
        return getTable1().getRefId();
    }
    String getTable1Name(); // 示例:返回Table1的name字段

    // Table2关联字段
    String getTable2Code(); // 示例:返回Table2的code字段
}

Elide会自动执行关联查询,将三张表的数据聚合到投影结构中。

3. 复用Elide原生接口能力

直接使用Elide原生接口

Elide自带REST接口,通过以下请求即可获取聚合数据,同时原生支持过滤、排序、分页:

  • 基础请求(返回聚合结构):GET /api/v1/table3?projection=table3Aggregate
  • 过滤table1Id多值:GET /api/v1/table3?projection=table3Aggregate&filter=table1.refId=in=(1,2,3)
  • 过滤date区间:GET /api/v1/table3?projection=table3Aggregate&filter=date=between=('2024-01-01','2024-12-31')
  • 排序:添加&sort=date,desc
  • 分页:添加&page[number]=0&page[size]=10

自定义接口路径/table3data

如果需要指定/table3data路径,通过Spring转发请求到Elide原生接口即可:

@RestController
@RequestMapping("/table3data")
public class Table3DataController {
    private final Elide elide;

    public Table3DataController(Elide elide) {
        this.elide = elide;
    }

    @GetMapping
    public ResponseEntity<?> getAggregateData(HttpServletRequest request) {
        // 拼接Elide原生请求路径,带上投影参数
        String elidePath = "/api/v1/table3?projection=table3Aggregate" + (request.getQueryString() != null ? "&" + request.getQueryString() : "");
        request.setAttribute("elidePath", elidePath);
        return elide.handleRequest(request);
    }
}

这样前端请求/table3data时,会自动继承Elide的所有过滤、排序、分页能力。

4. 权限控制(可选)

通过Elide的权限注解控制访问,比如允许所有用户读取:

@Include(name = "table3")
@ReadPermission(expression = "allow all")
public class Table3 {
    // 实体内容
}

也可以给Projection单独配置权限,细化字段访问控制。

内容的提问来源于stack exchange,提问作者5th

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 06:53:14