名为 isActive 的布尔变量的 setter

发布于 2024-10-15 00:24:48 字数 331 浏览 0 评论 0原文

我的 pojo 类中有一个名为 isActive 的属性。当我使用 Eclipse IDE 生成此属性的访问器时,它会生成以下 getter 和 setter

Getter : isActive()
Setter : setActive()

但是,当我尝试使用 ibatis 框架通过将属性名称提及为 "isActive" 来编写此属性时,它会提示无法找到任何名为的可写属性'处于活动状态'。我认为问题在于无法通过将 setter 推断为 setIsActive() 来推断出正确的属性名称。

在不更改属性名称或 getter 的情况下解决此问题的最佳方法是什么?

I have a property called isActive in my pojo class. When I generated the accessors for this property using Eclipse IDE, it generates following getters and setters

Getter : isActive()
Setter : setActive()

However, when I try to write this property using ibatis framework by mentioning property name as "isActive" , it cribs about not able to find any WRITEABLE propery named 'isActive'. The problem I think lies with not able to deduce the correct property name by inferring setter as setIsActive().

What is the best way to go about this without changing the property name or getter ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

迷迭香的记忆 2024-10-22 00:24:48

原始布尔字段 getter 创建为 isFieldName。因此,在 Ibatis 中,您应该将属性名称指定为 active 而不是 isActive

primitive boolean field getters are created as isFieldName. So in Ibatis you should give the property name as active not isActive

心房敞 2024-10-22 00:24:48

pojo 命名约定期望名为 xxxboolean 类型具有方法 isXxxsetXxx

在你的情况下,你的 pojo 应该看起来像;

public class MyPojo
{
  private boolean active;

  public boolean isActive()
  {
    return active;
  }

  public void setActive(boolean active)
  {
    this.active = active;
  }
}

您可以通过在 IDE 中创建一个类并定义 private boolean active 变量来亲自演示这一点,然后让 IDE 生成 getter 和 setter。

The pojo naming convention expects boolean types called xxx to have methods isXxx and setXxx.

In your case your pojo should look like;

public class MyPojo
{
  private boolean active;

  public boolean isActive()
  {
    return active;
  }

  public void setActive(boolean active)
  {
    this.active = active;
  }
}

You can demonstrate this yourself by creating a class in your IDE and defining the private boolean active variable, and then getting the IDE to generate getters and setters.

忆沫 2024-10-22 00:24:48

有一条出路。

访问 Windows ->首选项-> Java->代码样式并取消选择“使用'is'前缀...”属性(当然,如果您不希望将其作为 Eclipse 中的全局行为,则可以在项目属性上更改此属性)。

在我看来,这将使行为变得

Getter : getIsActive()
Setter : setIsActive()

丑陋,但 ibatis 现在应该停止抱怨。

There's a way out.

Visit Windows -> Preferences -> Java -> Code Style and deselect the "Use 'is' prefix..." property (of course you can change this on project properties if you don't want this as a global behaviour in eclipse).

This will change the behaviour to

Getter : getIsActive()
Setter : setIsActive()

Ugly to my eyes but ibatis should stop complaining now.

迷鸟归林 2024-10-22 00:24:48

我没有使用过 iBatis,但是 Hibernate 允许指定访问方法名称。您可以在此处覆盖 ORM 的默认行为,以计算设置属性的方法名称。

I have not used iBatis, but Hibernate allows you to specify the access method name. This is where you can override the default behavior of ORMs to compute method name for setting property.

独﹏钓一江月 2024-10-22 00:24:48

感谢您的回复。根据我不想更改 pojo 类成员变量的要求,我使用的 ibatis 版本无法按预期工作。当我将版本从 2.3.0 升级到 2.3.4 时,问题得到解决,并且相同的代码可以无缝运行。我假设通过这次升级,如果 boolean 原语类型的属性定义为 isActive,他们会考虑生成 isActive() 和 setIsActive() 访问器的 java beans 约定。谢谢 !

Thanks for the responses. Going by the requirements I had that I didn't wish to change my pojo class member variables, ibatis version that I was using wasn't working as expected. When I upgraded my version to 2.3.4 from 2.3.0 , the issue was resolved and same code worked seamlessly. I assume with this upgrade, they factored in the java beans convention of generating isActive() and setIsActive() accessors if property of type boolean primitive is defined as isActive. Thanks !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文