有效地重用具有相同功能但处理不同实体的多个服务。

问题描述 投票:0回答:1

我正在处理多个实体类,但它们具有相同的属性,这不是一个好的数据结构,但根据我的用例,我必须处理两个具有相同属性的实体。

这不是一个好的数据结构,但根据我的用例,我必须处理两个具有相同属性的实体。目前我使用多个控制器,多个requestresponse和多个接口和实现来做逻辑部分,并保存到两个相应的表中.有没有什么有效的方法可以让我把两者结合到一个单一的接口中,而DB操作发生在多个DB中。

以下是一个示例代码。

@RestController
..
public class FooController  {

    @Autowired
    private FooService fooService;

    @GetMapping
    public FooResponse findFoo(
            @PathVariable(FOO_CONSTANT.PATH_VARIABLE_FOO_ID) String fooId)
            throws FooException {
        return fooService.findFoo(fooId));
    }
}

public interface FooService{

    FooResponse findFoo(String fooId);

}

@Service
public class ProspectAssetServiceImpl implements ProspectAssetService {

    @Autowired
    private FooRepository fooRepository;

    @Override
    public FooResponse findFoo(String fooId){
        FooEntity fooEntity = fooRepository.findByFooId(fooId));
        return convertBomToMessaging(fooEntity);
    }


}

public class Foo {

    private String fooId;

    private String fooName;

    //getters and setters

}

@Entity
@Table(name = "foo_table")
public class FooEntity {

    private String fooId;

    private String fooName;

    //getters and setters

}

第二服务:

@RestController
..
public class ZooController  {

    @Autowired
    private ZooService ZooService;

    @GetMapping
    public ZooResponse findZoo(
            @PathVariable(Zoo_CONSTANT.PATH_VARIABLE_Zoo_ID) String ZooId)
            throws ZooException {
        return ZooService.findZoo(ZooId));
    }
}

public interface ZooService{

    ZooResponse findZoo(String ZooId);

}

@Service
public class ProspectAssetServiceImpl implements ProspectAssetService {

    @Autowired
    private ZooRepository ZooRepository;

    @Override
    public ZooResponse findZoo(String ZooId){
        ZooEntity ZooEntity = ZooRepository.findByZooId(ZooId));
        return convertBomToMessaging(ZooEntity);
    }


}

public class Zoo {

    private String ZooId;

    private String ZooName;

    //getters and setters

}

@Entity
@Table(name = "Zoo_table")
public class ZooEntity {

    private String ZooId;

    private String ZooName;

    //getters and setters

}

给我一个有效的方法把这两个结合在一起?

java spring spring-boot rest spring-data
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.