【问题标题】:Cannot use method with argument in mapper file无法在映射器文件中使用带参数的方法
【发布时间】:2014-04-03 10:07:32
【问题描述】:

Java 1.7、WildFly 8、MyBatis 3.2.6、PostgreSQL 9.4。

我有一个简单的借书系统原型。我使用 MyBatis 作为 ORM。在我尝试过滤器之前,一切都很好。 有书桌。获取书籍列表在映射器中定义如下:

<select id="selectBooks" resultType="org.bookman.json.library.JsonBook">
  select id, created, inclusion_date as inclusionDate, title, author
  from bok_books
  <where>
    <if test="filter('title') != null">
      title ilike #{filter('title')}||'%'
    </if>
  </where>
  order by ${orderCols}
</select>

在 EL 中使用了两种方法 - filter(String) 和 getOrderCols()。 select就是这样调用的:

TableReqHnd bookHnd = new TableReqHnd();
... // among other things sets filter data
List<JsonBook> books = sqlSession.selectList("selectBooks", bookHnd, rowBounds);

这是 TableReqHnd 类的外观:

public class TableReqHnd
{
  private Map<String, String> filters        = new HashMap<>();
  ...
  public String getOrderCols()
  {
    ...
  }
  ...
  public String filter(String fieldId)
  {
    if (!filters.containsKey(fieldId)) return null;
    return filters.get(fieldId);
  }
}

为什么我会那样做?它允许我为任何表重用 TableReqHnd 对象,只需为排序、过滤器等定义新值。我不必为每个表创建单独的类,每个过滤器都有自己的 setter 和 getter。

现在问题本身。方法 getOrderCols() 工作正常,但 filter(String) 不能。以下是引发的异常:http://pastebin.com/raw.php?i=9jAjBhtd

它基本上抛出 java.lang.NoSuchMethodException: filter(java.lang.String)。我觉得很奇怪。 TableReqHnd 中肯定存在这个方法:public String filter(String fieldId)。 ${orderCols} 工作正常(它只是简单的 getter public String getOrderCols())。

根据 OGNL 规范,调用

filter('title')

应该是正确的。异常实际上证实了它应该是可能的,它只是由于某种原因找不到方法。有人知道它是否可以以某种方式修复?

【问题讨论】:

    标签: datamapper mybatis


    【解决方案1】:

    你试过了吗

    <select id="selectBooks" paramType="org.bookman.json.library.JsonBook">
      select id, created, inclusion_date as inclusionDate, title, author
      from bok_books
      <where>
        <if test="filter('title') != null">
          title ilike #{filter('title')}||'%'
        </if>
      </where>
      order by ${orderCols}
    </select>
    

    【讨论】:

    • 试过了,更具体地说是
    猜你喜欢
    • 1970-01-01
    • 2018-04-24
    • 2017-03-19
    • 2013-10-07
    • 1970-01-01
    • 2019-08-31
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多