此框架主要针对SpringBoot项目各类功能做出封装,整合各类插件,提供简便快速用法;
已完成功能:
待整合功能
Nacos
easy-es
RabbitMQ
Redis
Debezium
Cancel
请求拦截器
内部过滤器
常用工具类
gateway
auth2
文件上传下载接口封装
hdfs/fastdfs文件存储
本地文件夹监控
文件读取
压缩包读取
数据库配置加密
切面
配置文件读取
日期自动填充
自定义注解转换 0-false
整合Mybatis Plus分页查询
官网说明:https://baomidou.com/pages/97710a/#paginationinnerinterceptor
首先根据官网说明进行配置
/**
* @author Ryan
* @date 2022年03月07日
* @note 配置分页查询拦截器
*/
@Configuration
@MapperScan("com.ryan.fw.mapper")
public class MybatisPlusConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
实现类
@Override
public StudentPageVO getPageList(Integer currentPage, Integer pageSize) {
Page<StudentDO> page = this.page(new Page<>(currentPage, pageSize));
//获取分页查询数据
List<StudentDO> records = page.getRecords();
//验证是否为null
ObjUtils.checkNull(records,"分页查询无数据");
//类型转换
List<StudentVO> studentVO = ObjUtils.toList(records, StudentVO.class);
//返回结果
return StudentPageVO.builder().data(studentVO).count(page.getSize()).build();
}
整合WebSocket服务端;
服务端代码如下:(包含:客户端注册、消息推送、针对IP消息推送功能)
/**
* @author Ryan
* @date 2022/8/1 17:33
* @note WebSocket服务端
*/
@Component
@Slf4j
@ServerEndpoint("/websocket/{id}")
public class WebSocketServer {
private static Map<String, WebSocketServer> map = new HashMap<>(16);
private Session session;
/**
* WebSocket连接
*
* @param session 连接session
* @param id 连接id
*/
@OnOpen
public void onOpen(Session session, @PathParam("id") String id) {
this.session = session;
String ip = WebSocketUtils.getRemoteAddress(session);
if (Objects.nonNull(ip)) {
id = ip + "-" + id;
}
map.put(id, this);
try {
sendMessage(id, WebSocketVo.builder().flag("WebSocketConnect").status(true).message("WebSocket连接注册成功").data("WebSocket连接注册成功").build());
} catch (IOException e) {
log.error("【WebSocket】推送至" + id + "消息失败");
e.printStackTrace();
}
log.info("【WebSocket】页面: " + id + " WebSocket注册连接成功");
}
/**
* WebSocket连接关闭
*
* @param session 连接session
* @param id 连接id
*/
@OnClose
public void onClose(Session session, @PathParam("id") String id) {
String ip = WebSocketUtils.getRemoteAddress(session);
if (Objects.nonNull(ip)) {
id = ip + "-" + id;
}
map.remove(id);
log.info("【WebSocket】页面 " + id + " 断开连接");
log.info("【WebSocket】当前断开IP" + ip);
log.info("【WebSocket】当前连接集合:" + map);
WebSocketVo vo = WebSocketVo.builder().flag("WebSocketDisconnect").status(true).message(id + "页面断开连接").data(id).type("setFlag").build();
sendAllSession(vo, ip);
}
/**
* 发送消息
*
* @param id 连接id
* @param vo 消息内容
* @return
* @throws IOException
*/
public static Boolean sendMessage(String id, WebSocketVo vo) throws IOException {
WebSocketServer webSocketServer = map.get(id);
if (webSocketServer.session.isOpen()) {
try {
webSocketServer.session.getBasicRemote().sendText(JSON.toJSONString(vo));
} catch (IllegalStateException e) {
return false;
}
return true;
} else {
log.error("【WebSocket】接收方连接已关闭");
return false;
}
}
/**
* 广播发送特定IP消息
*
* @param vo 消息内容
* @param ip 指定IP地址
* @return
*/
public synchronized static Boolean sendAllSession(WebSocketVo vo, String ip) {
for (Map.Entry<String, WebSocketServer> entry : map.entrySet()) {
WebSocketServer ws = entry.getValue();
String key = entry.getKey();
if (key.indexOf(ip) > -1) {
try {
log.info("【WebSocket】发送" + entry.getKey() + "页面" + vo.getData() + "断开连接");
if (ws.session.isOpen()) {
ws.session.getBasicRemote().sendText(JSON.toJSONString(vo));
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
return true;
}
/**
* 验证当前连接是否存在
*
* @param id 连接id
* @return Boolean
*/
public synchronized static Boolean vertifyId(String id) {
return Objects.isNull(map.get(id)) ? false : true;
}
}
封装word处理工具类;
当前功能:提取word文件中图片;
/**
* @author Ryan
* @date 2022年04月24日
* @note
*/
@Slf4j
public class WordUtils {
/**
* 解析word中图片
*
* @param bytes
* @return
*/
public static List<MultipartFile> getPictures(byte[] bytes) {
List<MultipartFile> list = new ArrayList<>();
InputStream in = new ByteArrayInputStream(bytes);
try {
Document doc = new Document(in);
NodeCollection nodes = doc.getChildNodes(NodeType.SHAPE, true);
for (Object obj : nodes) {
Shape shape = (Shape) obj;
if (shape.hasImage()) {
String name = UUID.randomUUID().toString() + ".png";
byte[] imageBytes = shape.getImageData().getImageBytes();
MultipartFile file = new MockMultipartFile(name, imageBytes);
list.add(file);
}
}
} catch (Exception e) {
e.printStackTrace();
log.error("【Word解析】解析文件中图片失败");
}
return list;
}
}
GitHub: https://github.com/fsyxjwxw/SpringBoot-Framework
如有其他想法或想要整合的插件请与本人联系;