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 solve the failure problem with integer type 0 of mybatis

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge about "how to solve the failure problem with integer type 0 of mybatis". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

integer Data of type 0 if test invalid

mybatis where dynamic judgment statement if test encounters tinyint type 0 data invalidation

Found a pit of mybatis, there is a payment table, filter paid/unpaid data by status, payment status is represented by status field, status=0 means unpaid, status=1 means paid, and status type is Integer. When status=1 is selected, the paid data list can be filtered successfully. However, when status=0 is selected, all the data found are unpaid and paid data.

At this point, I was a little confused. After debugging, I traced it layer by layer. When status=0, the where condition in sql constructed by mybatis did not splice the status field, but when status=1, you can see that there is a status field in sql.

After looking up the data later, we found that in the if test condition in mybatis, the integer type field will treat the value of 0 as false, because the parameter integer=0 will be defaulted to ''(empty string), that is, the judgment result of status=0 is false, so the unpaid condition will never appear, and the data found is the data of all states.

The following is an error statement:

and status=#{status} and created_dt = ]]> #{createdDtBegin, jdbcType=TIMESTAMP} and created_dt #{createdDtEnd, jdbcType=TIMESTAMP} solution

Change to **and status=#{status, jdbcType=TINYINT}

That is, put status != 'Remove

Or there is another approach, namely:

If status is integer, or if status is integer,

mybatis if Determine Integer type of pit

When the attribute type in POJO is Integer, 0 is passed in. At this time, the condition cannot always be satisfied in the if judgment of xxxMapper.xml, which leads to invalid query conditions. This is because mybatis has carried out special processing for 0 of integer type, treating it as ''. The following judgment can be made:

payStatus is of type Integer and a.pay_status = #{payStatus}"mybatis integer type 0 invalid problem how to solve" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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