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

Mybatis receives a comma-separated string batch query method

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

Share

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

This article introduces the knowledge of "mybatis receives comma-separated string batch query method". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Receive a comma-separated string query AND msd.supplier_id NOT IN # {item} AND msd.supplier_id! = # {supplierIds}

Paste it directly, the upper part implements a circular query, and the following is a supplement for a single character (because there is no comma for a single character)

How to convert comma-separated strings and List to each other

If a programmer wants to achieve a function, there are two ways to go. One is to implement it yourself, the other is to call other people's implementation, and others' implementation is the so-called API. And in most cases, many "others" have achieved this function. Programmers have to choose between them. In most cases, programmers will know which to use and which to use first. In the end, in the actual project, the same function will be called to the API. I saw this in the company's project. In fact, there is nothing wrong with it. I believe many projects are like this. It is impossible to require programmers to use the same method. Programmers may have different likes and dislikes. In order to make programmers happy and free to program, let him go! Because programmers are most productive when they feel free and happy.

Don't be ridiculous. Back to the point, after all these different implementation methods or API really have no distinction between high and low? Take I encounter this comma-separated string to List as an example to explore:

Note: the following code is not guaranteed to run and may need to be modified slightly.

Convert comma-separated strings to List

Method 1: use the Arrays class of JDK

String str = "a dint bjorc"; List result = Arrays.asList (str.split (","))

Method 2: use the Splitter of Guava

String str = "a, b, c"; List result = Splitter.on (","). TrimResults (). SplitToList (str)

Method 3: take advantage of Apache Commons's StringUtils (just split)

String str = "a str bjorc"; List result = Arrays.asList (StringUtils.split (str, ","))

Method 4: use the StringUtils of Spring Framework

String str = "a dint b str c"; List str = Arrays.asList (StringUtils.commaDelimitedListToStringArray (str)); convert List to a comma delimiter

Method 1: use JDK (there seems to be no good method and needs to be implemented step by step)

NA

Method 2: use the Joiner of Guava

List list = new ArrayList (); list.add ("a"); list.add ("b"); list.add ("c"); String str = Joiner.on (",") .join (list)

Method 3: use the StringUtils of Apache Commons

List list = new ArrayList (); list.add ("a"); list.add ("b"); list.add ("c"); String str = StringUtils.join (list.toArray (), ",")

Method 4: use the StringUtils of Spring Framework

List list = new ArrayList (); list.add ("a"); list.add ("b"); list.add ("c"); String str = StringUtils.collectionToDelimitedString (list, ",")

In comparison, my view is that the Guava library is more flexible and widely applicable. If Guava is not introduced into the project, add it.

/ / concatenate all strings public String getAllIdByUserMobile (List userMobile) throws Exception {StringBuilder userMobileIdString = new StringBuilder (); / / concatenate the string userMobile productId if (userMobile.size ())

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