-1

I use mysql type tinyint(1) for rabbitmqFlag field.BUT,not as excepted,actually,the value -1 or >0 will map to true.other will map to false. Usually,we think 0 is false,other is true.Why?

@Data
public class User implements Serializable {

    private Long id;
    private String name;
    private Integer age;

    private Boolean rabbitmqFlag;

}

@Mapper
public interface UserMapper {

    @Select("SELECT * FROM user1 WHERE rabbitmq_flag = 0")
    List<User> findUnSend();

}

binglong li
  • 79
  • 1
  • 8

1 Answers1

0

MyBatis does not process the numeric value.
It just returns the result of ResultSet#getBoolean.

According to the driver's source code comment, it's for compatibility with the ODBC driver, etc..

public Boolean createFromLong(long l) {
  // Goes back to ODBC driver compatibility, and VB/Automation Languages/COM, where in Windows "-1" can mean true as well.
  return (l == -1 || l > 0);
}
ave
  • 3,244
  • 2
  • 14
  • 20
  • so the boolean field value actually depends on jdbc driver.should we better not use boolean field? – binglong li Mar 11 '20 at 08:59
  • Or, you can write a custom boolean type handler which behaves as you want (e.g. call `ResultSet#getInt()` internally instead of `getBoolean()`). See the [doc](https://mybatis.org/mybatis-3/configuration.html#typeHandlers) for the details. – ave Mar 11 '20 at 09:05