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

环境准备
首先需要准备好开发环境,确保本地已经安装好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>
数据库设计
简单博客系统需要设计三张核心表,分别是用户表、文章表和分类表,表结构如下:
| 表名 | 字段 | 说明 |
|---|---|---|
| user | id, username, password, create_time | 存储博客用户的基础信息 |
| category | id, name, create_time | 存储文章分类信息 |
| article | id, 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