javaweb:在线聊天网站(框架版)

之前写过一次在线聊天网站,不过那次是无框架版的,这次用框架构建网站,基本功能和上次差不多

涉及知识

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 <
  • 29
    点赞
  • 112
    收藏
    觉得还不错? 一键收藏
  • 18
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值