In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "mybatis conditional statement with array parameters processing", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in-depth, together to study and learn "mybatis conditional statements with array parameters processing" it!
Catalogue
Mybatis conditional statement with array parameters
Here's an example.
What needs to be explained here is the collection attribute in the foreach tag.
Mybatis multi-parameter passing (including arrays)
Mapper interface
Configuration of mapper.xml
Mybatis conditional statement with array parameters
For example, when writing sql statements in mybatis, we may encounter keywords such as in, and we know that in needs to be followed by a structure like 'axiomanagrical statement. It is like an array, but it is wrapped in parentheses (), and the parameters are separated by commas.
Because the array parameters are variables, it is not feasible to concatenate them directly.
Here we need a tag foreach, which can have attributes such as item,collection,open,close,separator, which represent variables, variable sets, start symbols, end symbols, and delimiters.
Here's an example.
Define the return object
Write sql statements
Select * from users where id in # {id,jdbcType=BIGINT}
Define the dao method:
Package com.xxx.springmybatis.dao;import java.util.List;import com.xxx.springmybatis.domain.User;public interface UserMapper {User getById (Integer id); User getByName (String name); void save (User user); List findByIds (List ids);}
In the above example, the corresponding set of users is queried through the id array. The user ID we pass in will eventually form the condition of where id in (3jin4) by splicing in sql.
As shown below, if you want to query a user whose id is 3Power4, you can see what the splicing looks like in the printed sql statement:
What needs to be explained here is the collection attribute in the foreach tag.
1. List collection. By default, list is used to represent input parameters, array, and array is used as input parameter by default.
2. If the parameter passed in is an object and the object User has a property List ids, then ids is used as the input parameter.
3. If the input parameter is specified by the annotation @ Param ("xx") where the API is declared, the default parameter list,array is invalid. In this case, you need to specify xx as the input parameter.
In this example, because you are using the List collection to pass in, list is used as the key for the collection input parameter by default.
Mybatis passes multiple parameters (including arrays) mapper interface public void batchDelete (@ Param (value = "activityId") Integer activityId, @ Param (value = "userIds") Integer [] userIds) Configuration of mapper.xml delete from T_ACT_USERS where ACTIVITY_ID = # {activityId} and USER_ID in # {item} Thank you for reading. This is the content of "how to deal with array parameters in mybatis conditional statements". After the study of this article, I believe you have a deeper understanding of how to deal with array parameters in mybatis conditional statements. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.