Jij*_*hew 5 java asynchronous project-reactor spring-data-r2dbc r2dbc
我是反应式编程的新手。我必须开发一个简单的 Spring Boot 应用程序来返回 json 响应,其中包含公司详细信息及其所有子公司和员工
创建了一个 Spring Boot 应用程序(Spring Webflux + Spring data r2dbc )
使用下面的数据库表来表示公司和子公司以及员工关系(这是公司和子公司的层次关系,其中一个公司可以有N个子公司,每个子公司可以有另外N个子公司等等)
以下是代表上述表格的 java 模型类
公司.java
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class Company implements Serializable {
private int id;
private String name;
private String address;
@With
@Transient
private List<Company> subCompanies;
@With
@Transient
private List<Employee> employees;
}
Run Code Online (Sandbox Code Playgroud)
员工.java
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class Employee implements Serializable {
@Id
private int id;
private String name;
private String designation;
}
Run Code Online (Sandbox Code Playgroud)
创建以下存储库
@Repository
public interface CompanyRepository extends ReactiveCrudRepository<Company, Integer> {
@Query("select sub.sub_company_id from Company_SubCompany sub inner join Company c on sub.sub_company_id = c.id where sub.id = :id")
Flux<Integer> findSubCompnayIds(int id);
@Query("select * from Company c where id = :id")
Mono<Company> findCompanyById(Integer id);
}
@Repository
public interface EmployeeRepository extends ReactiveCrudRepository<Employee, Integer> {
@Query("select * from Employee where company_id = :id")
Flux<Employee> findEmployeeByCompanyId(Integer id);
}
Run Code Online (Sandbox Code Playgroud)
在Company_SubCompany 表中,超级公司用id -1 表示。因此,使用这个 id 我们现在可以获得超级母公司。
通过以下服务代码,我现在可以获得公司及其员工的一级信息。但我不知道如何获取所有嵌套的子公司并添加到其中
@Service
public class ComanyService {
@Autowired
CompanyRepository companyRepository;
@Autowired
EmployeeRepository employeeRepository;
public Flux<Company> findComapnyWithAllChilds() {
Flux<Integer> childCompanyIds = companyRepository.findSubCompnayIds(-1);
Flux<Company> companies = childCompanyIds.flatMap(p -> {
return Flux.zip(companyRepository.findCompanyById(p),
employeeRepository.findEmployeeByCompanyId(p).collectList(),
(t1, t2) -> t1.withEmployees(t2));
});
return companies;
}
}
Run Code Online (Sandbox Code Playgroud)
我对反应式、函数式编程和 r2dbc 非常陌生,所以请帮助我解决我的问题。如果有任何其他更好的方法可用,也将使用该方法。要求是获取公司、其所有员工和子公司(最多N个员工)。
小智 6
这段代码可以帮助你,这种方法从数据库调用中填充列表对象
public Flux<Company> findComapnyWithAllChilds(){
return companyRepository.findAll()
.flatMap(companies ->
Mono.just(companies)
.zipWith(employeeRepository.findEmployeeByCompanyId(companies.getId()).collectList())
.map(tupla -> tupla.getT1().withEmployees(tupla.getT2()))
.zipWith(anotherRepository.findAnotherByCompanyId(companies.getId()).collectList())
.map(tupla -> tupla.getT1().withAnother(tupla.getT2()))
);
Run Code Online (Sandbox Code Playgroud)
}