Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Registered type handler should be used for anonymous enum #2957

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,13 @@ private Map<JdbcType, TypeHandler<?>> getJdbcHandlerMap(Type type) {
if (type instanceof Class) {
Class<?> clazz = (Class<?>) type;
if (Enum.class.isAssignableFrom(clazz)) {
Class<?> enumClass = clazz.isAnonymousClass() ? clazz.getSuperclass() : clazz;
jdbcHandlerMap = getJdbcHandlerMapForEnumInterfaces(enumClass, enumClass);
if (clazz.isAnonymousClass()) {
return getJdbcHandlerMap(clazz.getSuperclass());
}
jdbcHandlerMap = getJdbcHandlerMapForEnumInterfaces(clazz, clazz);
if (jdbcHandlerMap == null) {
register(enumClass, getInstance(enumClass, defaultEnumTypeHandler));
return typeHandlerMap.get(enumClass);
register(clazz, getInstance(clazz, defaultEnumTypeHandler));
return typeHandlerMap.get(clazz);
}
} else {
jdbcHandlerMap = getJdbcHandlerMapForSuperclass(clazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.sql.PreparedStatement;
import java.sql.SQLException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void shouldGetAUser() {
Mapper mapper = sqlSession.getMapper(Mapper.class);
User user = mapper.getUser(1);
Assertions.assertEquals("User1", user.getName());
Assertions.assertEquals(Mood.GOOD, user.getMood());
}
}

Expand All @@ -60,7 +61,15 @@ void shouldInsertAUser() {
user.setId(2);
user.setName("User2");
user.setCur(Currency.Dollar);
user.setMood(Mood.BAD);
mapper.insertUser(user);
sqlSession.commit();
}
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
User user = mapper.getUser(2);
Assertions.assertEquals("User2", user.getName());
Assertions.assertEquals(Mood.BAD, user.getMood());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2009-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.ibatis.submitted.enum_with_method;

public enum Mood {
GOOD(1) {
@Override
public String getMessage() {
return "Yeehaw";
}
},
BAD(2) {
@Override
public String getMessage() {
return "whatevs";
}
};

private int value;

private Mood(int value) {
this.value = value;
}

public int getValue() {
return value;
}

public static Mood fromValue(int i) {
for (Mood t : values()) {
if (t.value == i) {
return t;
}
}
throw new IllegalArgumentException("Unknown value for Mood: " + i);
}

public abstract String getMessage();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2009-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.ibatis.submitted.enum_with_method;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

public class MoodTypeTypeHandler extends BaseTypeHandler<Mood> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, Mood parameter, JdbcType jdbcType) throws SQLException {
ps.setInt(i, parameter.getValue());
}

@Override
public Mood getNullableResult(ResultSet rs, String columnName) throws SQLException {
return Mood.fromValue(rs.getInt(columnName));
}

@Override
public Mood getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return Mood.fromValue(rs.getInt(columnIndex));
}

@Override
public Mood getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return Mood.fromValue(cs.getInt(columnIndex));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class User {
private Integer id;
private String name;
private Currency cur;
private Mood mood;

public Integer getId() {
return id;
Expand All @@ -44,4 +45,12 @@ public Currency getCur() {
public void setCur(Currency cur) {
this.cur = cur;
}

public Mood getMood() {
return mood;
}

public void setMood(Mood mood) {
this.mood = mood;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--
-- Copyright 2009-2022 the original author or authors.
-- Copyright 2009-2023 the original author or authors.
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
Expand All @@ -19,7 +19,8 @@ drop table users if exists;
create table users (
id int,
name varchar(20),
cur varchar(20)
cur varchar(20),
mood int
);

insert into users (id, name, cur) values(1, 'User1', 'RMB');
insert into users (id, name, cur, mood) values(1, 'User1', 'RMB', 1);
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2009-2022 the original author or authors.
Copyright 2009-2023 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,7 +31,7 @@
<!-- without parameterType="org.apache.ibatis.submitted.enum_with_method.User" -->
<insert id="insertUser">
insert into users
values (#{id}, #{name}, #{cur})
values (#{id}, #{name}, #{cur}, #{mood})
</insert>

</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@

<configuration>

<typeHandlers>
<typeHandler
handler="org.apache.ibatis.submitted.enum_with_method.MoodTypeTypeHandler" />
</typeHandlers>

<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
Expand Down