mysql有个字段是bit,只存储1和0,是二进制存储,那么在java的dao层如何映射成boolean呢
1
2
3
4
5
6
7
8
9
10
|
@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的时候,做转换
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
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); 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; } |
注意这个
1
2
3
|
else if (filedCls == boolean . class || filedCls == Boolean. class ) { columnValueObj = resultSet.getBoolean(columnName); } |
resultSet.getBoolean(columnName) 就是转换的方法,具体实现为
1
2
3
4
5
6
7
8
9
10
11
|
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中Boolean与字符串或者数字1和0的转换实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。