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
- 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
- Update
User set flag = # flag # Where
ID in (# ID #)
- 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
- Update
User set flag = '1' where
ID in ('1, 2, 3 ')
- 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
- Update
User set flag = $ flag $ where
ID in ($ ID $)
- Update
User set flag = 1 where
ID in (1, 2, 3)
- Delete
From User
Where ID in ($ ID $)
- 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'