BeanPropertyRowMapper如何在Spring内部工作?

时间:2015-01-21 05:29:38

标签: java database spring spring-mvc java-ee

我知道BeanPropertyRowmapper使用setter方法解除select查询但是否使用getter方法

我面临以下问题:

database中,defaultPriority位于string,但我想在int pojo类中设置SMSAction值。

class SMSAction implements Serializable {

private int defaultPriority;

public int getDefaultPriority() {
    System.out.println("Inside getDefaultPriority()");
    return defaultPriority;
}

public void setDefaultPriority(String defaultPriority) {
    System.out.println("Inside setDefaultPriority(String defaultPriority)"+defaultPriority);

    if(defaultPriority.equalsIgnoreCase("L")){
        System.out.println("Condition");
        this.defaultPriority = 1;
    }
  }
}    

这是我得到的错误:

Exception in thread "main" org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [select SMSACTIONID,SMSACTIONCODE,ACTIONDESC,CASID,DEFAULTPRIORITY from tblsmsaction]; SQL state [99999]; error code [17059]; Fail to convert to internal representation; nested exception is java.sql.SQLException: Fail to convert to internal representation
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:84)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:660)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:695)

如果在数据库和我的pojo中数据类型不同时如何解决上述问题? 当我将 return type of getDefaultPriority()更改为String 然后正常工作但我无法理解为什么BeanPropertyRowMapper使用{ {1}}以及我在getDefaultPrioriy() 内打印的log未显示

注意:我不想制作自定义行映射器或休眠或JPA。 请帮帮我。

1 个答案:

答案 0 :(得分:0)

在我看来,你做的事情(不是很)是错的。您的模板中有某些规则set的内容(阅读class)。 Setters / Getters应该非常整洁;您需要存储int这一事实但是您正在应用一些规则来确定int值与您在bean中设置/获取该特定值的方式无关

每当您从数据库中获取String值时,请应用规则并调用setter:

// [db layer] here you already got the real value(s) through a database call
SMSAction action = new SMSAction();

// priority is some arbitrary name I used to simulate the variable that holds the String coming from the database
if (priority.equalsIgnoreCase("L")) {
  action.setDefaultPriority(1);
}
// ...

现在,有些人更喜欢另一层中的那种商业规则,但这是一个不同的主题;正如您所看到的那样,在DTO /包装器对象或类似物品中,可以轻松地将值从一层转移到另一层。

祝你好运!

相关问题