mybatis笔记之tinyint自动转型为boolean

背景

数据表定义

create table timed_task (
    id                  bigint unsigned auto_increment comment 'PK' primary key,
    task_status         tinyint(1) default 0                 not null comment '任务状态:1启用,2禁用',
    mq_switch           tinyint(1) default 0                 not null comment '是否发送消息至MQ:1发送,0不发送',
    isactive 			tinyint(1) default 1 				 not null comment '逻辑删除',
    inserttime          datetime   default CURRENT_TIMESTAMP not null comment '插入时间',
	insertby            varchar(100)                         null comment '创建人',
    updatetime          datetime   default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
    updateby            varchar(100)                         null comment '更新人'
) comment '定时任务配置表' collate = utf8mb4_unicode_ci;

其中有三个tinyint字段。
Java接口定义为:

List<Map> selectListBySelective(Map<String, Object> map);

mybatis mapper文件定义:

<select id="selectListBySelective" parameterType="Map" resultType="Map">
    select
    tt.id,
    ifnull(tt.task_status, 0) as taskStatus,
    tt.mq_switch as mqSwitch,
    tt.inserttime,
    tt.insertby,
    date_format(tt.updatetime,'%Y-%m-%d %H:%i:%s') as updatetime,
    tt.updateby,
    ifnull(tt.isactive, 0)
    from timed_task tt
    where tt.isactive = 1
    order by tt.updatetime desc
select>

可见,对于三个不同的tinyint字段的处理方式不一样。
对于Spring Boot + mybatis 原因,在配置文件application.properties里面新增一条配置信息:logging.level.com.aaa.mapper=debug即可实现打印输出SQL语句到日志控制台。
SQL语句如下:

select tt.id,
       ifnull(tt.task_status, 0)                       as taskStatus,
       tt.mq_switch                                    as mqSwitch,
       tt.inserttime,
       tt.insertby,
       date_format(tt.updatetime, '%Y-%m-%d %H:%i:%s') as updatetime,
       tt.updateby,
       ifnull(tt.isactive, 0)
from ppdai_feiyu.timed_task tt
where tt.isactive = 1
order by tt.updatetime desc

拿到SQL语句去DataGrip执行,没有问题:

id taskStatus mqSwitch inserttime insertby updatetime updateby ifnull(tt.isactive, 0)
3 1 1 2020-08-26 10:49:52 awesome 2020-08-26 10:49:52 awesome 1

但是postman调用接口得到的返回数据是:

{
     
  "list": [
    {
     
      "id": 3,
      "ifnull(tt": {
     
        "isactive, 0)": 1
      },
      "inserttime": "1598410192000",
      "mqSwitch": true,
      "taskStatus": 1,
      "updateby": "awesome",
      "insertby": "awesome",      
      "updatetime": "2020-08-26 10:49:52",
    }
  ]
}

分析

问题1

inserttime字段是datetime类型,取值变成timestamp,除非如date_format(tt.updatetime, '%Y-%m-%d %H:%i:%s') as updatetime一样处理一下。

问题1

在返回值为Map类型(即resultType="Map")时,数据表里的tinyint(1)类型的数据(即[1, 0]),被mybatis会自动把转换成boolean类型数据(即[true/false]),参考Mybatis中tinyint(1)数据自动转化为boolean处理。
解决方案:

  1. 使用ifnull(column, 0)处理该字段
  2. 在jdbcUrl添加参数:tinyInt1isBit=false(默认为true)
  3. 避免使用长度为1的tinyint类型字段存储数字格式的数据。

在笔者的问题场景下,只推荐第一种解决方案。即通过ifnull处理。因此,可以看到taskStatus如期返回1,而mqSwitch还是返回true。

问题2

isactive字段,也采用ifnull(tt.isactive, 0)加以处理,但是没有后面的as表达式部分。接口返回居然是:

"ifnull(tt": {
     
  "isactive, 0)": 1
},

mybatis笔记之tinyint自动转型为boolean_第1张图片

备注

本文使用的mybatis为mybatis-spring-boot-starter,版本:

<dependency>
    <groupId>org.mybatis.spring.bootgroupId>
    <artifactId>mybatis-spring-boot-starterartifactId>
    <version>2.0.1version>
    
    
dependency>

对应的mybatis版本:

<dependency>
    <groupId>org.mybatisgroupId>
    <artifactId>mybatisartifactId>
    <version>3.5.1version>
    
dependency>

参考

Mybatis中tinyint(1)数据自动转化为boolean处理

你可能感兴趣的:(mybatis,MySQL)