Skip to main content
Hint

This article is generated by AI translation.

Enum type handlers

dbVisitor automatically uses EnumTypeHandler for enums, so no extra setup is usually needed.

Object mapping contains userType enum
public class User {
private UserType userType;

// getters and setters omitted
}
No extra handling needed
// Query result
jdbc.queryForList("select * from users", User.class);

// Query argument
User userInfo= ...
jdbc.queryForList("select * from users where user_type = #{userType}", userInfo);

Explicit declaration

On an argument
// Query argument
User userInfo= ...
jdbc.queryForList("select * from users where user_type = #{userType, typeHandler=net.<省略>.EnumTypeHandler}", userInfo);
  • Full class name: net.hasor.dbvisitor.types.handler.string.EnumTypeHandler.
On object mapping
public class User {
@Column(typeHandler = net.hasor.dbvisitor.types.handler.string.EnumTypeHandler)
private UserType userType;

// getters and setters omitted
}

Map numeric values to enums

To map numeric values from the database to an enum, implement net.hasor.dbvisitor.types.handler.string.EnumOfValue.

public enum LicenseEnum implements EnumOfValue<LicenseEnum> {
Private(0),
AGPLv3(1),
GPLv3(2),;

private final int type;

LicenseEnum(int type) {
this.type = type;
}

public int codeValue() {
return this.type;
}

public LicenseEnum valueOfCode(int codeValue) {
for (LicenseEnum item : LicenseEnum.values()) {
if (item.getType() == codeValue) {
return item;
}
}
return null;
}
}

Map codes to enums

By default the enum name is used for storage. If that does not work for you, implement net.hasor.dbvisitor.types.handler.string.EnumOfCode instead of writing a custom handler.

public enum LicenseEnum implements EnumOfCode<LicenseEnum> {
Private("Private"),
AGPLv3("AGPLv3"),
GPLv3("GPLv3"),;

private final String type;

LicenseEnum(String type) {
this.type = type;
}

public String codeName() {
return this.type;
}

public LicenseEnum valueOfCode(String codeValue) {
for (LicenseEnum item : LicenseEnum.values()) {
if (item.codeName().equalsIgnoreCase(codeValue)) {
return item;
}
}
return null;
}
}