之前写过一次在线聊天网站,不过那次是无框架版的,这次用框架构建网站,基本功能和上次差不多
涉及知识
java
spring(4.3.5):spring、spring MVC
hibernate
bootstrap
jsp
JavaScript,jquery
websocket
mysql
功能
1.用户的登录、注册、注销、密码修改
2.获知在线用户名字及数量
3.向在线用户发送消息
4.查看与该用户的历史信息
5.当有非当前聊天用户的信息到来时,会有提示
数据库
类一览
mysql数据库建立
一个账户表,一个聊天内容表
create database db_talk;
create table tbl_account
(
id int not null primary key auto_increment,
name varchar(30) not null unique,
password char(20) not null
)CHARACTER SET 'utf8'
COLLATE 'utf8_general_ci';
create table tbl_talk
(
id int not null primary key auto_increment,
content varchar(255),
srcAccountId int not null,
targetAccountId int not null,
time datetime not null default now(),
foreign key(srcAccountId) references tbl_account(id),
foreign key(targetAccountId) references tbl_account(id)
)CHARACTER SET 'utf8'
COLLATE 'utf8_general_ci';
hibernate实体层bean
P.S.关于hibernate注解解释可看http://blog.csdn.net/name_z/article/details/51318271
实体类Account和TalkContent都继承了通用实体类CommonBean
CommonBean
@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class CommonBean {
@Id
@Column(name = "id", insertable = false, updatable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected int id;
@Transient
protected static final String DEFAULT_ID_COLUMN_NAME = "id";
public int getId() {
return id;
}
// public void setId(int id) {
// this.id = id;
// }
//获取本类的简单类名
public abstract String getClassSimpleName();
public String getIdColumnName(){
return DEFAULT_ID_COLUMN_NAME;
}
}
@Entity
@Table(name = "tbl_talk")
public class TalkContent extends CommonBean {
...}
@Entity
@Table(name = "tbl_account")
public class Account extends CommonBean {
...}
使用CommonBean原因
1.各实体类的相同部分(如这次的id)可放在CommonBean中,从而减少了代码量,使代码整洁
2.采用abstract方法约束子对象,方便后面dao层使通用持久层CommonDao类
hibernate持久层dao
使用模板与反射减少代码量。
所有dao类接口都继承了ICommonDao,CommonDao实现了ICommonDao,所有dao类实现都继承了CommonDao
CommonDao使用模板设计,所有子类能不做修改直接调用CommonDao中方法进行实体的增删改查
接口
public interface ICommonDao<T extends CommonBean> {
boolean save(T entity);
boolean delete(T entity);
boolean updateByID(T entity);
T getByID(T entity);
T getByID(int id);
List<T> getAll();
void setSessionFactory(SessionFactory sessionFactory);
}
public interface IAccountDao <