桥接模式就是把一个大类拆分成多个小类,不需要过多层次的继承
在使用对象的时候,传入数据源接口
在数据源接口可以看到,有不同的数据源实现类,只需要在jdbcTemplate传入不同的实现类
就可以操作对应数据源的功能,执行增删改查
在每个数据源内部都会自己去连接数据库,使用的时候 不需要关心你内部到底是怎么实现的
我们在使用easyexcel的时候,在监听器中不能使用@Autowired 这样注入 会是null
需要通过构造注入的方式传进来,但是每一个导入模版都需要一个监听类,太过麻烦了
那么我们使用桥接模式来改造下
项目使用springcloud alibaba
4.0.0
com.example
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot
1.8
UTF-8
UTF-8
2.3.7.RELEASE
2.2.2.RELEASE
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
com.baomidou
mybatis-plus-boot-starter
3.5.1
mysql
mysql-connector-java
8.0.16
com.alibaba
druid
1.1.14
org.projectlombok
lombok
true
com.alibaba
easyexcel
3.1.0
com.alibaba
fastjson
1.2.47
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
com.alibaba.cloud
spring-cloud-alibaba-dependencies
${spring-cloud-alibaba.version}
pom
import
org.apache.maven.plugins
maven-compiler-plugin
3.8.1
1.8
UTF-8
org.springframework.boot
spring-boot-maven-plugin
2.3.7.RELEASE
com.example.demo.DemoApplication
repackage
repackage
# 应用名称
spring.application.name=kucun
server.port=8089
# Nacos认证信息
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
# Nacos 服务发现与注册配置,其中子属性 server-addr 指定 Nacos 服务器主机和端口
spring.cloud.nacos.discovery.server-addr=localhost:8848
# 注册到 nacos 的指定 namespace,默认为 public
spring.cloud.nacos.discovery.namespace=public
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/dmg?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis-plus.mapper-locations=classpath:mappers/*.xml
# 控制台打印sql语句
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
CREATE TABLE `chu_chai` (
`id` bigint NOT NULL AUTO_INCREMENT,
`cc_name` varchar(50) DEFAULT NULL COMMENT '出差名称',
`cc_day` int DEFAULT NULL COMMENT '出差天数',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE `qingjia` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL COMMENT '请假名称',
`day` int DEFAULT NULL COMMENT '请假天数',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='请假表';
package com.example.demo.entity;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
@Data
public class CcData {
@ExcelProperty("出差名称")
private String ccName;
@ExcelProperty("出差天数")
private Integer ccDay;
}
package com.example.demo.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("chu_chai")
public class ChuChai {
//主键
@TableId(type = IdType.AUTO)
private Long id;
//出差名称
private String ccName;
//出差天数
private Integer ccDay;
}
package com.example.demo.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@TableName("qingjia")
@Data
public class QingJia {
//主键
@TableId(type = IdType.AUTO)
private Long id;
//请假名称
private String name;
//请假天数
private Integer day;
}
package com.example.demo.entity;
import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
@Data
public class QjData {
@ExcelProperty("请假名称")
private String name;
@ExcelProperty("请假天数")
private Integer day;
}
package com.example.demo.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.ChuChai;
public interface ChuChaiDao extends BaseMapper {
}
package com.example.demo.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.QingJia;
public interface QingJiaDao extends BaseMapper {
}
CommonService是公共的上传接口,其他实现类实现这个接口,在传入监听器类的时候,传入对应的实现类就可以了
package com.example.demo.service;
import java.util.List;
public interface CommonService {
/**
*
* 添加
* @param
* @return
* @throws Exception
*/
public void add(List
package com.example.demo.service.impl;
import com.example.demo.dao.ChuChaiDao;
import com.example.demo.dao.QingJiaDao;
import com.example.demo.entity.CcData;
import com.example.demo.entity.ChuChai;
import com.example.demo.entity.QingJia;
import com.example.demo.service.CommonService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CcServiceImpl implements CommonService {
@Autowired
private ChuChaiDao chuChaiDao;
@Override
public void add(List
package com.example.demo.service.impl;
import com.example.demo.dao.QingJiaDao;
import com.example.demo.entity.CcData;
import com.example.demo.entity.ChuChai;
import com.example.demo.entity.QingJia;
import com.example.demo.entity.QjData;
import com.example.demo.service.CommonService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class QjServiceImpl implements CommonService {
@Autowired
private QingJiaDao qingJiaDao;
@Override
public void add(List
package com.example.demo.listener;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON;
import com.example.demo.service.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
*
* 公共的excel导入监听器
* @param
* @return
* @throws Exception
*/
@Slf4j
public class CommonListener extends AnalysisEventListener
往监听器类传参只需要传入对应的接口就行
package com.example.demo.controller;
import com.alibaba.excel.EasyExcel;
import com.example.demo.entity.QjData;
import com.example.demo.listener.CommonListener;
import com.example.demo.service.impl.QjServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
@RestController
public class QingJiaController {
@Autowired
private QjServiceImpl qjService;
@PostMapping("qj/upload")
@ResponseBody
public String upload(MultipartFile file) throws IOException {
EasyExcel.read(file.getInputStream(), QjData.class,
new CommonListener(qjService)).sheet().doRead();
return "success";
}
}
往监听器类传参只需要传入对应的接口就行
package com.example.demo.controller;
import com.alibaba.excel.EasyExcel;
import com.example.demo.entity.CcData;
import com.example.demo.entity.QjData;
import com.example.demo.listener.CommonListener;
import com.example.demo.service.impl.CcServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
@RestController
public class CcController {
@Autowired
private CcServiceImpl ccService;
@PostMapping("cc/upload")
@ResponseBody
public String upload(MultipartFile file) throws IOException {
EasyExcel.read(file.getInputStream(), CcData.class,
new CommonListener(ccService)).sheet().doRead();
return "success";
}
}
创建一些模拟数据
调用接口进行测试
装饰器模式就是不改变原有的的结构,使用类包裹着这个接口,对齐进行增强
我们使用的new CommonListener就是装饰器模式,包裹着(接口)