github上的项目地址
https://github.com/1259472421/springMVC_demo.git
请参考Readme文件进行相关操作。
需求:查询商品列表
一.整合原理
1.ssm系统架构
2.项目工程结构(参考前一小节建立)
3.整合过程
(1).整合dao (需要的数据库文件从github的项目代码中获取)
- 配置mybatis.xml。
- 配置applicationContext-dao.xml(数据源,SqlSessionFactory,mapper扫描器)。
- 逆向工程生成po类及mapper(ItemsMapperCustom.xml和ItemsMapperCustom.java)。
(2).整合service
- 定义service接口。
- 在spring容器配置事务控制(applicationContext-transaction.xml)。
(3).整合springmvc
- 创建springmvc.xml文件,配置处理器、处理器映射器、处理器适配器、视图解析器。
- 编写controller。
- 编写jsp。
- 加载spring容器(web.xml中)。
二.配置
1.web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<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_3_1.xsd"
version="3.1">
<!-- 启动Web容器时,加载spring容器,自动装配ApplicationContext.xml的配置信息 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/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>
<!-- 加载springmvc配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<!-- 配置将所有请求交给springmvc来处理 -->