MyBatis the problem of judging the condition equal to

Source: Internet
Author: User

In the use of mybatis operation of the database when I believe that many people use, when in the judgment null, greater than, greater than, less than, less than equals, not equal to the estimated many are used, easier to achieve, here is omitted, but only the judgment condition is equal to When it is estimated that quite a lot of people meet the pit, I hang on this issue for almost a day, so the experiment summarized and briefly recorded;

When the mybatis determines that the condition is equal, the constant needs to be added. ToString () to convert, This method is stable and recommended , such as:

<!--correct, stable, recommended use--><if test= "Newsimage! = null and Newsimage = = ' 1 '. toString ()" ><! [Cdata[and Len (newsimage) > 0]]></if>

Where the judge newsimage = = ' 1 ' When the man considered success, but in fact is unsuccessful, need to change newsimage = = ' 1 '. toString () can succeed, the reason is not detailed into the study, according to the actual use of speculation should be "equals" In Java is a more complex problem, the "equals" may be the variable address is equal, or the variable value content is equal, in the XML file in the simple = = after mybatis processing can not determine what type of "equal", so add . toString () Do the cast operation, MyBatis know is the value of the comparison of content, of course, success; Note that this constant is not limited to numbers, and for letters, such as ' Y ' also need to be added . toString () to succeed, as follows:

<!--correct, stable, recommended to use--><if test= "Newsimage! = null and Newsimage = = ' Y '. toString ()" ><! [Cdata[and Len (newsimage) > 0]]></if>

Is it OK to add . toString () to the variable? This is wrong, at least actually in the MyBatis version used (Mybatis-3.2.5.jar,Mybatis-spring-1.2.1.jar) is not possible, later versions do not know, this should be in the past MyBatis, it affects the conversion operation, so there is an error, the following is wrong:

<!--error--><if test= "Newsimage! = null and newsimage.tostring () = = ' Y '" ><! [Cdata[and Len (newsimage) > 0]]></if>

Since is the value content comparison, we naturally associate to Java's equals, equalsignorecase keyword, use this can? Actually tested, sometimes successful, sometimes unsuccessful (probably with my machine and the reason I use the Java environment), very unstable, not recommended to use , so the following is not stable:

<!--sometimes successful and sometimes unsuccessful, it is not recommended to use--><if test= "Newsimage! = null and newsimage.equal (' y ')" ><! [Cdata[and Len (newsimage) > 0]]></if>

Encountered the pit, the whole almost bitter half a day, hehe, briefly summary and record here.

MyBatis the problem of judging the condition equal to

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.