第一种:value
public enum JdbcDriverTypeEnum {
MYSQL("com.mysql.cj.jdbc.Driver"),
ORACLE("oracle.jdbc.driver.OracleDriver");
private String value;
JdbcDriverTypeEnum(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
第二种:name-value
public enum JdbcDriverTypeEnum {
MYSQL("MYSQL","com.mysql.cj.jdbc.Driver");
private String name;
private String value;
JdbcDriverTypeEnum(String name, String value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public static String getDriverType(String name) {
for (JdbcDriverTypeEnum type : JdbcDriverTypeEnum.values()) {
if (name.equals(type.getName())) {
return type.value;
}
}
return null;
}
public static JdbcDriverTypeEnum getType(String name) {
for (JdbcDriverTypeEnum enums : JdbcDriverTypeEnum.values()) {
if (enums.name.equals(name)) {
return enums;
}
}
return null;
}
}
第三种:相同业务不同处理
public enum JdbcDriverTypeEnum {
INPUT {
@Override
public String getSql(String leftParam, String rightParam) {
if (StringUtils.isBlank(rightParam)) {
return "";
}
return leftParam + " = '" + rightParam + "'";
}
},
LIKE {
@Override
public String getSql(String leftParam, String rightParam) {
if (StringUtils.isBlank(rightParam)) {
return "";
}
String sql = leftParam + " like " + "'%" + rightParam + "%'";
return sql;
}
},
COMBO_BOX {
@Override
public String getSql(String leftParam, String rightParam) {
if (StringUtils.isBlank(rightParam)) {
return "";
}
String sql = leftParam + " = '" + rightParam + "'";
return sql;
}
},
MULTI_COMBO_BOX {
@Override
public String getSql(String leftParam, String rightParam) {
if (StringUtils.isBlank(rightParam)) {
return "";
}
rightParam = StringUtils.replace(rightParam, "####", "','");
rightParam = "('" + rightParam + "')";
String sql = leftParam + " in " + rightParam;
return sql;
}
},
INPUT_COMBO_BOX {
@Override
public String getSql(String leftParam, String rightParam) {
if (StringUtils.isBlank(rightParam)) {
return "";
}
String sql = leftParam + " = '" + rightParam + "'";
return sql;
}
},
INPUT_MULTI_BOX {
@Override
public String getSql(String leftParam, String rightParam) {
if (StringUtils.isBlank(rightParam)) {
return "";
}
rightParam = StringUtils.replace(rightParam, "####", "','");
rightParam = "('" + rightParam + "')";
String sql = leftParam + " in " + rightParam;
return sql;
}
};
public abstract String getSql(String leftParam, String rightParam);
}
class test{
public static void main(String[] args) {
String type = "INPUT";
JdbcDriverTypeEnum controlTypeEnum = JdbcDriverTypeEnum.valueOf(type);
controlTypeEnum.getSql("","");
}
}