postgresql MyBatis 设置参数时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10406969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:53:06  来源:igfitidea点击:

Error in MyBatis while setting parameters

javaibatismybatispostgresql

提问by Topo

I get this error when running select in MyBatis with PostgreSQL:

使用 PostgreSQL 在 MyBatis 中运行 select 时出现此错误:

### The error may exist in data/mapper.xml
### The error may involve Transaccion.selectDeFraude-Inline
### The error occurred while setting parameters
### SQL: SELECT transaction_id, card_number, transaction_date, fraud FROM transactions.? ORDER BY card_number, transaction_date ASC;
### Cause: org.postgresql.util.PSQLException: ERROR: error de sintaxis en o cerca de ??

I get the error here, in the mapper.xml:

我在这里得到错误,在 mapper.xml 中:

<select id="selectDeFraude" parameterType="String" resultMap="result">
    SELECT transaction_id, card_number, transaction_date, fraud FROM transactions.#{tabla} ORDER BY card_number, transaction_date ASC;
</select>

This is the method that calls the select:

这是调用选择的方法:

public List<Transaccion> selectDeFraude(String tabla){

    SqlSession session = sqlSessionFactory.openSession();

    try {
        List<Transaccion> list = session.selectList("Transaccion.selectDeFraude", tabla);
        return list;
    } finally {
        session.close();
    }
}

If I replace #{tabla} with the name of the table it works just fine. None of the mapper methods work but all of them work if I replace the #{something} with the appropriate value.

如果我用表名替换 #{tabla} ,它就可以正常工作。所有映射器方法都不起作用,但如果我将 #{something} 替换为适当的值,它们都会起作用。

回答by Topo

It's not exactly the answer to the question but I got it working now. When mapping a table name with myBatis you should use ${table_name}instead of #{table_name}, and it should be an attribute of the object you passed as an argument.

这不完全是问题的答案,但我现在开始工作了。当用 myBatis 映射表名时,你应该使用${table_name}而不是#{table_name},它应该是你作为参数传递的对象的属性。

I changed my code to look like this:

我将代码更改为如下所示:

<select id="selectDeFraude" parameterType="Transaccion" resultMap="result">
    SELECT transaction_id, card_number, transaction_date, fraud FROM transactions.${tabla} ORDER BY card_number, transaction_date ASC;
</select>

And I added the property tabla to it and know is working just fine.

我向它添加了属性 tabla 并且知道它工作得很好。