background
Data table definition
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;
There are three tinyint
fields.
The Java interface is defined as:
List<Map> selectListBySelective(Map<String, Object> map);
Mybatis mapper file definition:
<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>
It can be seen that tinyint
the processing methods for the three different fields are different.
For Spring Boot + mybatis reasons, a application.properties
new configuration information is added to the configuration file : logging.level.com.aaa.mapper=debug
you can print and output SQL statements to the log console.
The SQL statement is as follows:
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
Get the SQL statement to DataGrip to execute, there is no problem:
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 |
But the return data obtained by postman calling the interface is:
{
"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",
}
]
}
analysis
Question 1
The inserttime field is of the datetime type, and the value becomes timestamp, unless date_format(tt.updatetime, '%Y-%m-%d %H:%i:%s') as updatetime
it is handled as the same.
Question 1
When the return value is Map type (ie resultType="Map"
), the data of the tinyint(1)
type in the data table (ie [1, 0]
) will be automatically converted into boolean type data (ie [true/false]
) by mybatis . Refer to the tinyint(1) data in Mybatis to automatically convert the data to boolean for processing .
solution:
- Use to
ifnull(column, 0)
process the field - Add parameters in jdbcUrl:
tinyInt1isBit=false
(default is true) - Avoid using tinyint type fields with a length of 1 to store data in numeric format.
In the author's problem scenario, only the first solution is recommended. That is, through ifnull processing. Therefore, you can see that it taskStatus
returns 1 as expected, but mqSwitch
still returns true.
Question 2
isactive
Fields are also used ifnull(tt.isactive, 0)加以处理
, but there is no subsequent as
expression part. The interface return is actually:
"ifnull(tt": {
"isactive, 0)": 1
},
Remarks
The mybatis used in this article is mybatis-spring-boot-starter, version:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
<!--升级到这个版本,还是有问题-->
<!--<version>2.1.3</version>-->
</dependency>
Corresponding mybatis version:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
<!--<version>3.5.5</version>-->
</dependency>
reference
The tinyint(1) data in Mybatis is automatically converted to boolean processing