在Java中如何开发简单博客系统

来源:AI大模型作者:广州程序员头衔:程序员
导读:本期聚焦于小伙伴创作的《在Java中如何开发简单博客系统》,敬请观看详情,探索知识的价值。以下视频、文章将为您系统阐述其核心内容与价值。如果您觉得《在Java中如何开发简单博客系统》有用,将其分享出去将是对创作者最好的鼓励。

在Java中开发简单博客系统,我们可以采用Spring Boot搭配MyBatis和MySQL的技术栈,这种方式开发效率高,结构清晰,适合快速实现基础功能。系统主要包含文章管理、分类管理、用户登录等核心模块,能够满足个人博客的基本使用需求。

在Java中如何开发简单博客系统

环境准备

首先需要准备好开发环境,确保本地已经安装好JDK1.8及以上版本,配置好Maven依赖管理工具,安装MySQL数据库和可视化工具。然后创建Spring Boot项目,在pom.xml中添加必要的依赖,包括web模块、MyBatis starter、MySQL驱动、模板引擎等。

核心依赖配置如下:

<dependencies>
    <!-- Spring Boot Web 依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- MyBatis 整合 Spring Boot 依赖 -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.3.0</version>
    </dependency>
    <!-- MySQL 驱动依赖 -->
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!-- Thymeleaf 模板引擎依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
</dependencies>

数据库设计

简单博客系统需要设计三张核心表,分别是用户表、文章表和分类表,表结构如下:

表名字段说明
userid, username, password, create_time存储博客用户的基础信息
categoryid, name, create_time存储文章分类信息
articleid, title, content, user_id, category_id, create_time, update_time存储博客文章的具体内容和关联信息

核心功能开发

配置文件设置

在application.yml中配置数据库连接信息和MyBatis的相关参数:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/blog_db?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
  thymeleaf:
    cache: false # 开发阶段关闭模板缓存
mybatis:
  mapper-locations: classpath:mapper/*.xml # 指定Mapper文件位置
  type-aliases-package: com.example.blog.entity # 实体类包路径

实体类开发

根据数据库表结构创建对应的实体类,以文章实体类为例:

package com.example.blog.entity;

import java.util.Date;

public class Article {
    private Integer id;
    private String title;
    private String content;
    private Integer userId;
    private Integer categoryId;
    private Date createTime;
    private Date updateTime;

    // 省略getter和setter方法
}

文章管理接口开发

开发文章相关的增删改查接口,首先创建ArticleMapper接口:

package com.example.blog.mapper;

import com.example.blog.entity.Article;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;

@Mapper
public interface ArticleMapper {
    // 新增文章
    int insert(Article article);
    // 根据id删除文章
    int deleteById(Integer id);
    // 更新文章
    int update(Article article);
    // 根据id查询文章
    Article selectById(Integer id);
    // 查询所有文章
    List<Article> selectAll();
}

对应的Mapper.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.blog.mapper.ArticleMapper">
    <insert id="insert" parameterType="com.example.blog.entity.Article">
        insert into article (title, content, user_id, category_id, create_time, update_time)
        values (#{title}, #{content}, #{userId}, #{categoryId}, #{createTime}, #{updateTime})
    </insert>

    <delete id="deleteById" parameterType="int">
        delete from article where id = #{id}
    </delete>

    <update id="update" parameterType="com.example.blog.entity.Article">
        update article
        set title = #{title},
            content = #{content},
            category_id = #{categoryId},
            update_time = #{updateTime}
        where id = #{id}
    </update>

    <select id="selectById" parameterType="int" resultType="com.example.blog.entity.Article">
        select * from article where id = #{id}
    </select>

    <select id="selectAll" resultType="com.example.blog.entity.Article">
        select * from article order by create_time desc
    </select>
</mapper>

控制器开发

创建ArticleController处理前端的请求,实现文章的列表展示、新增、编辑等功能:

package com.example.blog.controller;

import com.example.blog.entity.Article;
import com.example.blog.mapper.ArticleMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/article")
public class ArticleController {
    private final ArticleMapper articleMapper;

    public ArticleController(ArticleMapper articleMapper) {
        this.articleMapper = articleMapper;
    }

    // 文章列表页
    @GetMapping("/list")
    public String list(Model model) {
        List<Article> articleList = articleMapper.selectAll();
        model.addAttribute("articleList", articleList);
        return "article/list";
    }

    // 新增文章页面
    @GetMapping("/add")
    public String addPage() {
        return "article/add";
    }

    // 保存新增文章
    @PostMapping("/save")
    public String save(Article article) {
        article.setCreateTime(new Date());
        article.setUpdateTime(new Date());
        article.setUserId(1); // 暂时默认用户id为1
        articleMapper.insert(article);
        return "redirect:/article/list";
    }

    // 编辑文章页面
    @GetMapping("/edit")
    public String editPage(Integer id, Model model) {
        Article article = articleMapper.selectById(id);
        model.addAttribute("article", article);
        return "article/edit";
    }

    // 更新文章
    @PostMapping("/update")
    public String update(Article article) {
        article.setUpdateTime(new Date());
        articleMapper.update(article);
        return "redirect:/article/list";
    }

    // 删除文章
    @GetMapping("/delete")
    public String delete(Integer id) {
        articleMapper.deleteById(id);
        return "redirect:/article/list";
    }
}

页面开发

使用Thymeleaf模板引擎开发前端页面,文章列表页的核心代码如下:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>博客文章列表</title>
</head>
<body>
    <h1>博客文章列表</h1>
    <a href="/article/add">新增文章</a>
    <table border="1" cellpadding="5" cellspacing="0">
        <tr>
            <th>文章标题</th>
            <th>创建时间</th>
            <th>操作</th>
        </tr>
        <tr th:each="article : ${articleList}">
            <td th:text="${article.title}"></td>
            <td th:text="${#dates.format(article.createTime, 'yyyy-MM-dd HH:mm:ss')}"></td>
            <td>
                <a th:href="@{/article/edit(id=${article.id})}">编辑</a>
                <a th:href="@{/article/delete(id=${article.id})}" onclick="return confirm('确定要删除吗?')">删除</a>
            </td>
        </tr>
    </table>
</body>
</html>

项目运行测试

完成上述开发后,启动Spring Boot项目,访问localhost:8080/article/list就可以看到文章列表页面,点击新增文章可以进入添加页面,填写标题和内容后保存就能在列表中看到新增的文章,也可以对已有文章进行编辑和删除操作。如果要扩展功能,还可以添加用户登录验证、文章分页、分类筛选等功能,让博客系统更加完善。

Java博客系统Spring_BootMyBatisMySQL修改时间:2026-06-19 14:48:45

免责声明:​ 已尽一切努力确保本网站所含信息的准确性。网站内容多为原创整理与精心编撰,观点力求客观中立。本站旨在免费分享,内容仅供个人学习、研究或参考使用。若引用了第三方作品,版权归原作者所有。如内容涉及您的权益,请联系我们处理。
内容垂直聚焦
专注技术核心技术栏目,确保每篇文章深度聚焦于实用技能。从代码技巧到架构设计,为用户提供无干扰的纯技术知识沉淀,精准满足专业提升需求。
知识结构清晰
覆盖从开发到部署的全链路。AI、前端、编程、数据库、服务器、建站、系统层层递进,构建清晰学习路径,帮助用户系统化掌握开发与运维所需的核心技术。
深度技术解析
拒绝泛泛而谈,深入技术细节与实践难点。无论是数据库优化还是服务器配置,均结合真实场景与代码示例进行剖析,致力于提供可直接应用于工作的解决方案。
专业领域覆盖
精准对应开发生命周期。从前端界面到后端编程,从数据库操作到服务器运维,形成完整闭环,一站式满足全栈工程师和运维人员的技术需求。
即学即用高效
内容强调实操性,步骤清晰、代码完整。用户可根据教程直接复现和应用于自身项目,显著缩短从学习到实践的距离,快速解决开发中的具体问题。
持续更新保障
专注既定技术方向进行长期、稳定的内容输出。确保各栏目技术文章持续更新迭代,紧跟主流技术发展趋势,为用户提供经久不衰的学习价值。