How does iBATIS use $and #
This article mainly shows you "how to use $and # in iBATIS". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn how to use $and # in iBATIS.
We often use the symbol # when we use iBATIS.
For example:
Sql code
Select * from member where id = # id#
Then, we will pass a value to the variable id in the program, and iBATIS will automatically transfer # id# to what we pass.
But I have a strange problem recently. When I deleted or modified in batches, the SQL failed.
SQL is as follows:
Sql code
Update user set flag=#flag# where id in (# id#) delete from user where id in (# id#)
The id passed on is 1pm / 2pm / 3. But there is no change in the data.
Later, I searched for a long time, and the original reason was this # problem. Because iBATIS defaults to treating the variable in the middle of "#" as a string. In this way, this kind of SQL will appear.
Sql code
Update user set flag='1' where id in ('1pyrrine 2pyr3') delete from user where id in ('1pyrrine 2pence3')
Of course, such a SQL database will not be executed. So do we have to bypass iBATIS?
No, iBATIS actually provides another way, which is to use $to pass values. You use $to enclose your variable, and iBATIS will not do any processing for this variable, but directly generate the SQL you want.
SQL code
Update user set flag=$flag$ where id in ($id$) update user set flag=1 where id in (1gime 2je 3) delete from user where id in ($id$) delete from user where id in (1m 2pr 3)
You can also use ibatis's iterate to solve:
SQL:
< select id= test "parameterClass=" java.util.List "resultClass=" test.Roadline "> select * from SYS_ROAD_LINE_INFO where ROAD_LINE_NO in < iterate open=" ("close=") "conjunction=", "> # value [] # < / iterate > < / select > List list = new ArrayList (); list.add (" aaa "); list.add (" bbb ") List rsList = sqlMap.queryForList ("roadline.test", list)
Generated SQL:
Select * from SYS_ROAD_LINE_INFO where ROAD_LINE_NO in
The variable in the middle of $is directly replaced with the value.
# will be replaced according to the type of variable
For example, when the type of articleTitle is string and the value is "title"
$articleTitle$ = title
# articleTitle# = 'title'
If the name of a field does not have a standard #, an error will be reported in the < select... > select name# from reader where id=#id#... < / select > statement. I have seen someone ask this question, saying that it is name#### but still cannot solve the query for fields with #. There must be a solution. For example, you can pass this field to ibatis as a parameter. Then enclose the variable in $$.
These are all the contents of the article "how iBATIS uses $and #". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!