Java項(xiàng)目中如何將Boolean與字符串進(jìn)行轉(zhuǎn)換

Java項(xiàng)目中如何將Boolean與字符串進(jìn)行轉(zhuǎn)換?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

站在用戶的角度思考問題,與客戶深入溝通,找到金臺(tái)網(wǎng)站設(shè)計(jì)與金臺(tái)網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:做網(wǎng)站、網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、申請(qǐng)域名、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋金臺(tái)地區(qū)。

MySQL有個(gè)字段是bit,只存儲(chǔ)1和0,是二進(jìn)制存儲(chǔ),那么在java的dao層如何映射成boolean呢

@Column(name="is_standard")
private boolean isStandard;

public void setIsStandard(boolean isStandard){
 this.isStandard = isStandard;
}

public boolean getIsStandard(){
 return isStandard;
}

其實(shí)就是在底層dao做反射的時(shí)候,先判斷字段(比如isStandard)的字段類型是否為boolean,如果是,則在查出數(shù)據(jù)庫字段bit is_standard的時(shí)候,做轉(zhuǎn)換

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;
 }

注意這個(gè)

else if(filedCls == boolean.class || filedCls == Boolean.class) {
 columnValueObj = resultSet.getBoolean(columnName);
}

resultSet.getBoolean(columnName) 就是轉(zhuǎn)換的方法,具體實(shí)現(xiàn)為

 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項(xiàng)目中如何將Boolean與字符串進(jìn)行轉(zhuǎn)換

核心原理就是根據(jù)字符串/數(shù)字做對(duì)比,如果是1,0就返回true/false,其它的直接返回false或拋出異常,字符串如果是[true, yes, y, on, 1]就返回true,如果是[false, no, n, off, 0]就返回false

看完上述內(nèi)容,你們掌握J(rèn)ava項(xiàng)目中如何將Boolean與字符串進(jìn)行轉(zhuǎn)換的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

名稱欄目:Java項(xiàng)目中如何將Boolean與字符串進(jìn)行轉(zhuǎn)換
本文地址:http://muchs.cn/article2/jpghic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、關(guān)鍵詞優(yōu)化ChatGPT、App開發(fā)、品牌網(wǎng)站制作、建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)