Final 字段是可变的。我的问题是什么?

发布于 2025-01-10 22:28:00 字数 1918 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文