使用ssm框架写一个博客系统
时间: 2023-11-29 14:10:56 浏览: 188
好的,下面是一个简单的博客系统的SSM框架搭建步骤:
1. 创建Maven项目
首先,在Eclipse或IntelliJ IDEA中创建一个Maven项目,并添加以下依赖:
```
<dependencies>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!--MyBatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.4</version>
</dependency>
<!--JDBC-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<!--Servlet API-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!--JSP API-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
```
2. 配置web.xml
在Web项目的web.xml文件中添加Spring MVC的DispatcherServlet和ContextLoaderListener,还要添加字符编码和文件上传的过滤器。以下是一个示例web.xml文件:
```
<web-app>
<!--Spring MVC-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--Spring-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-context.xml</param-value>
</context-param>
<!--字符编码过滤器-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--文件上传过滤器-->
<filter>
<filter-name>multipartFilter</filter-name>
<filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>multipartFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
```
3. 配置Spring
在src/main/resources目录下创建一个spring目录,并创建两个配置文件:spring-context.xml和spring-mvc.xml。
- spring-context.xml:配置数据源和MyBatis的SqlSessionFactory等等。
```
<beans>
<!--数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--MyBatis SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.example.blog.entity"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!--DAO层扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.blog.dao"/>
</bean>
</beans>
```
- spring-mvc.xml:配置Spring MVC的组件扫描、视图解析器等等。
```
<beans>
<!--组件扫描-->
<context:component-scan base-package="com.example.blog"/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--处理静态资源-->
<mvc:default-servlet-handler/>
<!--处理请求映射-->
<mvc:annotation-driven/>
</beans>
```
4. 创建实体类和DAO层
创建一些实体类和相应的DAO接口,并使用MyBatis的注解或XML文件来实现这些接口。以下是一个简单的示例:
```
public interface BlogMapper {
//查询所有博客
List<Blog> findAllBlogs();
//根据ID查询博客
Blog findBlogById(Integer id);
//添加博客
void addBlog(Blog blog);
//更新博客
void updateBlog(Blog blog);
//删除博客
void deleteBlog(Integer id);
}
public class Blog {
private Integer id; //博客ID
private String title; //博客标题
private String content; //博客内容
private Date createTime; //博客创建时间
private Date updateTime; //博客更新时间
//getter和setter方法
}
```
5. 创建Service层
在Service层中定义一些接口和实现类,用于对DAO层进行进一步的封装。以下是一个简单的示例:
```
public interface BlogService {
//查询所有博客
List<Blog> findAllBlogs();
//根据ID查询博客
Blog findBlogById(Integer id);
//添加博客
void addBlog(Blog blog);
//更新博客
void updateBlog(Blog blog);
//删除博客
void deleteBlog(Integer id);
}
@Service
public class BlogServiceImpl implements BlogService {
@Autowired
private BlogMapper blogMapper;
@Override
public List<Blog> findAllBlogs() {
return blogMapper.findAllBlogs();
}
@Override
public Blog findBlogById(Integer id) {
return blogMapper.findBlogById(id);
}
@Override
public void addBlog(Blog blog) {
blogMapper.addBlog(blog);
}
@Override
public void updateBlog(Blog blog) {
blogMapper.updateBlog(blog);
}
@Override
public void deleteBlog(Integer id) {
blogMapper.deleteBlog(id);
}
}
```
6. 创建Controller层
在Controller层中定义一些控制器,用于处理请求和返回视图。以下是一个简单的示例:
```
@Controller
@RequestMapping("/blog")
public class BlogController {
@Autowired
private BlogService blogService;
//查询所有博客
@RequestMapping("/list")
public String list(Model model) {
List<Blog> blogs = blogService.findAllBlogs();
model.addAttribute("blogs", blogs);
return "blogList";
}
//添加博客
@RequestMapping("/add")
public String add(Blog blog) {
blogService.addBlog(blog);
return "redirect:/blog/list";
}
//更新博客
@RequestMapping("/update")
public String update(Blog blog) {
blogService.updateBlog(blog);
return "redirect:/blog/list";
}
//删除博客
@RequestMapping("/delete")
public String delete(Integer id) {
blogService.deleteBlog(id);
return "redirect:/blog/list";
}
}
```
7. 创建视图
在Web项目的WEB-INF/views目录下创建一些视图,用于显示页面和处理数据。以下是一个简单的示例:
```
<!--博客列表页面-->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>博客列表</title>
</head>
<body>
<h1>博客列表</h1>
<a href="${pageContext.request.contextPath}/blog/add">添加博客</a>
<table border="1">
<tr>
<th>ID</th>
<th>标题</th>
<th>内容</th>
<th>创建时间</th>
<th>更新时间</th>
<th>操作</th>
</tr>
<c:forEach items="${blogs}" var="blog">
<tr>
<td>${blog.id}</td>
<td>${blog.title}</td>
<td>${blog.content}</td>
<td>${blog.createTime}</td>
<td>${blog.updateTime}</td>
<td>
<a href="${pageContext.request.contextPath}/blog/update?id=${blog.id}">修改</a>
<a href="${pageContext.request.contextPath}/blog/delete?id=${blog.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
<!--博客添加页面-->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加博客</title>
</head>
<body>
<h1>添加博客</h1>
<form action="${pageContext.request.contextPath}/blog/add" method="post">
标题:<input type="text" name="title"><br/>
内容:<textarea name="content"></textarea><br/>
<input type="submit" value="添加">
</form>
</body>
</html>
<!--博客更新页面-->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>更新博客</title>
</head>
<body>
<h1>更新博客</h1>
<form action="${pageContext.request.contextPath}/blog/update" method="post">
ID:<input type="text" name="id" value="${blog.id}" readonly><br/>
标题:<input type="text" name="title" value="${blog.title}"><br/>
内容:<textarea name="content">${blog.content}</textarea><br/>
<input type="submit" value="保存">
</form>
</body>
</html>
```
到此,一个简单的博客系统就搭建好了。可以通过访问http://localhost:8080/blog/list来查看博客列表页面。
阅读全文