在Spring Batch应用程序中,我读取了一个数据库表并将其映射到dto。我的dto有一些类型为boolean
的字段。
我的Dto和dao方法如下:
public class MyDto {
private long id;
private String yyy;
private boolean xxx;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getYyy() {
return id;
}
public void setYyy(String yyy) {
this.yyy = yyy;
}
public boolean getXxx() {
return xxx;
}
public void setXxx(boolean xxx) {
this.xxx = xxx;
}
}
@Override
public MyDto readMyDto(String id) {
SqlParameterSource paramMap = new MapSqlParameterSource(ID, id);
BeanPropertyRowMapper<MyDto > rowMapper = new BeanPropertyRowMapper<>(MyDto.class);
rowMapper.setPrimitivesDefaultedForNullValue(true);
try {
return this.jdbcTemplateStpl.queryForObject(LESE_KRAFTST_STEUERFALL, paramMap, rowMapper);
} catch (EmptyResultDataAccessException e) {
return null;
}
}
尽管我将Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [null] to required type [boolean] for property 'xxx'
设置为setPrimitivesDefaultedForNullValue
,但我得到了true
。
在我的UnitTests中,即使有异常,它也可以正常工作。当我将setPrimitivesDefaultedForNullValue
设置为false
时,使用Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [null] to required type [boolean] for property 'xxx'.
setPrimitivesDefaultedForNullValue
设置为true
的异常是我这边还是Spring Batch上的错误,还是这种行为是预期的,并且BeanPropertyRowMapper会捕获该异常,并且在这种情况下(布尔值)默认为null?< / p>
答案 0 :(得分:1)
Your method..
public void setId(boolean xxx) {
this.xxx = xxx;
}
should be
public void setXxx(boolean xxx) {
this.xxx = xxx;
}