学习目的:在spring、SpringMVC、mybatis掌握的基础上,学习如何将三个框架在idea中整合。
Part 1
新建一个web application项目
命名为ssm。
Part 2
在WEB-INF中新建classes、lib文件夹,将相应的jar包放进lib文件夹。将output path设置成这个classes,在library中,添加刚刚的lib的文件夹。
Part 3
新建数据库ssm,以及category_表,并插入数据:
create table ssm.category_
(
id int auto_increment
primary key,
name varchar(30) null
)
charset = utf8;
INSERT INTO ssm.category_ (id, name) VALUES (1, 'category1');
INSERT INTO ssm.category_ (id, name) VALUES (2, 'category2');
INSERT INTO ssm.category_ (id, name) VALUES (3, 'category3');
INSERT INTO ssm.category_ (id, name) VALUES (4, 'category4');
INSERT INTO ssm.category_ (id, name) VALUES (5, 'category5');
Part 4
新建pojo:
package cn.vaefun.pojo;
public class Category {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Category{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Part 5
CategoryMapper
package cn.vaefun.mapper;
import cn.vaefun.pojo.Category;
import java.util.List;
public interface CategoryMapper {
public List<Category> list();
}
Part 6
在mapper包中新建Category.xml,注意namespace要写CategoryMapper的完整类名。
<?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="cn.vaefun.mapper.CategoryMapper">
<select id="list" resultType="Category">
select * from category_
</select>
</mapper>
Part 7
CategoryService
package cn.vaefun.service;
import cn.vaefun.pojo.Category;
import java.util.List;
public interface CategoryService {
List<Category> list();
}
Part 8
CategoryServiceImpl
CategoryServiceImpl被注解@Service标示为一个Service,并且注解方式装配了CategoryMapper。
package cn.vaefun.service.impl;
import cn.vaefun.mapper.CategoryMapper;
import cn.vaefun.pojo.Category;
import cn.vaefun.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CategoryServiceImp implements CategoryService {
@Autowired
CategoryMapper mapper;
@Override
public List<Category> list() {
return mapper.list();
}
}
Part 9
CategoryController
通过Controller标签将该类标示为控制器,并装配了CategoryService。通过RequestMapping映射/listCategory访问路径到listCategory方法,方法中调用service的list的方法获得数据,存放在cs中,并将数据传给cs这个key。
package cn.vaefun.controller;
import cn.vaefun.pojo.Category;
import cn.vaefun.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
public class CategoryController {
@Autowired
CategoryService service;
@RequestMapping("listCategory")
public ModelAndView listCategory(){
ModelAndView mav = new ModelAndView("listCategory");
List<Category> cs = service.list();
mav.addObject("cs",cs);
return mav;
}
}
Part 10
web.xml
该配置文件有两个作用:
1. 通过ContextLoaderListener在web app启动的时候,获取contextConfigLocation配置文件的文件名applicationContext.xml,并进行Spring相关初始化工作
2. 有任何访问,都被DispatcherServlet所拦截,这就是Spring MVC那套工作机制了。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<!-- 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>
<!-- spring mvc核心:分发servlet -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- spring mvc的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Part 11
在src目录下新建applicationContext.xml文件,这是Spring的配置文件,其作用
1. 通过注解,将Service的生命周期纳入Spring的管理
<context:annotation-config />
<context:component-scan base-package="com.how2java.service" />
2. 配置数据源
<bean id="dataSource" class="org.springframework.
jdbc.datasource.DriverManagerDataSource">
3. 扫描存放SQL语句的Category.xml
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
4. 扫描Mapper,并将其生命周期纳入Spring的管理
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config />
<context:component-scan base-package="cn.vaefun.service" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/ssm?characterEncoding=UTF-8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="cn.vaefun.pojo" />
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:cn/vaefun/mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.vaefun.mapper"/>
</bean>
</beans>
Part 12
1. 扫描Controller,并将其生命周期纳入Spring管理
<context:annotation-config/>
<context:component-scan base-package="com.how2java.controller">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
2. 注解驱动,以使得访问路径与方法的匹配可以通过注解配置
<mvc:annotation-driven />
3. 静态页面,如html,css,js,images可以访问
<mvc:default-servlet-handler />
4. 视图定位到/WEB/INF/jsp 这个目录下
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
springMVC.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:annotation-config/>
<context:component-scan base-package="cn.vaefun.controller">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<!-- 视图定位 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Part 13
在WEB-INF文件夹中新建jsp文件夹,在jsp文件夹中新建:listCategory.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<table align='center' border='1' cellspacing='0'>
<tr>
<td>id</td>
<td>name</td>
</tr>
<c:forEach items="${cs}" var="c" varStatus="st">
<tr>
<td>${c.id}</td>
<td>${c.name}</td>
</tr>
</c:forEach>
</table>
Part 14
部署项目,启动服务器,访问ocalhost:8080/ssm_war_exploded/listCategory
Part 15
工作原理
- 首先在浏览器中访问/listCategory。
- Tomcat根据web.xml的配置,拦截到了/listCategory,并交给DispatcherServlet处理。
- DispatcherServlet根据springMVC.xml的配置,将/listCategory请求交给CategoryController类处理,所以要将这个类进行实例化。
- 实例化CategoryController时,注入了CategoryServiceImpl(自动装配的方式实现了CategoryService接口的实例,因为只有一个CategoryServiceImpl实现了该接口,所以就会注入CategoryServiceImpl)。
- 实例化CategoryServiceImpl时,又注入了CategoryMapper。
- 根据applicationContext.xm配置信息,CategoryMapper与Category.xml关联起来。
- 拿到实例化的CategoryController,并调用list方法。
- 获取到数据存放在List<Category> cs上,服务端跳转到listCategory.jsp。
- 在listCategory.jsp上显示数据。