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 to pass in multiple parameters in MyBatis

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

Share

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

This article mainly introduces how to introduce MyBatis multi-parameters, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

1. Make use of the order in which the parameters appear

Using mapper.xml

Select * from user where id = # {param1} and username = # {param2}

Use mybatis annotation (this is recommended when the sql statement is simple)

@ Select ("select * from user where id = # {arg0} and username = # {arg1}") User MutiParameter (int id,String username)

The order of occurrence of parameters can be arg0. To argn, you can also use param1 to param n settings to indicate the position of the first parameter to the nth parameter

In the example, arg0 and param1 represent the first parameter, id,arg1 and param2 represent the second parameter.

two。 Using annotations requires the use of mybatis @ Param annotations

Using mapper.xml

Select * from user where id = # {id} and username = # {username}

Use mybatis annotation (this is recommended when the sql statement is simple)

@ Select ("select * from user where id = # {id} and username = # {username}") User MutiParameter (@ Param ("id") int id,@Param ("username") String username)

The @ Param annotation in front of the interface method @ Param ("content") needs to be consistent with the content in # {content} in our sql statement to find it.

3. Using map requires the key of map to be consistent with the content in # {content}.

Using mapper.xml

Select * from user where id = # {id} and username = # {username}

Use mybatis annotation (this is recommended when the sql statement is simple)

@ Select ("select * from user where id = # {id} and username = # {username}") User MutiParameter (Map params)

Testing method

@ Test public void testMutiParameter () {AuthorityMapper mapper = session.getMapper (AuthorityMapper.class); Map params = new HashMap (); params.put ("id", 2); params.put ("username", "admin"); mapper.MutiParameter (params);} 4. Encapsulate the parameters in Javabean

Using mapper.xml

Select * from user where id = # {id} and username = # {username}

Use mybatis annotation (this is recommended when the sql statement is simple)

@ Select ("select * from user where id = # {id} and username = # {username}") User MutiParameter (User user)

Testing method

@ Test public void testMutiParameter () {AuthorityMapper mapper = session.getMapper (AuthorityMapper.class); User user = new User (); user.setId (2); user.setUsername ("admin"); mapper.MutiParameter (user);}

The field that requires User is the same as the content in # {content} of the query.

Thank you for reading this article carefully. I hope the article "how to introduce multiple parameters of MyBatis" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you 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: 291

*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