数据库
首页 > 数据库> > boolean数据在mysql数据库中的保存方式

boolean数据在mysql数据库中的保存方式

作者:互联网

5. boolean数据的保存

Mysql中没有内置boolean类型,所以我们需要用到其他类型的数据来进行存储boolean数据。一般可以用TINYINT,Mysql的范围是(-127, 128),而sql的范围是(0, 255)。
有一点需要记住,TINYINT保存的是Byte类型的数据,也就是在实体类中,我们需要用Byte数据类型来获取我们存在sql中的数据:

// 获取sql中的数据
private Byte hasNameByte;

// 再手动转换为boolean
private boolean hasName;
hasName = hasNameByte == 1 ? true : false;
// 这里true等于多少可以自己定义

当然,使用TINYINT还有一个好处就是,我们在使用selectOneRadio时,可以直接对value进行存和取,只需要设置好itemValue所对应的值,我们就不需要像转换boolean再多一个步骤。

<!--  plain: true 为圆形, false 为方块    -->
<!--  styleClass="#{selectBox.valid ? '' : 'custom-error-highlight'}"> 
      这个表示当用户没有选择任何一个选项时,且required为true时去提交表单,
      提交无法成功且这个组件会高光错误提示-->
<p:selectOneRadio 
    id="selectBox"
    value="#{javaBean.hasName}"
    required="true"
    requiredMessage="please select at least one option"
    plain="true" 
    binding="#{selectBox}"
    styleClass="#{selectBox.valid ? '' : 'custom-error-highlight'}"> 
        <f:selectItem itemLabel="Yes" itemValue="1" />
        <f:selectItem itemLabel="No" itemValue="2" />
        <f:selectItem itemLabel="N/A" itemValue="3" />
</p:selectOneRadio>

标签:数据,数据库,mysql,TINYINT,boolean,sql,Byte,true
来源: https://blog.csdn.net/weixin_46112273/article/details/120647989