Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize Fuzzy query mapper.xml in MyBatis

Shulou Source: shulou.com Published: 2022-06-03 08:24:43 10月26日 Update

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!

Tags: Query mode element parameter statement content multiple character attribute suggestion data database article security value interest writing function variable string Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MariaDB NVidia Microsoft macOS