策略设计模式:解决多重if判断
//数据库
CREATE TABLE `payment_channel` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
`channel_name` varchar(32) NOT NULL COMMENT '渠道名称',
`channel_id` varchar(32) NOT NULL COMMENT '渠道ID',
`strategy_bean_id` varchar(255) DEFAULT NULL COMMENT '策略执行beanid',
PRIMARY KEY (`id`,`channel_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COMMENT='支付渠道 ';
-- ----------------------------
-- Records of payment_channel
-- ----------------------------
INSERT INTO `payment_channel` VALUES ('1', '支付宝渠道', 'ali_pay', 'aliPayStrategy');
INSERT INTO `payment_channel` VALUES ('2', '微信支付渠道', 'weChat_pay', 'weChatPayStrategy');
//实体类
@Data
public class PaymentChannelEntity {
/** ID */
private Integer id;
/** 渠道名称 */
private String channelName;
/** 渠道ID */
private String channelId;
/**
* 策略执行beanId
*/
private String strategyBeanId;
}
//dao层
public interface PaymentChannelMapper {
@Select("SELECT id, channel_name as channelName, channel_id as channelId, strategy_bean_id as strategyBeanId FROM payment_channel where channel_id=#{payType}")
PaymentChannelEntity getPaymentChannel(String payType);
}
public interface PayStrategy {
/**
* 共同的方法
*/
String toPayHtml();
}
@Component
public class AliPayStrategy implements PayStrategy {
@Override
public String toPayHtml() {
String result="支付宝支付。。。。。。。。。。";
return result;
}
}
@ Component
public class WeChatPayStrategy implements PayStrategy {
@Override
public String toPayHtml() {
String result="微信支付。。。。。。。。。。";
return result;
}
}
@ Component
public class PayContextStrategy {
@Autowired
private PaymentChannelMapper paymentChannelMapper;
/**
* 获取具体策略实现
*
* @return
*/
public String toPayHtml(String payCode) {
if (StringUtils.isEmpty(payCode)) {
return "payCode不能为空!.....";
}
// 1.查询数据库获取具体策略实现
PaymentChannelEntity paymentChannel = paymentChannelMapper.getPaymentChannel(payCode);
if (paymentChannel == null) {
return "没有查询支付渠道";
}
// 获取spring注入的bean的id
String strategyBeanId = paymentChannel.getStrategyBeanId();
System.err.println("strategyBeanId========"+strategyBeanId);
if (StringUtils.isEmpty(strategyBeanId)) {
return "数据库没有配置strategyBeanId";
}
PayStrategy payStrategy = SpringUtils.getBean(strategyBeanId, PayStrategy.class);
return payStrategy.toPayHtml();
}
}
@ RestController
public class PayController {
@Autowired
private PayContextStrategy payContextStrategy;
@GetMapping("/toPayHtml")
public String toPayHtml(String payType){
String toPayHtml = payContextStrategy.toPayHtml(payType);
return toPayHtml;
}
}
//工具类
@ Component
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
//获取applicationContext
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
//通过name获取 Bean.
public static Object getBean(String name){
return getApplicationContext().getBean(name);
}
//通过class获取Bean.
public static <T> T getBean(Class<T> clazz){
return getApplicationContext().getBean(clazz);
}
//通过name,以及Clazz返回指定的Bean
public static <T> T getBean(String name,Class<T> clazz){
return getApplicationContext().getBean(name, clazz);
}
//application.yml
###服务启动端口号
server:
port: 8080
spring:
###数据库相关连接
datasource:
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/factory?useUnicode=true&characterEncoding=utf-8&useSSL=false