Last active
November 23, 2021 04:56
-
-
Save umbum/5410363d4be0637a127d9b3a81955ee0 to your computer and use it in GitHub Desktop.
MyBatis EnumUsingDbCodeTypeHandler using interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.CallableStatement; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.util.Arrays; | |
import org.apache.ibatis.type.BaseTypeHandler; | |
import org.apache.ibatis.type.JdbcType; | |
import org.apache.ibatis.type.MappedTypes; | |
@MappedTypes(EnumUsingDbCode.class) | |
public class EnumUsingDbCodeTypeHandler<E extends Enum<E> & EnumUsingDbCode> extends BaseTypeHandler<E> { | |
private final Class<E> type; | |
private final E[] enumConstants; | |
public EnumUsingDbCodeTypeHandler(Class<E> type) { | |
if (type == null) { | |
throw new IllegalArgumentException("Type argument cannot be null"); | |
} | |
this.type = type; | |
this.enumConstants = type.getEnumConstants(); | |
if (!type.isInterface() && this.enumConstants == null) { | |
throw new IllegalArgumentException(type.getSimpleName() + " does not represent an enum type."); | |
} | |
} | |
@Override | |
public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException { | |
ps.setString(i, parameter.getDbCode()); | |
} | |
@Override | |
public E getNullableResult(ResultSet rs, String columnName) throws SQLException { | |
String dbCode = rs.getString(columnName); | |
return rs.wasNull() ? null : getEnum(dbCode); | |
} | |
@Override | |
public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException { | |
String dbCode = rs.getString(columnIndex); | |
return rs.wasNull() ? null : getEnum(dbCode); | |
} | |
@Override | |
public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { | |
String dbCode = cs.getString(columnIndex); | |
return cs.wasNull() ? null : getEnum(dbCode); | |
} | |
private E getEnum(String dbCode) { | |
return Arrays.stream(enumConstants) | |
.filter(e -> e.getDbCode().equals(dbCode)) | |
.findFirst() | |
.orElseThrow(() -> new IllegalArgumentException("Cannot convert " + dbCode + " to " + type.getSimpleName())); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface EnumUsingDbCode { | |
String getDbCode(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Getter | |
@RequiredArgsConstructor | |
public enum ExampleEnum implements EnumUsingDbCode { | |
EX1, | |
EX2 | |
; | |
@Override | |
public String getDbCode() { | |
return this.name().toLowerCase(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!