Browse Source

Merge pull request #5301 from eye-gu/Fix-5266

fix mapping for null value enum
qmdx 1 year ago
parent
commit
57b838b412

+ 3 - 3
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/handlers/MybatisEnumTypeHandler.java

@@ -116,7 +116,7 @@ public class MybatisEnumTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E
     @Override
     public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
         Object value = rs.getObject(columnName, this.propertyType);
-        if (null == value && rs.wasNull()) {
+        if (null == value || rs.wasNull()) {
             return null;
         }
         return this.valueOf(value);
@@ -125,7 +125,7 @@ public class MybatisEnumTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E
     @Override
     public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
         Object value = rs.getObject(columnIndex, this.propertyType);
-        if (null == value && rs.wasNull()) {
+        if (null == value || rs.wasNull()) {
             return null;
         }
         return this.valueOf(value);
@@ -134,7 +134,7 @@ public class MybatisEnumTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E
     @Override
     public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
         Object value = cs.getObject(columnIndex, this.propertyType);
-        if (null == value && cs.wasNull()) {
+        if (null == value || cs.wasNull()) {
             return null;
         }
         return this.valueOf(value);

+ 7 - 0
mybatis-plus-extension/src/test/java/com/baomidou/mybatisplus/test/handlers/MybatisEnumTypeHandlerTest.java

@@ -129,6 +129,13 @@ public class MybatisEnumTypeHandlerTest extends BaseTypeHandlerTest {
         assertNull(GRADE_ENUM_ENUM_TYPE_HANDLER.getResult(callableStatement, 6));
     }
 
+    @Test
+    public void testNullButReturnZero() throws Exception {
+        when(callableStatement.getObject(1, Integer.class)).thenReturn(0);
+        when(callableStatement.wasNull()).thenReturn(true);
+        assertNull(SEX_ENUM_ENUM_TYPE_HANDLER.getResult(callableStatement, 1));
+    }
+
     @Getter
     @AllArgsConstructor
     enum SexEnum implements IEnum<Integer> {