Java 枚举定义

第一种:value

public enum JdbcDriverTypeEnum {
    /**
     * mysql
     */
    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

/**
 * driver-class-name
 */
public enum JdbcDriverTypeEnum {
	/**
	 * mysql
	 */
	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;
	}

	//传入name找value值
	public static String getDriverType(String name) {
		for (JdbcDriverTypeEnum type : JdbcDriverTypeEnum.values()) {
			if (name.equals(type.getName())) {
				return type.value;
			}
		}
		return null;
	}

	//传入name获取enums
	public static JdbcDriverTypeEnum getType(String name) {
		for (JdbcDriverTypeEnum enums : JdbcDriverTypeEnum.values()) {
			if (enums.name.equals(name)) {
				return enums;
			}
		}
		return null;
	}
}

第三种:相同业务不同处理

/**
 * driver-class-name
 */
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";//这里作为类别 和 ControlTypeEnum 中 INPUT 对应
		JdbcDriverTypeEnum controlTypeEnum = JdbcDriverTypeEnum.valueOf(type);//通过“INPUT”定位到对应的枚举方法 “INPUT”
		controlTypeEnum.getSql("","");//传参相同 type不同 业务处理逻辑不同 结果也不相同
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值