解決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