Mybatis fuzzy query implementation method

Mybatis fuzzy query implementation method

Mybatis fuzzy query implementation method

The reverse assistant of mybatis is really easy to use and can save a lot of time in writing regular SQL statements, but it cannot automatically generate fuzzy query statements. However, fuzzy query is indispensable in development, so it is necessary to manually write fuzzy query function for mapper.

Here we first clarify the difference between # and $ in MyBatis/Ibatis:

1. # Treat all incoming data as a string, and add double quotes to the automatically incoming data. For example: order by #user_id#, if the value passed in is 111, then the value parsed into SQL is order by "111", if the value passed in is id, then the parsed SQL is order by "id".

2. $ displays the incoming data directly in sql. For example: order by $user_id$, if the value passed in is 111, then the value parsed into SQL is order by user_id, if the value passed in is id, then the parsed SQL is order by id.

3. #This method can prevent SQL injection to a great extent.

4. The $ method cannot prevent SQL injection.

5. The $ method is generally used to pass in database objects, such as table names.

6. Generally, don’t use $ when you can use #.

ps: When using mybatis, you will also encounter the usage of <![CDATA[]]>. The statements within this symbol will not be processed as strings, but directly as SQL statements, such as executing a stored procedure.

Our requirement is to perform a fuzzy query on a User. The idea is to compare all basic fields (username, gender, etc.) with the passed key (keyword).

1. Encode UserMapper.xml

 <select id="queryUserByKey" parameterType="string"
    resultType="com.lqr.pojo.User">
    select * from user where uid like CONCAT('%',#{key},'%')
    or username like CONCAT('%',#{key},'%')
    or realname like CONCAT('%',#{key},'%')
    or identification like CONCAT('%',#{key},'%')
    or email like CONCAT('%',#{key},'%')
  </select>

2. Coding UserMapper.java

List<User> queryUserByKey(String key);

The above is the scenario I encountered during development. I will continue to record other uses of mybatis fuzzy queries in the future.

If you have any questions, please leave a message or come to the community to discuss. Thank you for reading and I hope it can help you. Thank you for your support of this site!

You may also be interested in:
  • Detailed explanation of the universal Map and fuzzy query writing in Mybatis
  • Several ways to implement fuzzy query in MyBatis
  • Detailed explanation of Mybatis mapper file writing
  • MyBatis fuzzy query mapper.xml writing explanation

<<:  React tsx generates random verification code

>>:  Solution to no Chinese input method in Ubuntu

Recommend

Why is IE6 used by the most people?

First and foremost, I am a web designer. To be mor...

Selection and thinking of MySQL data backup method

Table of contents 1. rsync, cp copy files 2. sele...

Vue implements QR code scanning function (with style)

need: Use vue to realize QR code scanning; Plugin...

Recommended 20 best free English handwriting fonts

Jellyka BeesAntique Handwriting [ank]* Jellyka Cut...

How to view and configure password expiration on Linux

With the right settings, you can force Linux user...

Sample code for converting video using ffmpeg command line

Before starting the main text of this article, yo...

How many times will multiple setStates in React be called?

Table of contents 1. Two setState, how many times...

Docker Swarm from deployment to basic operations

About Docker Swarm Docker Swarm consists of two p...

Detailed tutorial on how to quickly install Zookeeper in Docker

Docker Quickly Install Zookeeper I haven't us...

JavaScript event capture bubbling and capture details

Table of contents 1. Event Flow 1. Concept 2. DOM...

Docker deployment of Kafka and Spring Kafka implementation

This article mainly introduces the deployment of ...

How to solve jQuery conflict problem

In front-end development, $ is a function in jQue...

How to use Greek letters in HTML pages

Greek letters are a very commonly used series of ...

WHMCS V7.4.2 Graphical Installation Tutorial

1. Introduction WHMCS provides an all-in-one solu...

How to install and configure the supervisor daemon under centos7

Newbie, record it yourself 1. Install supervisor....