为了实现一个基于Spring和Spring MVC的汉服文化宣传网站,我们需要创建一个简单的Web应用程序来展示汉服文化和相关信息。这个系统将包括以下几个部分:
- 数据库表设计:定义文章、用户和评论的相关表。
- 实体类:表示数据库中的数据。
- DAO层接口及MyBatis映射文件:用于与数据库交互。
- Service层接口及其实现类:业务逻辑处理。
- Controller层:处理HTTP请求。
- 前端页面:展示信息并允许用户操作。
1. 数据库表设计
假设我们有一个名为hanfu_culture
的数据库,其中包含以下表:
articles
users
comments
CREATE TABLE articles (id INT AUTO_INCREMENT PRIMARY KEY,title VARCHAR(255) NOT NULL,content TEXT NOT NULL,author_id INT NOT NULL,created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,FOREIGN KEY (author_id) REFERENCES users(id)
);CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY,username VARCHAR(100) NOT NULL UNIQUE,email VARCHAR(100) NOT NULL UNIQUE,password VARCHAR(255) NOT NULL
);CREATE TABLE comments (id INT AUTO_INCREMENT PRIMARY KEY,article_id INT NOT NULL,user_id INT NOT NULL,comment_text TEXT NOT NULL,created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,FOREIGN KEY (article_id) REFERENCES articles(id),FOREIGN KEY (user_id) REFERENCES users(id)
);
2. 实体类
创建Java实体类来表示这些表的数据。
Article.java
import java.sql.Timestamp;public class Article {private Integer id;private String title;private String content;private Integer authorId;private Timestamp createdAt;// Getters and Setters
}
User.java
public class User {private Integer id;private String username;private String email;private String password;// Getters and Setters
}
Comment.java
import java.sql.Timestamp;public class Comment {private Integer id;private Integer articleId;private Integer userId;private String commentText;private Timestamp createdAt;// Getters and Setters
}
3. DAO层接口及MyBatis映射文件
定义DAO层接口以及对应的XML映射文件。
ArticleDao.java
import java.util.List;public interface ArticleDao {List<Article> getAllArticles();Article getArticleById(int id);void addArticle(Article article);
}
ArticleMapper.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.dao.ArticleDao"><select id="getAllArticles" resultType="com.example.model.Article">SELECT * FROM articles ORDER BY created_at DESC</select><select id="getArticleById" parameterType="int" resultType="com.example.model.Article">SELECT * FROM articles WHERE id = #{id}</select><insert id="addArticle" parameterType="com.example.model.Article">INSERT INTO articles (title, content, author_id, created_at)VALUES (#{title}, #{content}, #{authorId}, NOW())</insert>
</mapper>
UserDao.java
import java.util.List;public interface UserDao {List<User> getAllUsers();User getUserById(int id);User getUserByUsername(String username);void addUser(User user);
}
UserMapper.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.dao.UserDao"><select id="getAllUsers" resultType="com.example.model.User">SELECT * FROM users</select><select id="getUserById" parameterType="int" resultType="com.example.model.User">SELECT * FROM users WHERE id = #{id}</select><select id="getUserByUsername" parameterType="String" resultType="com.example.model.User">SELECT * FROM users WHERE username = #{username}</select><insert id="addUser" parameterType="com.example.model.User">INSERT INTO users (username, email, password)VALUES (#{username}, #{email}, #{password})</insert>
</mapper>
CommentDao.java
import java.util.List;public interface CommentDao {List<Comment> getCommentsByArticleId(int articleId);void addComment(Comment comment);
}
CommentMapper.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.dao.CommentDao"><select id="getCommentsByArticleId" parameterType="int" resultType="com.example.model.Comment">SELECT * FROM comments WHERE article_id = #{articleId} ORDER BY created_at ASC</select><insert id="addComment" parameterType="com.example.model.Comment">INSERT INTO comments (article_id, user_id, comment_text, created_at)VALUES (#{articleId}, #{userId}, #{commentText}, NOW())</insert>
</mapper>
4. Service层接口及其实现类
定义Service层接口及其实现类。
ArticleService.java
import java.util.List;public interface ArticleService {List<Article> getAllArticles();Article getArticleById(int id);void addArticle(Article article);
}
ArticleServiceImpl.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class ArticleServiceImpl implements ArticleService {@Autowiredprivate ArticleDao articleDao;@Overridepublic List<Article> getAllArticles() {return articleDao.getAllArticles();}@Overridepublic Article getArticleById(int id) {return articleDao.getArticleById(id);}@Overridepublic void addArticle(Article article) {articleDao.addArticle(article);}
}
UserService.java
import java.util.List;public interface UserService {List<User> getAllUsers();User getUse