关于Springboot + hibernate 使用websocket的详细代码

来源:互联网 发布:知乎pc客户端 编辑:程序博客网 时间:2024/06/07 10:26

在之前,学习Springboot的时候遇到了许多的问题,希望通过这个博客向爱好学习Springboot的人避免一些误区;

主要是Springboot + hibernate +websocket ,好了废话不多说开始吧:在之前我就不说springboot环境的问题了,网上教程也很多。

一、首先创建一个Springboot工程:

然后选择所需要的包:


然后先创建一个实体类:User类

@Entity@Table(name = "t_user")public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY) //这是自增private int id;@Columnprivate String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}

src/main/resources/application.properties中配置数据库连接:

#这里是设置自定义端口号server.port=8088#连接数据库信息spring.datasource.url = jdbc:mysql://localhost:3306/mytestspring.datasource.username = rootspring.datasource.password = 1234spring.datasource.driverClassName = com.mysql.jdbc.Driverspring.datasource.max-active=20spring.datasource.max-idle=8spring.datasource.min-idle=8spring.datasource.initial-size=10## 实体类自动扫描的包前缀 entitymanager.packagesToScan= com.sanker.Bean######################################################### Specify the DBMSspring.jpa.database = MYSQL# Show or not log for each sql queryspring.jpa.show-sql = true# Hibernate ddl auto (create, create-drop, update)spring.jpa.hibernate.ddl-auto = updatespring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

注意:如果你的实体类属性为userName 中有大写的情况 ,数据库自动建表的字段是:user_name;

在来创建一个websocket类:

主要是websocket的配置类很重要:websocketconfig

import org.springframework.context.annotation.Bean;import org.springframework.web.socket.WebSocketHandler;import org.springframework.web.socket.config.annotation.WebSocketConfigurer;import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;@Configuration@EnableWebSocketpublic class WebsocketConfig implements WebSocketConfigurer {@Overridepublic void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {// TODO Auto-generated method stubregistry.addHandler(WebSocketController(), "/websocket")//这里是地址.setAllowedOrigins("*");//设置所有的域名都可以访问,也就是说可以跨域访问}@Bean  //这里就是给websocket.Java类说明他是一个websocket控制器    public WebSocketHandler WebSocketController() {         return new WebSocket();      }  }

这样写websocket类里面就可以注入属性了!!!!!!这个很重要;

import java.io.IOException;import org.springframework.web.socket.CloseStatus;import org.springframework.web.socket.TextMessage;import org.springframework.web.socket.WebSocketSession;import org.springframework.web.socket.handler.TextWebSocketHandler;public class WebSocket extends TextWebSocketHandler {private WebSocketSession session;/** * 连接关闭调用函数 */@Overridepublic void afterConnectionClosed(WebSocketSession session, CloseStatus arg1) throws Exception {// TODO Auto-generated method stubSystem.out.println("afterConnectionClosed");}/** * 建立Websocket连接成功调用函数 */@Overridepublic void afterConnectionEstablished(WebSocketSession session) throws Exception {// TODO Auto-generated method stubthis.session= session;System.out.println("afterConnectionEstablished");}/** * 接受到客服端发送的消息 */@Overridepublic void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {// TODO Auto-generated method stub}@Overridepublic void handleTransportError(WebSocketSession session, Throwable arg1) throws Exception {// TODO Auto-generated method stubSystem.out.println("handleTransportError");}@Overridepublic boolean supportsPartialMessages() {// TODO Auto-generated method stubSystem.out.println("supportsPartialMessages");return false;}/** * 发送消息 *  * @param msg */public void sendTextMessage(String msg) {TextMessage t = new TextMessage(msg);try {session.sendMessage(t);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


如果写@ServerEndpoint    是不能注入属性的。大概就这样了 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 信而富绑卡失败怎么办 回访电话说错了怎么办 乐贷款逾期一天怎么办 被贷款公司骚扰怎么办 趣店账户异常怎么办 指还王要还款下载不了怎么办 随心花账户异常怎么办 账号已被锁定怎么办 哈罗单车被警告怎么办 禁止安装拼多多怎么办 该用户行为异常怎么办 设备被禁止登录怎么办 你的设备被禁封怎么办 监控显示未配置怎么办 监控显示不在线怎么办 兔聊认证不通过怎么办 高考成绩被屏蔽怎么办 百合号码换了怎么办 微粒贷还款限额怎么办 秒白条扣款失败怎么办 信用卡逾期没还怎么办 催收发侮辱短信怎么办? 快贷还清后被拒怎么办 任信用还不起怎么办 信用飞额度抢光怎么办 装修工钱要不回怎么办 装修客户不给钱怎么办 公司赖账不给怎么办 欠钱不还还把我设置黑名单怎么办 别人欠钱留我电话怎么办 微贷审核不通过怎么办 客户赖账不还怎么办 货款拖欠想赖账怎么办 有欠条还赖账怎么办 个人欠货款逃跑怎么办 微信借钱被拉黑怎么办 朋友借钱忘记还怎么办 座机接不到电话怎么办 工地拖欠材料款怎么办 工地不付货款怎么办 生气引起的失眠怎么办