【Mybatis升级版-02】mybatis与Spring整合service及controller
在【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完成。