解决mysql数据库tinyInt(1) 转换为java的Boolean解决方案

JAVA数据类型 和 MYSQL的数据类型转换,要注意tinyInt 类型,且存储长度为1的情况。

mysql文档给出的解释

java.lang.Boolean if the configuration property tinyInt1isBit is set to true (the default) and the storage size is 1, or java.lang.Integer if not.

要注意下面这个提示

The ResultSet.getObject() method uses the type conversions between MySQL and Java types, following the JDBC specification where appropriate. The values returned by ResultSetMetaData.GetColumnTypeName()and ResultSetMetaData.GetColumnClassName() are shown in the table below. For more information on the JDBC types, see the reference on the java.sql.Types class.


文档地址:https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-type-conversions.html

image.png

解决方案

1、tinyInt(1) 只用来代表Boolean含义的字段,且0代表False,1代表True。如果要存储多个数值,则定义为tinyInt(N), N>1。例如 tinyInt(2)
2、JDBC的URL增加 tinyInt1isBit=false参数,注意参数名区分大小写,否则不生效

jdbc:mysql://${ucmha.proxy1_2.host}/${db.mysql.db}?tinyInt1isBit=false
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容