1

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

### 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 «$1»

I get the error here, in the 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.

1
  • 2
    There is a semicolon at the end of the SQL statement. The error may be caused by this semicolon, you can remove it and try again. Commented Feb 27, 2017 at 13:29

1 Answer 1

5

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.

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.