博客系统项目详解

本文详述了一个Java Web项目的实现过程,使用Servlet、HTML、CSS和JS,涵盖用户登录、博客展示、编辑、删除等功能。项目采用前后端分离,数据库设计包括用户表和博客表,实现了前后端接口的约定和数据库操作的封装。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目类型:Java Web项目
主要技术:Html、CSS、JS、Servlet、MySQL
开发工具:IDEA
数据库:MySQL
数据库表:2张
项目介绍:使用约定前后端接口,前后端分离的方法。后端采用Servlet,前端使用的是Html、CSS、JS。基本的功能包括用户登录、显示用户信息 、博客的编辑与发布、删除博客和用户注销功能。判定用户状态限制用户可操作的功能。

github链接

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1.创建maven项目

1.创建maven项目并创建好所需要的webapp目录
在这里插入图片描述
2.在web.xml引入代码片段

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
</web-app>

3.引入依赖(pom.xml)

    <dependencies>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.6.1</version>
        </dependency>
        <!-- 引入  mysql  驱动包       -->
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
    </dependencies>

    <packaging>war</packaging>
    <build>
        <finalName>blog_sys</finalName>
    </build>

2.导入写好的前端代码

将之前写好的前端代码导入到webapp目录下
在这里插入图片描述

3.设计数据库

本系统的两个实体信息包括用户和博客,因此创建用户表和博客表。
创建用户表:

用户id ,用户名,用户密码

创建博客表

博客的 id,博客的标题,博客的内容,博客的日期,作者 id

db.sql

-- 编写建库建表的 sql
create database if not exists blog_sys;

use blog_sys;

-- 创建一个博客表
drop table if exists blog;
create table blog (
    blogId int primary key auto_increment,
    title varchar(1024),
    content mediumtext,
    userId int,
    postTime datetime
);

-- 给博客表插入数据,方便测试
insert into blog values(null, '这是第一篇博客', '从今天开始,我要认真学java', 1, now());
insert into blog values(null, '这是第二篇博客', '从今天开始,我要认真学java', 1, now());
insert into blog values(null, '这是第三篇博客', '从今天开始,我要认真学java', 1, now());
insert into blog values(null, '这是第一篇博客', '从今天开始,我要认真学java', 2, now());
insert into blog values(null, '这是第二篇博客', '从今天开始,我要认真学java', 2, now());
insert into blog values(null, '这是第三篇博客', '从今天开始,我要认真学java', 2, now());
insert into blog values(null, '这是第四篇博客', '从今天开始,我要认真学java', 2, now());

-- 创建一个用户表
drop table if exists user;
create table user(
    userId int primary key auto_increment,
    username varchar(128) unique,
    password varchar(128)
);

insert into user values(null, 'zhangsan', '123');
insert into user values(null, 'lisi', '123');

4.封装数据库操作

创建model包用来存放数据库操作代码
在这里插入图片描述

1)创建 DBUtil 类

用于和数据库建立连接

public class DBUtil {
   
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/blog_sys?characterEncoding=utf8&useSSL=false";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "";

    private static volatile DataSource dataSource = null;

    private static DataSource getDataSource(){
   
        if(dataSource == null){
   
            synchronized (DBUtil.class){
   
                if(dataSource == null){
   
                    dataSource = new MysqlDataSource();
                    ( (MysqlDataSource)dataSource).setURL(URL);
                    ((MysqlDataSource)dataSource).setUser(USERNAME);
                    ((MysqlDataSource)dataSource).setPassword(PASSWORD);
                }
            }
        }
        return dataSource;
    }

    public static Connection getConnection() throws SQLException {
   
        return getDataSource().getConnection();
    }

    public static void close(Connection connection, PreparedStatement statement, ResultSet resultSet){
   
        if(resultSet != null){
   
            try {
   
                resultSet.close();
            } catch (SQLException throwables) {
   
                throwables.printStackTrace();
            }
        }

        if(statement != null){
   
            try {
   
                statement.close();
            } catch (SQLException throwables) {
   
                throwables.printStackTrace();
            }
        }

        if(connection != null){
   
            try {
   
                connection.close();
            } catch (SQLException throwables) {
   
                throwables.printStackTrace();
            }
        }
    }
}

2)创建 Blog类

博客信息和获取、设置博客信息方法的抽象类

public class Blog {
   
    private int blogId;
    private String title;
    private String content;
    private int userId;
    private Timestamp postTime;

    public int getBlogId() {
   
        return blogId;
    }

    public void setBlogId(int blogId) {
   
        this.blogId = blogId;
    }

    public String getTitle() {
   
        return title;
    }

    public void setTitle(String title) {
   
        this.title = title;
    }

    public String getContent() {
   
        return content;
    }

    public void setContent(String content) {
   
        this.content = content;
    }

    public int getUserId() {
   
        return userId;
    }

    public void setUserId(int userId) {
   
        this.userId = userId;
    }

    //把这里的getter方法给改了,不是返回一个时间戳对象,而是返回一个String(格式化好的时间)
    public String getPostTime() {
   
        //使用SimpleDateFormat来完成时间戳到格式化日期时间的转换
        //这个转换过程,需要在构造方法中指定要转换的格式,然后调用format来进行转换
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(postTime);
    }

    public void setPostTime(Timestamp postTime) {
   
        this.postTime = postTime;
    }
}

3)创建 User

用户信息和获取、设置用户信息方法的抽象类

public class User {
   
    private int userId = 0;
    private String username = "";
    private String password = "";

    public int getUserId() {
   
        return userId;
    }

    public void setUserId(int userId) {
   
        this.userId = userId;
    }

    public String getUsername() {
   
        return username;
    }

    public void setUsername(String username) {
   
        this.username = username;
    }

    public String getPassword() {
   
        return password;
    }

    public void setPassword(String password) {
   
        this.password = password;
    }
}

4 )创建BlogDao类

对博客增删查改的操作的抽象类
实现操作包括:

  1. 往博客表里插入一个博客
  2. 能够获取到博客表中的所有博客信息(用在博客列表页,此处每篇博客不一定会获取到完整的正文
  3. 能够根据博客id获取到指定的博客内容(用在博客详情页)
  4. 从博客列表中根据博客id删除博客
//这个类用于封装博客表的基本操作
public class BlogDao {
   
    //1.往博客表里插入一个博客
    public void insert(Blog blog) {
   
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
   
            //1)和数据库建立连接
            connection = DBUtil.getConnection();
            //2)构造SQL语句
            
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值