【框架整合】spring+springmvc+mybatis整合
Spingmvc +mybatis整合
思路
第一步:整合dao層
mybatis和spring整合,通過spirng管理mapper介面。
使用mapper掃描器自動掃描mapper介面在spring中進行註冊。
第二步:整合service層,
通過spring管理service介面
使用配置方式在spring中對service介面進行註冊
實現事務控制
第三步:整合springmvc
1、dao整合
1.1、sqlMapConfig.xml
1.2、applicationContext-dao.xml
配置:
資料來源
sqlSessionFactory
mapper掃描器
1.3、配置 applicationContext-service.xml
配置:
事務
通知
aop
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 資料來源 --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 通知 --> <tx:advice id="txadvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="find*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- aop --> <aop:config> <aop:advisor advice-ref="txadvice" pointcut="execution(* cn.ssm.service.impl.*.*(..))"/> </aop:config> </beans>
1.4、逆向工程生成pojo和 mapper
查詢資料庫的表
items
建立包裝類和擴充套件類
因為可能不僅僅只查詢Items一張表,當涉及到多張表的查詢時,只有Items表的屬性是遠遠不夠的,所以為了系統的擴充套件性,需要定義擴充套件類和包裝類。
擴充套件類:逆向工程生成的pojo一般不要改動,當後期需要對商品的屬性進行擴充套件,就可以在擴充套件類裡面新增即可
包裝類:包裝類可以包裝任何pojo型別的資訊,可能有商品資訊(商品類),使用者資訊(使用者類),訂單資訊(訂單類)等等。那隻需要將我們定義的很多的擴充套件類引用到包裝類中,包裝類就可以使用所有的原始屬性和擴充套件屬性了。
這樣做的好處:在有很多類很多屬性的情況下清晰明瞭,便於管理;並可以將包裝類從表現層一直傳遞到持久層沒有一點鴨梨。。。。。
自定義商品查詢mapper
需求:根據商品名稱查詢商品
ItemsMapperCustom.xml
包裝類作為parameterType ,如果有多張表的子查詢,可以傳遞所有被包裝類的屬性。
擴充套件類作為resultType。現在的例子我們查詢的是items表,如果定義成Items,有可能會出現屬性不夠用的情況,所以定義成擴充套件類。
ItemsMapperCustom.java
2、整合service
定義service介面
實現service介面實現類
在applicaation-service.xml中配置service
整合springmvc
web.xml配置前端控制器
srpingmvc.xml
<!-- 可以掃描controller、service、 這裡掃描的是controller包 -->
<context:component-scan base-package="com.ssm.controller"></context:component-scan>
<!-- 檢視解析器 解析jsp,預設使用jstl標籤, -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 使用mvc:annotation-driven代替上面的註解對映器和註解介面卡 mvc:annotation-driven預設載入很多的引數繫結,比如json轉換解析器就預設載入了
實際開發使用mvc:annotation-driven -->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<!-- 自定義轉換器的類名 -->
<bean class="com.ssm.controller.converter.CustomDateConverter"></bean>
</property>
</bean>
</beans>
編寫Controller(就是Handler)
@Controller
@RequestMapping("/item")
public class ItemsHandler {
@Autowired
private ItemsService itemsService;
@RequestMapping("/itemsQuery")
public ModelAndView ItemsQueryList() throws Exception{
//System.out.println(request.getParameter("id"));
List<ItemsCustom> list = itemsService.findItemsList(null);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemsList", list);
modelAndView.setViewName("/items/itemslist");
return modelAndView;
}
編寫itemsList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/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/itemsQuery.action" method="get">
查詢條件
<table width="100%" border="1px">
<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/editItems.action?id=${item.id}" >修改</a>
</td>
<%-- id=${item.id} --%>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
載入spring容器
專案啟動時自動載入applicationContext.xml配置資訊
<!-- 載入spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!--1. 編譯後applicationaContextx.xml問價載入到該目錄下 -->
<!-- <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value> -->
<!--2. 專案中applicationContext-*.xml檔案的位置
兩種方式都可行
-->
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
ContextLoaderListener的作用
Spring提供ServletContentListener的一個實現類ContextLoaderListener監聽器,該類可以作為Listener使用,在啟動Tomcat
容器的時候,該類的作用就是自動裝載ApplicationContext的配置資訊,如果沒有設定contextConfigLocation的初始引數則會
使用預設引數WEB-INF路徑下的application.xml檔案。 --------------------- 原文:https://blog.csdn.net/java_wliang/article/details/18044507
商品修改功能開發
需求
操作流程: 1、進入商品查詢列表頁面
2、點選修改,進入商品修改頁面,頁面中顯示了要修改的商品(從資料庫查詢) 要修改的商品從資料庫查詢,根據商品id(主鍵)查詢商品資訊
3、在商品修改頁面,修改商品資訊,修改後,點選提交
開發mapper
1、首先根據id查詢出要修改的商品
Items selectByPrimaryKey(Integer id);
2、根據id修改表資料
int updateByPrimaryKeyWithBLOBs(ItemsCustom itemsCustom);
逆向工程已有
2、開發service
介面功能:
根據id查詢商品資訊
引數:id 返回值:ItemsCustom
修改商品資訊
引數 :id,接收修改後的資訊ItemsCusotm
3、開發Controller
@Controller
@RequestMapping("/item")
public class ItemsHandler {
@Autowired
private ItemsService itemsService;
@RequestMapping(value="/itemsQuery",method={RequestMethod.POST,RequestMethod.GET})
public ModelAndView ItemsQueryList() throws Exception{
//System.out.println(request.getParameter("id"));
List<ItemsCustom> list = itemsService.findItemsList(null);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemsList", list);
modelAndView.setViewName("/items/itemslist");
return modelAndView;
}
// @RequestMapping(value="/editItems" ,method={RequestMethod.POST,RequestMethod.GET})
// public ModelAndView editItems() throws Exception{
//
// ItemsCustom itemsCustom = itemsService.findItemsById(1);
//
// ModelAndView modelAndView = new ModelAndView();
//
// modelAndView.addObject("itemsCustom", itemsCustom);
//
// modelAndView.setViewName("/items/editItems");
// return modelAndView;
//
//
// }
@RequestMapping(value="/editItems" ,method={RequestMethod.POST,RequestMethod.GET})
public String editItems(HttpServletRequest request,Model model,@RequestParam(value="id") Integer item_id) throws Exception{
//String name = new String(request.getParameter("name").getBytes("ISO8859-1"),"utf-8");
ItemsCustom itemsCustom = itemsService.findItemsById(item_id);
model.addAttribute("itemsCustom", itemsCustom);
//modelAndView.setViewName("/items/editItems")
return "/items/editItems";
}
@RequestMapping("/editItemsSubmit")
public String editItemsSubmit(HttpServletRequest request,Integer id,ItemsCustom itemsCustom) throws Exception{
//String name = new String(request.getParameter("name").getBytes("ISO8859-1"),"utf-8");
itemsService.updateItems(id, itemsCustom);
return "forward:itemsQuery.action";
}