Java項目中如何將Boolean與字符串進(jìn)行轉(zhuǎn)換?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
站在用戶的角度思考問題,與客戶深入溝通,找到金臺網(wǎng)站設(shè)計與金臺網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:做網(wǎng)站、網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、申請域名、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋金臺地區(qū)。
MySQL有個字段是bit,只存儲1和0,是二進(jìn)制存儲,那么在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,如果是,則在查出數(shù)據(jù)庫字段bit is_standard的時候,做轉(zhuǎn)換
private ListpopulateData(ResultSet resultSet, Class clazz) throws Exception { List dataList = new ArrayList (); List fieldList = MappingAnnotationUtil.getAllFields(clazz); ResultSetMetaData rsmd = resultSet.getMetaData(); int columnsCount = rsmd.getColumnCount(); List columnNameList = new ArrayList (); 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) 就是轉(zhuǎn)換的方法,具體實現(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); } }
核心原理就是根據(jù)字符串/數(shù)字做對比,如果是1,0就返回true/false,其它的直接返回false或拋出異常,字符串如果是[true, yes, y, on, 1]就返回true,如果是[false, no, n, off, 0]就返回false
看完上述內(nèi)容,你們掌握J(rèn)ava項目中如何將Boolean與字符串進(jìn)行轉(zhuǎn)換的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!