文章目录
- SSM
- 1 介绍
- 2 集成步骤
- 目录结构
- 配置 web.xml
- 配置 jdbc.properties
- 配置 SqlMapConfig.xml
- 配置 applicationContext-dao.xml
- 配置 applicationContext-service.xml
- 配置 log4j.properties
- 编写解决中文乱码的过滤器
- 编写实体类
- 编写 ItemsMapper 接口
- 编写映射 ItemsMapper.xml
- 编写 ItemsService 接口
- 编写 ItemsServiceImpl 实现 ItemsService 接口
- 编写 ItemsController
SSM
1 介绍
SSM框架是指Spring + Spring MVC + MyBatis的组合,是一种常用的Java Web开发框架。这三个框架分别提供了不同的功能:
-
Spring(Spring Framework): 提供了IoC(控制反转)和AOP(面向切面编程)等功能,用于管理Java应用中的对象和组件,并提供了事务管理等企业级特性。
-
Spring MVC: 是Spring框架的一部分,用于构建Web应用程序的MVC框架,提供了基于Java的配置、灵活的URL映射、强大的数据绑定、表单处理和验证等功能。
-
MyBatis: 是一个持久层框架,用于将Java对象与数据库中的记录进行映射,提供了简单的SQL操作和结果映射。
SSM框架整合这三个框架,可以使得开发者在开发Java Web应用时更加方便和高效。典型的SSM框架的工作流程如下:
-
请求到达前端控制器(DispatcherServlet): 所有的请求都会先到达前端控制器,即DispatcherServlet。
-
DispatcherServlet分发请求: DispatcherServlet根据请求的URL,使用Handler Mapping找到对应的Controller。
-
Controller处理请求: Controller处理请求,可以调用Service层的方法,Service层包含了业务逻辑。
-
Service层处理业务逻辑: Service层处理业务逻辑,可能会调用DAO层的方法来访问数据库。
-
DAO层访问数据库: DAO层使用MyBatis等持久化框架与数据库进行交互。
-
返回结果给前端控制器: 数据库返回结果,Service层将结果传递给Controller。
-
前端控制器选择视图并返回: Controller选择合适的视图(通常是JSP页面),并将结果传递给前端控制器。
-
前端控制器返回响应: 前端控制器将视图渲染成HTML等内容,并返回给客户端。
2 集成步骤
目录结构
配置 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置中文乱码过滤器-->
<filter>
<filter-name>encoding</filter-name>
<filter-class>com.kdx.filter.CharSetEncoding</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!--加载spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<!--监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置前端控制器(中央控制器)-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
配置 jdbc.properties
若 mysql 为8.x版本则配置如下
jdbcs.driver=com.mysql.cj.jdbc.Driver
若 mysql 为8.x版本之前则配置如下
jdbcs.driver=com.mysql.jdbc.Driver
jdbcs.driver=com.mysql.cj.jdbc.Driver
jdbcs.url=jdbc:mysql:///mybatis
jdbcs.uname=root
jdbcs.pwd=kdx010908
配置 SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
</plugins>
</configuration>
配置 applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<!--读取外部的配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbcs.driver}"></property>
<property name="url" value="${jdbcs.url}"></property>
<property name="username" value="${jdbcs.uname}"></property>
<property name="password" value="${jdbcs.pwd}"></property>
</bean>
<!--配置mybatis框架-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
<property name="typeAliasesPackage" value="com.kdx.entity"></property>
</bean>
<!--配置mapper代理-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.kdx.mapper"></property>
</bean>
</beans>
配置 applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!--扫描业务逻辑类-->
<context:component-scan base-package="com.kdx.service"></context:component-scan>
</beans>
配置 log4j.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
编写解决中文乱码的过滤器
public class CharSetEncoding implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
chain.doFilter(request,response);
}
@Override
public void destroy() {
}
}
编写实体类
public class Items {
private Integer id;
private String name;
private Float price;
private String pic;
private String detail;
private Date createtime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic == null ? null : pic.trim();
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail == null ? null : detail.trim();
}
}
编写 ItemsMapper 接口
public interface ItemsMapper {
//查询全部的商品
public List<Items> findAll();
}
编写映射 ItemsMapper.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.easthome.mapper.ItemsMapper">
<select id="findAll" resultType="com.easthome.entity.Items">
select * from items
</select>
</mapper>
编写 ItemsService 接口
public interface ItemsService {
List<Items> queryAll();
}
编写 ItemsServiceImpl 实现 ItemsService 接口
@Service
public class ItemsServiceImpl implements ItemsService {
@Autowired
private ItemsMapper itemsMapper;
@Override
public List<Items> queryAll() {
List<Items> itemsList = itemsMapper.findAll();
return itemsList;
}
}
编写 ItemsController
第一种写法:
@Controller
public class ItemsController {
@Autowired
private ItemsService itemsService;
@RequestMapping("/list")
public ModelAndView list(){
ModelAndView mav = new ModelAndView();
List<Items> itemsList = itemsService.queryAll();
mav.addObject("itemsList",itemsList);
mav.setViewName("list");
return mav;
}
}
第二种写法:
@RequestMapping("/list")
public List<Items> list(){
//自动封装一个集合数据 itemsList 自动去视图解析器下面找 itemsList.jsp页面
return itemsService.queryAll();
}
第三种写法:
@RequestMapping("/list")
public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Items> itemsList = itemsService.queryAll();
request.setAttribute("itemsList",itemsList);
request.getRequestDispatcher("/WEB-INF/jsps/list.jsp").forward(request,response);
}
第四种写法:
@RequestMapping("/list")
public void list(Map<String,Object> map) {
//自动封装一个集合数据 itemsList 自动去视图解析器下面找 itemsList.jsp页面
map.put("itemsList",itemsService.queryAll());
}
最后编写前端页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>开发视图</title>
</head>
<body>
<table width="600px" border="1" align="center">
<tr>
<td>商品序号</td>
<td>商品名称</td>
<td>商品价格</td>
<td>商品简介</td>
<td>操作</td>
</tr>
<c:forEach items="${requestScope.itemsList}" var="items">
<tr>
<td>${items.id}</td>
<td>${items.name}</td>
<td>${items.price}</td>
<td>${items.detail}</td>
<td>
<a href="${pageContext.request.contextPath}/delItem.action?ids=${items.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
部署运行: