下载依赖
<!-- websocket -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<!-- MybatisPlus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
数据库sql
CREATE TABLE `interrogation` (
`ID` int(0) NOT NULL AUTO_INCREMENT COMMENT 'id',
`INITIATOR_ID` int(0) NULL DEFAULT NULL COMMENT '发起人的ID()',
`DOCTOR` int(0) NULL DEFAULT NULL COMMENT '接受人ID',
`STATUS` int(0) NULL DEFAULT NULL COMMENT '状态(1.开始聊天和2.结束)',
PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '聊天列表' ROW_FORMAT = Dynamic;
CREATE TABLE `interrogation_chat` (
`ID` int(0) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`INTERROGATION_ID` int(0) NULL DEFAULT NULL COMMENT '聊天表的id',
`CONTENT` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '内容',
`INITIATOR_ID` int(0) NULL DEFAULT NULL COMMENT '发送人',
`CREATE_TIME` datetime(0) NULL DEFAULT NULL COMMENT '发送时间',
PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 130 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '聊天记录表' ROW_FORMAT = Dynamic;
实体类
后续业务采用mybatils-plus写的,如果不会使用手写sql也是可以的
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
/**
* @description: 聊天记录表
* @author:
* @date: 2024/10/27 9:37
**/
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public class InterrogationChat {
/**
* ID
**/
@TableId(value = "ID", type = IdType.AUTO)
private Integer id;
/**
* 聊天表的id
**/
@TableField("INTERROGATION_ID")
private Integer interrogationId;
/**
* 内容
**/
@TableField("CONTENT")
private String content;
/**
* 发送人
**/
@TableField("INITIATOR_ID")
private Integer initiatorId;
/**
* 发送时间
**/
@TableField("CREATE_TIME")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
}
websocker配置
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* @author
* @date 2024/10/23
* @apiNote
*/
@Configuration
public class WebSocketConfig {
/**
* 注入一个ServerEndpointExporter,该Bean会自动注册使用@ServerEndpoint注解申明的websocket endpoint
*/
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import javax.websocket.server.ServerEndpointConfig;
/**
* @author
* @date 2024/10/30
* @apiNote
*/
public class MyEndpointConfigure extends ServerEndpointConfig.Configurator implements ApplicationContextAware {
private static volatile BeanFactory context;
@Override
public <T> T getEndpointInstance(Class<T> clazz) throws InstantiationException
{
return context.getBean(clazz);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
{
System.out.println("auto load"+this.hashCode());
MyEndpointConfigure.context = applicationContext;
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author
* @date 2024/10/30
* @apiNote
*/
@Configuration
public class MyConfigure {
@Bean
public MyEndpointConfigure newConfigure()
{
return new MyEndpointConfigure();
}
}
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.Ta

最低0.47元/天 解锁文章
5319

被折叠的 条评论
为什么被折叠?



