The difference between $ and # in MyBatis

Source: Internet
Author: User

Use $ and # in MyBatis to refer to the data passed in,

The difference between the two is:

1. Use # to handle incoming values as strings, and automatically add single quotes to the resulting SQL statement

2. Use $ to directly reference the value passed in (the resulting SQL statement does not enclose a single quotation mark)

3. Using $ easy to cause SQL injection

The problem I encountered:

When using the PostgreSQL database to store data, error:

Error updating database. Cause:org.postgresql.util.PSQLException: Error: The type of the field "message" is JSON, but the type of the expression is character varying
Recommendation: You need to rewrite or convert an expression

The reason for the error is that #{message} is used to reference the message, and the type of the argument becomes a string (the actual type in the database should be JSON)

Use ${message} to resolve

The difference between $ and # in MyBatis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.