在之前,学习Springboot的时候遇到了许多的问题,希望通过这个博客向爱好学习Springboot的人避免一些误区;
主要是Springboot + hibernate +websocket ,好了废话不多说开始吧:在之前我就不说springboot环境的问题了,网上教程也很多。
一、首先创建一个Springboot工程:
然后选择所需要的包:
然后先创建一个实体类:User类
@Entity
@Table(name = "t_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) //这是自增
private int id;
@Column
private 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/mytest
spring.datasource.username = root
spring.datasource.password = 1234
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
## 实体类自动扫描的包前缀
entitymanager.packagesToScan= com.sanker.Bean
########################################################
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
spring.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
@EnableWebSocket
public class WebsocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
// TODO Auto-generated method stub
registry.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;
/**
* 连接关闭调用函数
*/
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus arg1) throws Exception {
// TODO Auto-generated method stub
System.out.println("afterConnectionClosed");
}
/**
* 建立Websocket连接成功调用函数
*/
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
// TODO Auto-generated method stub
this.session= session;
System.out.println("afterConnectionEstablished");
}
/**
* 接受到客服端发送的消息
*/
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void handleTransportError(WebSocketSession session, Throwable arg1) throws Exception {
// TODO Auto-generated method stub
System.out.println("handleTransportError");
}
@Override
public boolean supportsPartialMessages() {
// TODO Auto-generated method stub
System.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 block
e.printStackTrace();
}
}
}
如果写@ServerEndpoint 是不能注入属性的。大概就这样了