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

What is the method of passing String into mybatis in query

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

The main content of this article is to explain "what is the method of mybatis in query into String", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the method of introducing mybatis in queries into String"?

Mybatis in query is passed into String

When using mybaits for in query, pass in String, such as 1Jing 2jue 3, and find that the result of the query is not what we want

This is because # {} automatically adds double quotation marks after compilation, that is, it becomes in ("1pd2p3").

If you want to get the results we want, you can use ${} to compile the in like this (1pd2)

For example, query the total number of multiple ringtones in the ringtone library

Select count (ring_no) from progandresmanage_ringinfo where valid_day > now () and ring_no in (${ringNo})

If the input parameter is List or Array, you can simply use foreach

For example

Select count (ring_no) from progandresmanage_ringinfo where valid_day > now () and ring_no in # {item, jdbcType=VARCHAR} mybatis in query pass string parameters

The in operator in sql allows us to specify multiple values to match in the where clause.

Syntax:

SELECT column_name (s) FROM table_nameWHERE column_name IN (value1,value2,...)

In mybatis, you can specify parameters to the in operator by passing in an array or container (array, list, set, map) through the foreach tag.

Problem: to match the data of field org_id in OR001, OR002, OR004 from the org table, org_id is a field of type string.

The general method is to pass in a list object orgIdList containing "OR001", "OR002" and "OR004" in mapper.java, and in xml:

SELECT * from org where org_id in # {orgId}

If you want multiple values as matching parameters of in in an object of type String, orgs, and you want to pass them directly through String, there are two ways to do this.

1. In xml, use ${orgs} to make the whole String part of sql.

SELECT * from org where org_id in (${orgs})

This kind of writing needs to pay attention to whether the whole sql is grammatical after the contents of the string orgs are spelled. According to the above requirements and sql writing, the value of orgs as part of the sql is required to be "'OR001','OR002','OR004'".

There is a risk of sql injection when parameters are used as query statements directly as part of sql, and some projects may restrict developers from using this method of writing across the board.

2. In the foreach tag of xml, when passing the collection attribute, convert the string to an array with the split function.

SELECT * from org where org_id in # {orgId}

The value of the orgs string passed in here can be "OR001,OR002,OR004". When you call the split function, you can set "," grid, and you can directly split out three strings as elements of the array.

At this point, I believe you have a deeper understanding of "what is the method of mybatis in query passing into String". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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