1
2
3
4
5
6
7
8
shardingsphere默认使用等jdk版本是1.7,所以并没有支持jdk1.8+的LocalDateTime等特性

我已经在fork上扩展了这个特性,并将JDK的功能升级到jdk1.8。

主要验证了支持 Mybatis5+

4.0.0-RC2-1.8
https://github.com/lonyee1989/incubator-shardingsphere/tree/4.0.0-RC2-1.8

直接上实现代码

fork代码

1、package org.apache.shardingsphere.shardingjdbc.jdbc.core.resultset;

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
54
55
56
57
/**
* Convert value via expected class type.
*
* @param value original value
* @param convertType expected class type
* @return converted value
*/
public static Object convertValue(final Object value, final Class<?> convertType) {
if (null == value) {
return convertNullValue(convertType);
}
if (value.getClass() == convertType) {
return value;
}
if (LocalDateTime.class.equals(convertType)) {
return convertLocalDateTimeValue(value, convertType);
}
if (LocalDate.class.equals(convertType)) {
return convertLocalDateValue(value, convertType);
}
if (LocalTime.class.equals(convertType)) {
return convertLocalTimeValue(value, convertType);
}


if (value instanceof Number) {
return convertNumberValue(value, convertType);
}
if (value instanceof Date) {
return convertDateValue(value, convertType);
}
if (value instanceof byte[]) {
return convertByteArrayValue(value, convertType);
}
if (String.class.equals(convertType)) {
return value.toString();
} else {
return value;
}
}

......

private static Object convertLocalDateTimeValue(final Object value, final Class<?> convertType) {
Timestamp timestamp = (Timestamp) value;
return timestamp.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
}

private static Object convertLocalDateValue(final Object value, final Class<?> convertType) {
Timestamp timestamp = (Timestamp) value;
return timestamp.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}

private static Object convertLocalTimeValue(final Object value, final Class<?> convertType) {
Timestamp timestamp = (Timestamp) value;
return timestamp.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
}

2、package org.apache.shardingsphere.shardingjdbc.jdbc.core.resultset;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Override
public <T> T getObject(final int columnIndex, final Class<T> type) throws SQLException {
if (LocalDateTime.class.equals(type) || LocalDate.class.equals(type) || LocalTime.class.equals(type)) {
return (T) ResultSetUtil.convertValue(mergeResultSet.getValue(columnIndex, Timestamp.class), type);
}
throw new SQLFeatureNotSupportedException("getObject with type");
}

@Override
public <T> T getObject(final String columnLabel, final Class<T> type) throws SQLException {
if (LocalDateTime.class.equals(type) || LocalDate.class.equals(type) || LocalTime.class.equals(type)) {
return (T) ResultSetUtil.convertValue(mergeResultSet.getValue(columnLabel, Timestamp.class), type);
}
throw new SQLFeatureNotSupportedException("getObject with type");
}

3、package org.apache.shardingsphere.shardingjdbc.jdbc.core.resultset;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Override
public <T> T getObject(final int columnIndex, final Class<T> type) throws SQLException {
if (LocalDateTime.class.equals(type) || LocalDate.class.equals(type) || LocalTime.class.equals(type)) {
return (T) ResultSetUtil.convertValue(resultSet.getValue(columnIndex, Timestamp.class), type);
}
throw new SQLFeatureNotSupportedException("getObject with type");
}

@Override
public <T> T getObject(final String columnLabel, final Class<T> type) throws SQLException {
if (LocalDateTime.class.equals(type) || LocalDate.class.equals(type) || LocalTime.class.equals(type)) {
return (T) ResultSetUtil.convertValue(resultSet.getValue(columnLabel, Timestamp.class), type);
}
throw new SQLFeatureNotSupportedException("getObject with type");
}

4、package org.apache.shardingsphere.shardingjdbc.jdbc.unsupported;

1
2
3
4
5
6
7
8
9
@Override
public <T> T getObject(final int columnIndex, final Class<T> type) throws SQLException {
throw new SQLFeatureNotSupportedException("getObject with type");
}

@Override
public <T> T getObject(final String columnLabel, final Class<T> type) throws SQLException {
throw new SQLFeatureNotSupportedException("getObject with type");
}