在Java Spring Boot应用程序上工作时,我发现以下问题,但我不知道是否存在可以解决我的问题的模式。

我将尝试详细解释我的情况以及如何解决这种情况(目前尚无具体结果)。

我有一个名为ExcelTrendTabGeneralDTO的DTO类:

public class ExcelTrendTabGeneralDTO {

    private String excelDocumentName;
    private String excelTabName;

    private List<ExcelTabInterface> tabTrendList;

    public ExcelTrendTabGeneralDTO() {
        super();
    }

    public ExcelTrendTabGeneralDTO(String excelDocumentName, String excelTabName,
            List<ExcelTabInterface> tabTrendList) {
        super();
        this.excelDocumentName = excelDocumentName;
        this.excelTabName = excelTabName;
        this.tabTrendList = tabTrendList;
    }

    .............................................................
    .............................................................
    .............................................................
    GETTER AND SETTER METHODS
    .............................................................
    .............................................................
    .............................................................
}


这节课是我问题的核心。如您所见,它包含以下字段:

    private List<ExcelTabInterface> tabTrendList;


目前,我定义为一个空接口:

public interface ExcelTabInterface {

}


这是ExcelTabInterface的列表(这是一个接口,现在我解释为什么和要做什么)。在一开始,这个ExcelTabInterface相反,我有一个特定的具体类型(一个类),但我用一个接口代替了它,因为此列表可能包含实现该接口的类的不同种类的对象实例(这是我的想法)。

例如,我有一个像这样的类(名为CompVibrAndTempDTO):

@Description(value = "DTO for the \"Vibration Monitore\" Excel tab")
public class CompVibrAndTempDTO extends ExcelTabAbstractDTO implements ExcelTabInterface {
    private String tempReadingPointA;
    private String tempReadingPointB;
    private String tempReadingPointC;

    ....................................................................
    ....................................................................
    ....................................................................
    CONSTRUCTOR AND GETTER AND SETTER METHODS
    ....................................................................
    ....................................................................
    ....................................................................
}


好的...这个主意似乎行得通(我的IDE中没有语法错误)。

问题出在这里:然后我有了一个使用JdbcTemplate在数据库上执行查询的存储库类:

@Repository
public class ExcelRepositoryImpl implements ExcelRepository{

    @Autowired
     private JdbcTemplate jdbcTemplate;

    @Override
    //public List<CompVibrAndTempDTO> findCompVibrAndTempTab() {
    public List<ExcelTabInterface> findCompVibrAndTempTab() {

        String SELECT_COMP_VIBR_AND_TEMP_TAB = "SELECT * FROM TREND006";

        List<ExcelTabInterface> resultList = jdbcTemplate.query(SELECT_COMP_VIBR_AND_TEMP_TAB, new CompVibrAndTempMapper());

        return resultList;

    }

}


如您所见,我正在执行SQL查询,期望检索实现ExcelTabInterface接口的对象列表。问题是要构建这些对象,我使用了一个名为CompVibrAndTempMapper的自定义映射器类,该类实现了Spring RowMapper接口。

这是我的自定义映射器类:

public class CompVibrAndTempMapper implements RowMapper<ExcelTabInterface>{

    @Override
    public CompVibrAndTempDTO mapRow(ResultSet rs, int rowNum) throws SQLException {
        ExcelTabInterface compVibrAndTempDto = new CompVibrAndTempDTO();

        SimpleDateFormat formatDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat formatDate = new SimpleDateFormat("dd/MM/yyyy");
        SimpleDateFormat formatTime = new SimpleDateFormat("HH:mm:ss");

        String dateStr = null;
        String timeStr = null;

        String dateFromDB = rs.getString("Time_Stamp");

        try {
            Date date = formatDateTime.parse(dateFromDB);
            dateStr = formatDate.format(date);
            timeStr = formatTime.format(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        compVibrAndTempDto.setDate(dateStr);
        compVibrAndTempDto.setTime(timeStr);
        compVibrAndTempDto.setTempReadingPointA(rs.getString("Sensor464DV"));
        compVibrAndTempDto.setTempReadingPointB(rs.getString("Sensor465DV"));
        compVibrAndTempDto.setTempReadingPointC(rs.getString("Sensor466DV"));
        ..........................................................................
        ..........................................................................
        ..........................................................................
        return compVibrAndTempDto;
    }

}


这是我的问题。问题是它给我setter方法错误,因为这些方法未在我的界面中定义(为空)。

好的,我可以为我的具体DTO插入这些setter方法,然后解决此错误!但是,这部分地抵消了我推广DTO类的努力。

因为我还有其他实现我的ExcelTabInterface接口的DTO类->所以我也必须将其他这些DTO类的getter和setter方法插入我的接口->所以我需要在我的所有声明所有DTO的所有方法实现ExcelTabInterface接口的DTO类。

从理论上讲,我认为解决方案可以实现我需要在所有DTO类中的ExcelTabInterface接口中声明的所有方法,并将无需使用的mnethods实现为“不执行任何操作”方法,但在我看来,这是一个不错的选择一团糟。

你有什么想法?可能是可行的解决方案,还是丑陋的地狱?存在一些模式或类似的东西可以更整洁地解决我的问题吗?

最佳答案

以及为什么要定义

ExcelTabInterface compVibrAndTempDto = new CompVibrAndTempDTO();

只是使用

CompVibrAndTempDTO compVibrAndTempDto = new CompVibrAndTempDTO();

您在这里需要具体的课程,而您只有一个。

    new RowMapper<ExcelTabInterface>() {
        @Override
        public ExcelTabInterface mapRow(ResultSet rs, int rowNum) throws SQLException {
            CompVibrAndTempDTO compVibrAndTempDTO = new CompVibrAndTempDTO();
            compVibrAndTempDTO.setvalue("value");
            return compVibrAndTempDTO ;
        }
    };

07-28 03:30
查看更多