JavaWeb 项目 --- 博客系统(前后分离)

Posted wwzzzzzzzzz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaWeb 项目 --- 博客系统(前后分离)相关的知识,希望对你有一定的参考价值。

文章目录

效果展示

可以访问 http://124.223.113.147:8080/MyBlog1/home.html 进入我的项目
账号:admin 密码:123456



1. 创建 maven 项目

创建必要的目录.引入需要的依赖


2. 设计数据库

本系统要存入博客文章的信息,
创建博客表.博客的id,博客的标题,博客的内容,博客的日期,博文的博主id
也要存入用户的信息,
创建用户表,用户id,用户名,用户密码

create database if not exists MyBlogSystem;

use MyBlogSystem;

drop table if exists blog;

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

drop table if exists user;

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

3. 封装数据库的操作代码

创建包Dao用来放数据库的代码.

3.1 创建 DBUtil 类

用来连接数据库

package Dao;


import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil 
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/MyBlogSystem?characterEncoding=utf8&useSSL=true&serverTimezone=UTC";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "0000";

    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 e) 
                e.printStackTrace();
            
        
        if(statement != null)
            try 
                statement.close();
             catch (SQLException e) 
                e.printStackTrace();
            
        
        if(connection != null)
            try 
                connection.close();
             catch (SQLException e) 
                e.printStackTrace();
            
        
    


3.2 创建类 Blog (代表一篇博客)

Blog

package Dao;

import java.sql.Timestamp;

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

    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 Timestamp getPostTime() 
        return postTime;
    

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

    public int getUserId() 
        return userId;
    

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



3.3 创建类 User (代表一个用户)

package Dao;

public class User 
    public int userId;
    public String username;
    public 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;
    

3.4 创建类 BlogDao (对博客表进行操作)

package Dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BlogDao 
    // 1. 插入一篇博客
    public void insert(Blog blog) 
        Connection connection = null;
        PreparedStatement statement = null;
        try 
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 拼装 SQL 语句
            String sql = "insert into blog values(null,?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1,blog.getTitle());
            statement.setString(2,blog.getContent());
            statement.setTimestamp(3,blog.getPostTime());
            statement.setInt(4,blog.getUserId());
            // 3. 执行 SQL 语句
            int ret = statement.executeUpdate();
            if(ret == 1)
                System.out.println("插入成功");
            else 
                System.out.println("插入失败");
            
         catch (SQLException e) 
            e.printStackTrace();
         finally 
            DBUtil.close(connection,statement,null);
        
    

    // 2. 获取全部博客
    public List<Blog> selectAll() 
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Blog> list = new ArrayList<>();
        try 
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 拼装 SQL 语句
            // 这里加上order by postTime desc 就可以根据时间排序了.
            String sql = "select * from blog order by postTime desc ";
            statement = connection.prepareStatement(sql);
            // 3. 执行 SQL 语句
            resultSet = statement.executeQuery();
            // 4. 遍历结果集
            while (resultSet.next())
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                list.add(blog);
            
         catch (SQLException e) 
            e.printStackTrace();
         finally 
            DBUtil.close(connection,statement,resultSet);
        
        return list;
    

    // 3. 获取个人博客
    public List<Blog> selectAllPerson(int userId)
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Blog> list = new ArrayList<>();
        try 
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 拼装 SQL 语句
            // 这里加上order by postTime desc 就可以根据时间排序了.
            String sql = "select * from blog where userId = ? order by postTime desc ";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,userId);
            // 3. 执行 SQL 语句
            resultSet = statement.executeQuery();
            // 4. 遍历结果集
            while (resultSet.next())
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                list.add(blog);
            
         catch (SQLException e) 
            e.printStackTrace();
         finally 
            DBUtil.close(connection,statement,resultSet);
        
        return list;
    

    // 4. 根据文章id获取指定博客
    public Blog selectOne(int blogId) 
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try 
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 拼装 SQL 语句
            // 这里加上order by postTime desc 就可以根据时间排序了.
            String sql = "select * from blog where blogId = ? ";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            // 3. 执行 SQL 语句
            resultSet = statement.executeQuery();
            // 4. 遍历结果集
            if (resultSet.next())
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                return blog;
            
         catch (SQLException e) 
            e.printStackTrace();
         finally 
            DBUtil.close(connection,statement,resultSet);
        
        return null;
    

    // 5. 删除指定文章id的博客
    public void delete(int blogId) 
        Connection connection = null;
        PreparedStatement statement = null;
        try 
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 拼装 SQL 语句
            String sql = "delete from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            // 3. 执行 SQL 语句
            int ret = statement.executeUpdate();
            if(ret == 1)
                System.out.println("删除成功");
            else
                System.out.println("删除失败");
            
         catch (SQLException e) 
            e.printStackTrace();
         finally 
            DBUtil.close(connection,statement,null);
        
    
    // 6. 计算个人文章

以上是关于JavaWeb 项目 --- 博客系统(前后分离)的主要内容,如果未能解决你的问题,请参考以下文章

基于springboot+vue的宠物商城系统(前后端分离)

JavaWeb项目放弃jsp?为什么要前后端解耦?为什么要前后端分离?

个人博客系统(前后端分离)

如何在开发时部署和运行前后端分离的JavaWeb项目

SpringBoot 前后端分离Vue个人博客系统

SpringBoot 前后端分离Vue个人博客系统