Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does iBATIS use $and #

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report