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 mybatis foreach attribute and its three uses

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

Share

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

This article mainly analyzes the mybatis foreach attribute and its three kinds of use of the relevant knowledge points, the content is detailed and easy to understand, the operation details are reasonable, has a certain reference value. If you are interested, you might as well follow the editor to take a look, and follow the editor to learn more about the mybatis foreach attribute and its three uses.

Introduction to foreach attribute

Foreach is used to iterate over the parameters passed in.

Its properties are introduced as follows

Collection: represents the data type of the parameter passed in. This parameter is required. To do the foreach object, as the input parameter, the List object defaults to list instead of the key, the array object has array instead of the key, and the Map object has no default key. Of course, you can use @ Param ("keyName") to set the key as an input parameter. After setting keyName, list,array will fail. In addition to the case of input parameters, there is also a time when a field is used as a parameter object. For example: if User has an attribute List ids. If the input parameter is a User object, then this collection = "ids" if User has an attribute Ids ids; where Ids is an object and Ids has an attribute List id; input is a User object, then collection = "ids.id"

If a single parameter is passed and the parameter type is a List, the value of the collection attribute is list

If a single parameter is passed and the parameter type is an array array, the attribute value of collection is array

If there are multiple parameters passed in, we need to encapsulate them into a Map. Of course, a single parameter can also be encapsulated as a map.

Item: the concrete object in the body of the loop. Supports point path access to attributes, such as item.age,item.info.details. Description: in list and array is one of the objects, in map is value, this parameter is required. (it is an alias for each element when iterating)

Index: in list and arrays, index is the ordinal number of the element; in map, index is the key of the element.

Open: indicates what the statement starts with

Close: indicates what the statement ends with

Separator: indicates what symbol is used as a separator between each iteration

After introducing the attributes, let's move on to practice. First, let's take a look at a simple to explosive watch (table name: t_test_foreach).

Single parameter is of type array

Test class

/ / ids = {1 ids 3} public List testFindByArray (int [] ids) throws Exception {SqlSession sqlSession = getSession (). OpenSession (); userList = sqlSession.selectList (NameSpace + ".findByArray", ids); System.out.println (userList.toString ()); sqlSession.close (); return userList;}

Mapper.xml

-- > SELECT id, `name` FROM t_test_foreach WHERE id IN # {ids}

Output result

DEBUG-= > Preparing: SELECT id, `name` FROM t_test_foreach WHERE id IN

DEBUG-= > Parameters: 1 (Integer), 2 (Integer), 3 (Integer)

DEBUG-Preparing: SELECT id, `name` FROM t_test_foreach WHERE id IN

DEBUG-= > Parameters: 1 (Integer), 3 (Integer), 5 (Integer)

DEBUG-Preparing: SELECT id, `name` FROM t_test_foreach WHERE id IN

DEBUG-= > Parameters: 2 (Integer), 4 (Integer)

DEBUG-Preparing: UPDATE t_test_foreach SET `name` =? WHERE id IN (?)

DEBUG-= > Parameters: updated (String), 1 (Integer), 2 (Integer), 4 (Integer)

This is the end of the introduction on "mybatis foreach attribute and its three uses". More related content can be searched for previous articles, hoping to help you answer questions and questions, please support the website!

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