[英]How BeanPropertyRowMapper works internally in spring?
我知道BeanPropertyRowmapper
在我触发select
查询时使用setter
方法,但它是否使用getter
方法 ?
我面临以下问题:
在database
,defaultPriority是string
但我想在我的SMSAction
pojo类中设置int
值。
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
使用getDefaultPrioriy()
并且我的log
打印在getDefaultPriority()
内部也没有显示 。
注意:我不想制作自定义行映射器或hibernate或JPA。 请帮我。
在我看来,你做的事情(不是非常)是错误的。 您有一些规则可以在模板中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 /包装器对象或类似的东西中将值从一层传递到另一层。
祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.