在项目中遇到一个巨坑,程序没问题,从mysql查询到的tinyint数据中莫名其妙的变成了布尔类型,导致后续出错。这种细节性的问题,只有遇到时才会关注到,记录备忘。
下面是详细解释:
在项目中有一个字段为user_type,长度设置为1,表示注册用户和非注册用户,是注册用户为 1,否则为 2。因为查询语句是使用的Jfinal相关函数,查找到其中的获取数据类型字段程序,如下所示:
在图中光标处断点发现,通过getObeject后,类型变为boolean。而其中tinyint长度为4的,经过转化后没有问题,确定是getObeject对长度为1的处理有问题。经过查找资料,发现网上确实有这个问题的介绍。链接如下:
http://www.cnblogs.com/joeylee/p/3878223.html
解决方法 :
Tinyint(1) 就只用来保存 boolean(或bool)值,“只有0和1”,不要保存其他的值,如果要保存多的值,就用Tinyint(4)
附:
查阅mysql官方文档仅找到如下描述:
11.10 Using Data Types from Other Database Engines
To facilitate the use of code written for SQL implementations from other vendors, MySQL maps data types as shown in the following table. These mappings make it easier to import table definitions from other database systems into MySQL.
Other Vendor Type MySQL Type BOOL
TINYINT
BOOLEAN
TINYINT
CHARACTER VARYING(
M
)VARCHAR(
M
)FIXED
DECIMAL
FLOAT4
FLOAT
FLOAT8
DOUBLE
INT1
TINYINT
INT2
SMALLINT
INT3
MEDIUMINT
INT4
INT
INT8
BIGINT
LONG VARBINARY
MEDIUMBLOB
LONG VARCHAR
MEDIUMTEXT
LONG
MEDIUMTEXT
MIDDLEINT
MEDIUMINT
NUMERIC
DECIMAL
Data type mapping occurs at table creation time, after which the original type specifications are discarded. If you create a table with types used by other vendors and then issue a
DESCRIBE
statement, MySQL reports the table structure using the equivalent MySQL types. For example:tbl_name
mysql> CREATE TABLE t (a BOOL, b FLOAT8, c LONG VARCHAR, d NUMERIC); Query OK, 0 rows affected (0.00 sec) mysql> DESCRIBE t; +-------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------+------+-----+---------+-------+ | a | tinyint(1) | YES | | NULL | | | b | double | YES | | NULL | | | c | mediumtext | YES | | NULL | | | d | decimal(10,0) | YES | | NULL | | +-------+---------------+------+-----+---------+-------+ 4 rows in set (0.01 sec)
转载自:
https://blog.csdn.net/yefengzhichen/article/details/50956369