哪些产品可以做单页网站,微信如何修改wordpress,wordpress主题路径,广州培训 网站开发为了实现一个功能完善的个人博客系统#xff0c;我们将使用Spring Boot作为框架#xff0c;MySQL作为数据库#xff0c;并引入Spring Security来处理用户认证和授权。以下是系统的详细设计和实现步骤#xff1a;
## 项目结构
- src/main/java/com/blog - controller …为了实现一个功能完善的个人博客系统我们将使用Spring Boot作为框架MySQL作为数据库并引入Spring Security来处理用户认证和授权。以下是系统的详细设计和实现步骤
## 项目结构
- src/main/java/com/blog - controller - service - repository - model - src/main/resources - application.properties - pom.xml
## 主要功能
1. 用户注册和登录 2. 文章的增删改查CRUD 3. 评论系统 4. 标签系统 5. 分页和搜索 6. 富文本编辑器支持
## 项目初始化
### 1. pom.xml 配置
xml dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-data-jpa/artifactId /dependency dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-security/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-thymeleaf/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-validation/artifactId /dependency /dependencies
### 2. application.properties 配置
properties spring.datasource.urljdbc:mysql://localhost:3306/blog spring.datasource.usernameroot spring.datasource.passwordroot
spring.jpa.hibernate.ddl-autoupdate spring.jpa.show-sqltrue spring.jpa.properties.hibernate.dialectorg.hibernate.dialect.MySQL5Dialect
### 3. 数据库模型
#### 3.1 用户模型
java Entity public class User { Id GeneratedValue(strategy GenerationType.IDENTITY) private Long id; private String username; private String password; private String email; // Getters and Setters }
#### 3.2 文章模型
java Entity public class Post { Id GeneratedValue(strategy GenerationType.IDENTITY) private Long id; private String title; private String content; private LocalDateTime createdAt; ManyToOne JoinColumn(name user_id) private User user; ManyToMany JoinTable( name post_tag, joinColumns JoinColumn(name post_id), inverseJoinColumns JoinColumn(name tag_id)) private SetTag tags new HashSet(); // Getters and Setters }
#### 3.3 评论模型
java Entity public class Comment { Id GeneratedValue(strategy GenerationType.IDENTITY) private Long id; private String content; private LocalDateTime createdAt; ManyToOne JoinColumn(name post_id) private Post post; ManyToOne JoinColumn(name user_id) private User user; ManyToOne JoinColumn(name parent_comment_id) private Comment parentComment; OneToMany(mappedBy parentComment, cascade CascadeType.ALL) private ListComment replies new ArrayList(); // Getters and Setters }
#### 3.4 标签模型
java Entity public class Tag { Id GeneratedValue(strategy GenerationType.IDENTITY) private Long id; private String name; ManyToMany(mappedBy tags) private SetPost posts new HashSet(); // Getters and Setters }
### 4. 数据库仓库
#### 4.1 用户仓库
java public interface UserRepository extends JpaRepositoryUser, Long { OptionalUser findByUsername(String username); }
#### 4.2 文章仓库
java public interface PostRepository extends JpaRepositoryPost, Long { PagePost findAll(Pageable pageable); }
#### 4.3 评论仓库
java public interface CommentRepository extends JpaRepositoryComment, Long { ListComment findByPostId(Long postId); ListComment findByParentCommentId(Long parentCommentId); }
#### 4.4 标签仓库
java public interface TagRepository extends JpaRepositoryTag, Long { }
### 5. 服务层
#### 5.1 用户服务
java Service public class UserService { Autowired private UserRepository userRepository; Autowired private PasswordEncoder passwordEncoder; public User save(User user) { user.setPassword(passwordEncoder.encode(user.getPassword())); return userRepository.save(user); } public OptionalUser findByUsername(String username) { return userRepository.findByUsername(username); } }
#### 5.2 文章服务
java Service public class PostService { Autowired private PostRepository postRepository; public Post save(Post post) { post.setCreatedAt(LocalDateTime.now()); return postRepository.save(post); } public PagePost findAll(Pageable pageable) { return postRepository.findAll(pageable); } }
#### 5.3 评论服务
java Service public class CommentService { Autowired private CommentRepository commentRepository; public Comment save(Comment comment) { comment.setCreatedAt(LocalDateTime.now()); return commentRepository.save(comment); } public ListComment findByPostId(Long postId) { return commentRepository.findByPostId(postId); } public ListComment findByParentCommentId(Long parentCommentId) { return commentRepository.findByParentCommentId(parentCommentId); } }
### 6. 控制器
#### 6.1 用户控制器
java Controller RequestMapping(/users) public class UserController { Autowired private UserService userService; PostMapping(/register) public String register(ModelAttribute User user) { userService.save(user); return redirect:/login; } }
#### 6.2 文章控制器
java Controller RequestMapping(/posts) public class PostController { Autowired private PostService postService; GetMapping public String list(Model model, Pageable pageable) { PagePost posts postService.findAll(pageable); model.addAttribute(posts, posts); return posts/list; } PostMapping public String save(ModelAttribute Post post) { postService.save(post); return redirect:/posts; } }
#### 6.3 评论控制器
java Controller RequestMapping(/comments) public class CommentController { Autowired private CommentService commentService; PostMapping public String save(ModelAttribute Comment comment) { commentService.save(comment); return redirect:/posts/ comment.getPost().getId(); } }
### 7. 前端模板使用Thymeleaf
#### 7.1 登录页面
html !DOCTYPE html html xmlns:thhttp://www.thymeleaf.org head titleLogin/title /head body form th:action{/login} methodpost div label forusernameUsername:/label input typetext idusername nameusername / /div div label forpasswordPassword:/label input typepassword idpassword namepassword / /div div button typesubmitLogin/button /div /form /body /html #### 7.2 文章列表页面
html !DOCTYPE html html xmlns:thhttp://www.thymeleaf.org head titlePosts/title /head body div a th:href{/posts/new}New Post/a /div div ul li th:eachpost : ${posts} h2 th:text${post.title}/h2 p th:text${post.content}/p pBy: span th:text${post.user.username}/span/p a th:href{/posts/{id}(id${post.id})}Read more/a /li /ul /div /body /html #### 7.3 文章详情页面
html !DOCTYPE html html xmlns:thhttp://www.thymeleaf.org head titlePost Details/title /head body h1 th:text${post.title}/h1 p th:text${post.content}/p h2Comments/h2 div th:eachcomment : ${comments} p th:text${comment.content}/p pBy: span th:text${comment.user.username}/span/p div th:eachreply : ${comment.replies} p th:text${reply.content}/p pBy: span th:text${reply.user.username}/span/p /div form th:action{/comments} methodpost input typehidden th:value${comment.id} nameparentComment.id / textarea namecontent/textarea button typesubmitReply/button /form /div /body /html ### 8. 富文本编辑器支持
在Thymeleaf模板中添加TinyMCE的支持
html !DOCTYPE html html xmlns:thhttp://www.thymeleaf.org head titleNew Post/title script srchttps://cdn.tiny.cloud/1/no-api-key/tinymce/5/tinymce.min.js/script script tinymce.init({ selector: #content }); /script /head body form th:action{/posts} methodpost div label fortitleTitle:/label input typetext idtitle nametitle / /div div label forcontentContent:/label textarea idcontent namecontent/textarea /div div button typesubmitSave/button /div /form /body /html
通过上述步骤我们已经建立了一个功能完善的个人博客系统。这个系统包括用户注册和登录、文章的增删改查、评论和标签系统以及富文本编辑器的支持。