What is the difference between "#" and "$" in ibatis?

Source: Internet
Author: User

Recently, I have been working on a problem for a long time. I would like to make a small summary of this problem:

We often use the # symbol when using ibatis.

For example:

SQL code
  1. Select *
    From Member where id = # ID #

Then, we will pass a value to the ID variable in the program, ibatis will automatically convert # ID # Into the content we pass.

But I recently encountered a strange problem. When I delete or modify them in batches, the SQL statement becomes invalid.

The SQL statement is as follows:

SQL code
  1. Update
    User set flag = # flag # Where
    ID in (# ID #)
  2. Delete
    From User
    Where ID in (# ID #)

The passed IDs are 1, 2, and 3. However, the data is not modified.

The original cause is this # problem. Because ibatis treats the variable in the middle of "#" as a string by default. In this way, such SQL

SQL code
  1. Update
    User set flag = '1' where
    ID in ('1, 2, 3 ')
  2. Delete
    From User
    Where ID in ('1, 2, 3 ')

Such a SQL database will not be executed. So we have to bypass ibatis?

Actually, no. ibatis actually provides another method, that is, using $ to pass the value. You use $ to enclose your variables. ibatis does not process the variables and directly generates the SQL statement you want.

SQL code
  1. Update
    User set flag = $ flag $ where
    ID in ($ ID $)
  2. Update
    User set flag = 1 where
    ID in (1, 2, 3)
  3. Delete
    From User
    Where ID in ($ ID $)
  4. Delete
    From User
    Where ID in (1, 2, 3)
In other words, the variable $ in the middle is directly replaced with a value.
# Will be replaced based on the type of the Variable
For example, when the articletitle type is string and the value is "title ",
$ Articletitle $ = title
# Articletitle # = 'title'

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.