Final 字段是可变的。我的问题是什么?
我正在使用 mybatis 和 jackson-databind
我尝试计算结果类型 dto 的构造函数中的一些值
不幸的是,getter 返回原始值,而不是计算值
我也尝试使用 @JsonCreator、@JsonProperty 等...
但是 getter 有返回原始值
有趣的是,当构造函数的参数名称更改(或sql别名)时,getter返回计算值
我猜jackson或mybatis覆盖了某些东西(或java反射问题?)
我怎样才能在不改变的情况下解决这个问题一些名字?
这是我的查询
<select id="getVoteResult" resultType="VoteResultDto">
SELECT SUM(good) AS voteCountOfGood
, SUM(bad) AS voteCountOfBad
FROM some.recommend_inf
WHERE id = #{id};
</select>
和结果 dto
public final class VoteResultDto {
private final int voteCountOfGood;
private final int voteCountOfBad;
public VoteResultDto(float voteCountOfGood, float voteCountOfBad) {
System.out.println("float voteCountOfGood: " + voteCountOfGood);
System.out.println("float voteCountOfBad: " + voteCountOfBad);
if (voteCountOfGood > 0 || voteCountOfBad > 0) {
this.voteCountOfGood = Math.round(voteCountOfGood / (voteCountOfGood + voteCountOfBad) * 100);
this.voteCountOfBad = 100 - this.voteCountOfGood;
} else {
this.voteCountOfGood = this.voteCountOfBad = 0;
}
System.out.println("this.voteCountOfGood: " + this.voteCountOfGood);
System.out.println("this.voteCountOfBad: " + this.voteCountOfBad);
}
public int getVoteCountOfGood() {
return voteCountOfGood;
}
public int getVoteCountOfBad() {
return voteCountOfBad;
}
@Override
public String toString() {
return "toString(): {voteCountOfGood: " + voteCountOfGood + ", voteCountOfBad: " + voteCountOfBad + '}';
}
}
和日志
float voteCountOfGood: 8.0
float voteCountOfBad: 4.0
this.voteCountOfGood: 67
this.voteCountOfBad: 33
toString(): {voteCountOfGood: 8, voteCountOfBad: 4}
I'm using mybatis and jackson-databind
And I tried to calculate some values in constructor of result type dto
Unfortunately, getter returns original values, not calculated values
I also tried to using @JsonCreator, @JsonProperty, etc...
But getter had return original value
Interestingly, When parameter names of constructor was changed(or sql alias), getter returned calculated value
I guess jackson or mybatis overrides something(or java reflection problem?)
How can I solve this problem without change some names?
This is my query
<select id="getVoteResult" resultType="VoteResultDto">
SELECT SUM(good) AS voteCountOfGood
, SUM(bad) AS voteCountOfBad
FROM some.recommend_inf
WHERE id = #{id};
</select>
and result dto
public final class VoteResultDto {
private final int voteCountOfGood;
private final int voteCountOfBad;
public VoteResultDto(float voteCountOfGood, float voteCountOfBad) {
System.out.println("float voteCountOfGood: " + voteCountOfGood);
System.out.println("float voteCountOfBad: " + voteCountOfBad);
if (voteCountOfGood > 0 || voteCountOfBad > 0) {
this.voteCountOfGood = Math.round(voteCountOfGood / (voteCountOfGood + voteCountOfBad) * 100);
this.voteCountOfBad = 100 - this.voteCountOfGood;
} else {
this.voteCountOfGood = this.voteCountOfBad = 0;
}
System.out.println("this.voteCountOfGood: " + this.voteCountOfGood);
System.out.println("this.voteCountOfBad: " + this.voteCountOfBad);
}
public int getVoteCountOfGood() {
return voteCountOfGood;
}
public int getVoteCountOfBad() {
return voteCountOfBad;
}
@Override
public String toString() {
return "toString(): {voteCountOfGood: " + voteCountOfGood + ", voteCountOfBad: " + voteCountOfBad + '}';
}
}
and log
float voteCountOfGood: 8.0
float voteCountOfBad: 4.0
this.voteCountOfGood: 67
this.voteCountOfBad: 33
toString(): {voteCountOfGood: 8, voteCountOfBad: 4}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论