Java中Boolean与字符串或者数字1和0的转换实例
时间:2020-10-18 13:56:01|栏目:JAVA代码|点击: 次
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); } }
核心原理就是根据字符串/数字做对比,如果是1,0就返回true/false,其它的直接返回false或抛出异常,字符串如果是[true, yes, y, on, 1]就返回true,如果是[false, no, n, off, 0]就返回false
上一篇:Java的Spring框架下RMI与quartz的调用方法
栏 目:JAVA代码
下一篇:java生成图片进行套打功能
本文标题:Java中Boolean与字符串或者数字1和0的转换实例
本文地址:http://www.codeinn.net/misctech/13243.html
阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机