在【01】章中介紹了mybatis與Spring整合之後的開發流程,Spring與mybatis整合,本質上是Spring將各層進行整合,(Spring管理持久層的mapper、Spring管理業務層service、Spring管理表現層的handler)。本章將介紹Spring與service及controller進行整合的操作流程思路簡單如下:
1.使用配置方式(後期會使用註解方式,顯示配置方式也要會)將service接口配置在Spring配置文件中
2.實現事務控制
3.整合Springmvc
4.配置前端控制器
5.編寫controller
6.編寫jsp
7.加載Spring容器
8.測試
具體操作流程如下:
【1】定義service接口(ItemsService.java),定義接口實現類(ItemsServiceImpl.java)。新建service文件夾,路徑:src\main\java\ssm\service,在文件夾下創建兩個文件:
public interface ItemsService {
//商品查詢列表
List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
}
實現類代碼:
public class ItemsServiceImpl implements ItemsService {
@Autowired
private ItemsCustomMapper itemsCustomMapper;
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception {
//通過itemsMapperCustom查詢數據庫
return itemsCustomMapper.findItemsList(itemsQueryVo);
}
}
【2】在Spring容器配置service(applicationContext-service.xml)
創建applicationContext-service.xml文件,路徑(src\main\resources\spring\applicationContext-service.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--商品管理的service-->
<bean id="itemsService" class="ssm.service.itemsServiceImpl"/>
</beans>
【3】事務控制(applicationContext-transaction.xml)路徑:src\main\resources\spring\applicationContext-transaction.xml,代碼如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <!--事務管理器 對mybatis操作數據庫事務控制,spring使用jdbc的事務控制類--> <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--數據源 dataSource在applicationContext-dao.xml中配置過了--> <property name="dataSource" ref="dataSource"/> </bean> <!-- 通知 --> <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/> </beans>
【4】整合Springmvc
創建Springmvc文件,路徑:src\main\resources\spring\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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--註解使用組件掃描--> <context:component-scan base-package="ssm.controller"/> <!--加載mapper的映射文件--> <import resource="classpath*:spring/applicationContext-*.xml"/> <!--推薦使用下面的方式來配置註解處理器和適配器--> <mvc:annotation-driven/> <!-- 視圖解析器解析jsp解析,默認使用jstl標籤,classpath下的得有jstl的包--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
【5】配置前端控制器,
修改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_3_1.xsd"
version="3.1">
<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:spring/springmvc.xml</param-value>
</init-param>
<!--表示servlet隨服務啓動-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 第一種:*.action,訪問以.action結尾 由DispatcherServlet進行解析
第二種:/,所以訪問的地址都由DispatcherServlet進行解析,對於靜態文件的解析需要配置不讓DispatcherServlet進行解析
使用此種方式可以實現 RESTful風格的url
第三種:/*,這樣配置不對,使用這種配置,最終要轉發到一個jsp頁面時,仍然會由DispatcherServlet解析jsp地址,
不能根據jsp頁面找到handler,會報錯。-->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
【6】編寫controller(就是Handler)
新建文件,ItemsController.java路徑爲:src\main\java\ssm\controller\ItemsController.java,代碼如下:
package ssm.controller;
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 ssm.po.ItemsCustom;
import ssm.service.ItemsService;
import java.util.List;
/**
* Function:商品的controller
* Created by: chansonpro
* Date-Time: 2017/8/25 11:13
*/
@Controller
public class ItemsController {
@Autowired
private ItemsService itemsService;
@RequestMapping("/queryItems")
public ModelAndView queryItems() throws Exception {
//調用service查找數據庫,查詢商品列表
List<ItemsCustom> itemsList = itemsService.findItemsList(null);
//返回modelAndView
ModelAndView modelAndView = new ModelAndView();
// 相當 於request的setAttribute,在jsp頁面中通過itemsList取數據
modelAndView.addObject("itemsList",itemsList);
// 指定視圖
// 下邊的路徑,如果在視圖解析器中配置jsp路徑的前綴和jsp路徑的後綴,修改爲
// modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
// 上邊的路徑配置可以不在程序中指定jsp路徑的前綴和jsp路徑的後綴
modelAndView.setViewName("items/itemsList");
return modelAndView;
}
}
【7】編寫jsp文件,路徑爲:web\WEB-INF\jsp\items\itemsList.jsp,代碼如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查詢商品列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/item/queryItem.action" method="post">
查詢條件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查詢"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
<td>商品名稱</td>
<td>商品價格</td>
<td>生產日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemsList }" var="item">
<tr>
<td>${item.name }</td>
<td>${item.price }</td>
<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td>${item.detail }</td>
<td><a href="${pageContext.request.contextPath }/item/editItem.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
【8】運行程序。
整合service、controller完成。