Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem that mybatis if test is not an empty string and is not null?

Shulou Source: shulou.com Published: 2022-06-01 06:05:24 10月31日 Update

This article mainly introduces "how to solve the problem that mybatis if test is not an empty string and is not null". In daily operation, I believe many people have doubts about how to solve the problem that mybatis if test is not an empty string and is not null. The editor consulted all kinds of materials and sorted out a simple and useful method of operation. I hope it will be helpful for you to answer the problem that mybatis if test is not an empty string and is not null. Next, please follow the editor to study!

If test is not an empty string and is not null if test in mybatis

When judging that it is not an empty string and null, a sql syntax error is reported

Xml file:

WHERE enable = 1 AND (mac_id = # {keyword}) OR (user_id = # {keyword})

The error in the postman tool is as follows:

Reason:

A case error occurred in if judgment, which does not conform to the mybatis grammar specification.

In mybatis, if test syntax

1 judge that it is not null

WHERE 1 # 1 AND user_id = # {keyword}

2 determine that it is not an empty string

WHERE 1 # 1 AND user_id = # {keyword}

3 determine that it is not a null and is not an empty string

WHERE 1 AND user_id = # {keyword} Controller layer-> corresponding xml file

When we get the list, we usually make an accurate or fuzzy query according to the keywords, and then make a non-empty or non-null judgment on the keywords. As follows:

Controller layer request parameters keyword request parameters default value Dao layer Xml file @ RequestParam (value = "keyword", required = false) String keywordnullif test= "keyword! = null" @ RequestParam (value = "keyword", required = false DefaultValue = "") String keyword empty string if test= "keyword! =''" / unclear default value can be if test= "keyword! = null and keyword! =''" Parameter default value Dao layer Xml file data obtained from other sources or keywords unclear default value if test= "keyword! = null and keyword! =''" mybatis treats 0 as an empty string

In development, it is common to judge whether the parameter is null or an empty string in sqlmap.

When the parameter is Int type 0, myBatis automatically defines 0 as an empty string ""

We add one more judgment where the input parameter is 0, a.AUTHORIZE_AMOUNT = # {authorizeAmount,jdbcType=VARCHAR} so far, the study on "how to solve the problem that mybatis if test is not an empty string and is not null" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Null characters questions parameters files learning keys keywords grammar clarity more errors help utility precision next reason place size letters tools Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Shulou Tech Info Huawei Docker MySQL