欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java中Boolean与字符串或者数字1和0的转换实例

程序员文章站 2023-12-11 08:17:10
mysql有个字段是bit,只存储1和0,是二进制存储,那么在java的dao层如何映射成boolean呢 @column(name="is_standard...

mysql有个字段是bit,只存储1和0,是二进制存储,那么在java的dao层如何映射成boolean呢

@column(name="is_standard")
private boolean isstandard;

public void setisstandard(boolean isstandard){
 this.isstandard = isstandard;
}

public boolean getisstandard(){
 return isstandard;
}

其实就是在底层dao做反射的时候,先判断字段(比如isstandard)的字段类型是否为boolean,如果是,则在查出数据库字段bit is_standard的时候,做转换

private list<t> populatedata(resultset resultset, class<t> clazz) throws exception {
 list<t> datalist = new arraylist<t>();
 list<field> fieldlist = mappingannotationutil.getallfields(clazz);

 resultsetmetadata rsmd = resultset.getmetadata();
 int columnscount = rsmd.getcolumncount();
 list<string> columnnamelist = new arraylist<string>();
 for(int i = 0; i < columnscount; i++){
  columnnamelist.add(rsmd.getcolumnlabel(i+1).tolowercase());
 }

 while (resultset.next()) { 
  t bean = clazz.newinstance();
  for(field f : fieldlist) {
  string columnname = mappingannotationutil.getdbcloumnname(clazz, f).tolowercase();
  if(columnnamelist.contains(columnname)) {
   object columnvalueobj = null;
   class<?> filedcls = f.gettype();
   
   if(filedcls == int.class || filedcls == integer.class) {
   columnvalueobj = resultset.getint(columnname);
   } else if(filedcls == string.class) {
   columnvalueobj = resultset.getstring(columnname);
   } else if(filedcls == boolean.class || filedcls == boolean.class) {
   columnvalueobj = resultset.getboolean(columnname);
   } else if(filedcls == byte.class || filedcls == byte.class) {
   columnvalueobj = resultset.getbyte(columnname);
   } else if(filedcls == short.class || filedcls == short.class) {
   columnvalueobj = resultset.getshort(columnname);
   } else if(filedcls == long.class || filedcls == long.class) {
   columnvalueobj = resultset.getlong(columnname);
   } else if(filedcls == float.class || filedcls == float.class) {
   columnvalueobj = resultset.getfloat(columnname);
   } else if(filedcls == double.class || filedcls == double.class) {
   columnvalueobj = resultset.getdouble(columnname);
   } else if(filedcls == bigdecimal.class) {
   columnvalueobj = resultset.getbigdecimal(columnname);
   } 
   
   else {
   columnvalueobj = resultset.getobject(columnname);
   }
   
   if (columnvalueobj != null) {
   method settermethod = mappingannotationutil.getsettermethod(clazz, f);
   settermethod.invoke(bean, new object[] { columnvalueobj });
   }
  }
  }
  datalist.add(bean);
 }
 return datalist;
 }

注意这个

else if(filedcls == boolean.class || filedcls == boolean.class) {
 columnvalueobj = resultset.getboolean(columnname);
} 

resultset.getboolean(columnname) 就是转换的方法,具体实现为

 for (int i = 0; i < truestrings.length; ++i) {
  if (truestrings[i].equals(stringvalue)) {
   return type.cast(boolean.true);
  }
  }

  for (int i = 0; i < falsestrings.length; ++i) {
  if (falsestrings[i].equals(stringvalue)) {
   return type.cast(boolean.false);
  }
  }

Java中Boolean与字符串或者数字1和0的转换实例

核心原理就是根据字符串/数字做对比,如果是1,0就返回true/false,其它的直接返回false或抛出异常,字符串如果是[true, yes, y, on, 1]就返回true,如果是[false, no, n, off, 0]就返回false

以上这篇java中boolean与字符串或者数字1和0的转换实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。