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


0.前言

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

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.

2.要注意下面这个提示

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

3.解决方案

  • tinyInt(1) 只用来代表Boolean含义的字段,且0代表False,1代表True。如果要存储多个数值,则定义为tinyInt(N), N>1。例如 tinyInt(2)
  • JDBC的URL增加 tinyInt1isBit=false参数,注意参数名区分大小写,否则不生效
    jdbc:mysql://${ucmha.proxy1_2.host}/${db.mysql.db}?tinyInt1isBit=false
    


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM