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 realize Fuzzy query mapper.xml in MyBatis

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

Share

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

This article mainly introduces how to achieve fuzzy query mapper.xml in MyBatis, which is very detailed and has a certain reference value. Interested friends must read it!

The Writing method of MyBatis Fuzzy query mapper.xml

Fuzzy query statement is not recommended to use ${}, or recommended to use the MyBatis built-in # {} mode, # {} is preloaded, relatively safe, ${} mode can be used but there is a risk of SQL injection!

1. Direct transmission of parameters

In the controller class

String id = "%" + id + "%"; String name = "%" + name + "%"; dao.selectByIdAndName (id,name)

In the mapper.xml mapping file

Select * from table wherer id=# {id} or name like # {name} 2. Statements against the MySQL database

Using the concat () function, it can concatenate multiple strings into a single character

Select * from table where name like concat ('%', # {name},'%') 3. For all databases, use MyBatis's bind element public xx selectByLike (@ Param ("_ name") String name); select * from table where name like # {user_name}

Where _ name is the passed parameter, and the value attribute of the bind element concatenates the passed parameter with the'% 'user_name assigned to the name attribute, and then you can use the variable user_name in the select statement.

The bind element also supports passing multiple parameters

Public xx selectByLike (@ Param ("_ name") String name, @ Param ("_ note") String note) Select * from table where name like # {user_name} and note like # {user_note} three common ways of fuzzy query in xml select from users name like "%" {name} "%" and phone like concat (concat ("%", # {phone}) "%") and email like # {pattern} above are all the contents of the article "how to implement fuzzy query mapper.xml in MyBatis" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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