天天看點

可單例開發、典型的教科書式的mvc構架----springmvc----day01

springmvc-day01
     springmvc的基礎知識    1.springmvc架構
         1.1什麼是springmvc
             springmvc是spring架構的一個子產品,springmvc和spring無需通過中間整合層進行整合。
             springmvc是一個基于mvc的web架構。        1.2mvc在B/S系統下的應用
             mvc是一個設計模式,mvc在B/S系統下的應用:        1.3springmvc架構
             第一步:發起請求到前端控制器(DispatcherServlet)
             
             第二步:前端控制器請求HandlerMapping查找Handler
                 可以根據xml配置、注解進行查找
             
             第三步:處理器映射器HandlerMapping向前端控制器傳回Handler            第四步:前端控制器調用處理器擴充卡去執行Handler
            第五步:處理器擴充卡去執行Handler
            第六步:Handler執行完成給擴充卡傳回ModelAndView
            第七步:處理器擴充卡向前端控制器傳回ModelAndView
                 ModelAndView是springmvc架構的一個底層對象,包括Model和view            第八步:前端控制器請求視圖解析器去進行視圖解析
                 根據邏輯視圖名解析成真正的視圖(jsp)            第九步:視圖解析器向前端控制器傳回view
            第十步:前端控制器進行視圖渲染
                 視圖渲染将模型資料(在ModelAndView對象中)填充到request域。            第十一步:前端控制器向使用者響應結果。
            元件:
                 1.前端控制器DispatcherServlet(不需要程式員開發)
                 作用接收請求,響應結果,相當于轉發器,中央處理器。
                 有了DispatcherServlet減少了其他元件之間的耦合度。                2.處理器映射器HandlerMapping(不需要程式員開發)
                 作用:根據請求的url查找Handler                3.處理器擴充卡HandlerAdapter 
                 作用:按照特定規則(HandlerAdapter要求的規則)去執行Handler                4.處理器Handler(需要程式員開發)
                 注意:編寫Handler時按照HandlerAdapter的要求去做,這樣擴充卡才可以去正确執行Handler                5.視圖解析器View resolver(不需要程式員開發)
                 作用:進行視圖解析,根據邏輯視圖名解析成真正的視圖(view)                6.視圖View(需要程式員開發jsp)
                 View是一個接口,實作類支援不同的View類型(jsp、freemarker、pdf...)    2.入門程式
         2.1需求
             以案例作為驅動
             springmvc和mybatis使用一個案例(商品訂單管理)。
             功能需求:商品清單查詢        2.2環境準備
             資料庫環境:mysql
             java環境:
                 jdk
                 eclipse indigo            springmvc版本:spring3.2
            需要spring3.2所有jar(一定要包括spring-webmvc-x.x.x.RELEASE.jar)
        2.3配置前端控制器
             在web.xml中配置前端控制器。
             <!-- springmvc前端控制器 -->
             <servlet>
                 <servlet-name>springmvc</servlet-name>
                 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                 
                 <!-- contextConfigLocation配置springmvc加載的配置檔案(配置處理器、映射器、擴充卡等等)
                     如果不配置contextConfigLocation,預設加載的是/WEB-INF/servlet名稱-servlet.xml(springmvc-servlet.xml)
                  -->
                 <init-param>
                     <param-name>contextConfigLocation</param-name>
                     <param-value>classpath:springmvc.xml</param-value>
                 </init-param>
             </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>        2.4配置處理器擴充卡
             在classpath下的springmvc.xml中配置處理器擴充卡
             <!-- 處理器擴充卡 -->
             <!-- 所有處理器擴充卡都實作HandlerAdapter接口 -->
             <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>            通過檢視源代碼:
                 public class SimpleControllerHandlerAdapter implements HandlerAdapter {                    @Override
                     public boolean supports(Object handler) {
                         return (handler instanceof Controller);
                     }                    @Override
                     public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
                             throws Exception {                        return ((Controller) handler).handleRequest(request, response);
                     }                    @Override
                     public long getLastModified(HttpServletRequest request, Object handler) {
                         if (handler instanceof LastModified) {
                             return ((LastModified) handler).getLastModified(request);
                         }
                         return -1L;
                     }                }
            此擴充卡能執行實作Controller接口的Handler
                 public interface Controller {                    /**
                      * Process the request and return a ModelAndView object which the DispatcherServlet
                      * will render. A {@code null} return value is not an error: it indicates that
                      * this object completed request processing itself and that there is therefore no
                      * ModelAndView to render.
                      * @param request current HTTP request
                      * @param response current HTTP response
                      * @return a ModelAndView to render, or {@code null} if handled directly
                      * @throws Exception in case of errors
                      */
                     ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception;                }
        2.5開發Handler
             需要實作controller接口,才能由org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter擴充卡執行。
             public class ItemsController1 implements Controller {                @Override
                 public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
                     // 調用service查找資料庫,查詢商品清單,這裡使用靜态資料模拟
                     List<Items> itemsList = new ArrayList<Items>();                    // 向list中填充靜态資料
                     Items items_1 = new Items();
                     items_1.setName("聯想筆記本");
                     items_1.setPrice(6000f);
                     items_1.setDetail("ThinkPad T430 聯想筆記本電腦!");                    Items items_2 = new Items();
                     items_2.setName("蘋果手機");
                     items_2.setPrice(5000f);
                     items_2.setDetail("iphone6蘋果手機!");                    itemsList.add(items_1);
                     itemsList.add(items_2);                    // 傳回ModelAndView
                     ModelAndView modelAndView = new ModelAndView();
                     // 相當于resquest的setAttribute,在jsp頁面中通過itemsList取資料
                     modelAndView.addObject("itemsList", itemsList);
                     
                     //指定視圖
                     modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
                                         return modelAndView;
                 }            }
        2.6視圖編寫
        2.7配置Handler
             将編寫Handler在spring容器中加載。
                 <!-- 配置Handler -->
                 <bean id="itemsController1" name="/queryItems.action" class="com.changemax.ssm.controller.ItemsController1"></bean>        2.8配置處理器映射器
             在classpath下的springmvc.xml中配置處理器映射器                <!-- 處理器映射器 -->
                 <!-- 将bean的name作為url進行查找,需要在配置Handler時指定beanname(就是url) beanname的所有映射器都實作了HTTPResultHandler -->
                 <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />        2.9配置視圖解析器
             需要配置解析器jsp的視圖解析器
                 <!-- 視圖解析器 -->
                 <!-- 解析jsp視圖,預設使用jstl标簽,classpath下得有jstl的jar包 -->
                 <bean
                     class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                     <!-- 配置jsp路徑的字首 -->
                     <property name="prefix" value="/WEB-INF/jsp/" />
                     <!-- 配置jsp路徑的字尾 -->
                     <property name="suffix" value=".jsp" />
                 </bean>        2.10部署調試
             通路位址:http://localhost:8080/springmvc-day01/queryItems.action    3.非注解的處理器映射器的擴充卡
         3.1非注解的處理器映射器
             處理器映射器:
                 org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping            另一個映射器:
                 org.springframework.web.servlet.handler.SimpleUrlHandlerMapping             <!-- 處理器映射器 -->
             <!-- 将bean的name作為url進行查找,需要在配置Handler時指定beanname(就是url) beanname的所有映射器都實作了HTTPResultHandler -->
             <bean
                 class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />             <!-- 簡單url映射 -->
             <bean
                 class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
                 <property name="mappings">
                     <props>
                         對Itesmcontroller進行url位址映射,url就是queryItems.action
                         <prop key="/queryItems1.action">itemsController1</prop>
                         <prop key="/queryItems2.action">itemsController1</prop>
                         <prop key="/queryItems3.action">itemsController2</prop>
                     </props>
                 </property>
             </bean>            多個映射器可以并存,前端控制器判斷url能讓哪些映射器映射,就讓正确的映射器處理。
        3.2非注解的處理器擴充卡
             org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
             要求編寫的Handler實作Controller接口。
                 public class ItemsController1 implements Controller {                    @Override
                     public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
                         // 調用service查找資料庫,查詢商品清單,這裡使用靜态資料模拟
                         List<Items> itemsList = new ArrayList<Items>();             org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter
             要求編寫的Handler實作HttpRequestHandler接口
                 public class ItemsController2 implements HttpRequestHandler {                    @Override
                     public void handleRequest(HttpServletRequest request, HttpServletResponse response)
                             throws ServletException, IOException {
                         // 調用service查找資料庫,查詢商品清單,這裡使用靜态資料模拟
                         List<Items> itemsList = new ArrayList<Items>();            
             <!-- 處理器擴充卡 -->
             <!-- 所有處理器擴充卡都實作HandlerAdapter接口 -->
             <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>            <!-- 另一個非注解的擴充卡 -->
             <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" />    4.DispatcherServlet.properties
         前端控制器從上邊的檔案中加載處理器映射器、擴充卡、視圖解析器等元件,如果不在springmvc.xml中配置,使用預設加載的配置。     5.注解的處理器映射器和擴充卡
         映射器:
             在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping(過時)
             在spring3.1之後使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping        擴充卡:
             在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter(過時)
             在spring3.1之後使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter         5.1配置注解映射器和擴充卡
             <!-- 注解的映射器 -->
             <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>            <!-- 注解的擴充卡 -->
             <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
         
         
             <!-- mvc:annotation-driven代替上邊注解映射器和注解擴充卡的配置 
                 mvc:annotation-driven預設加載很多的參數綁定方法,比如json轉換解析器就預設加載了,
                 如果使用mvc:annotation-driven不用配置上邊的RequestMappingHandlerMapping和RequsetMappingHandleAdaper
                 實際開發中使用:mvc:annotation-driven
             -->        5.2開發注解Handler
             使用注解的映射器和注解的擴充卡。(注解的映射器和注解的擴充卡必須配對使用)
                 //使用@Controller注解辨別這個是個控制器
                 @Controller
                 public class ItemsController3 {
                     // 商品查詢清單
                     //@RequestMapping實作了對方法和url進行映射,一個方法對應一個url
                     //一般建議url和方法名一樣,便于維護
                     @RequestMapping("/queryItems")
                     public ModelAndView queryItems() throws Exception {
                         // 調用service查找資料庫,查詢商品清單,這裡使用靜态資料模拟
                         List<Items> itemsList = new ArrayList<Items>();                        // 向list中填充靜态資料
                         Items items_1 = new Items();
                         items_1.setName("聯想筆記本");
                         items_1.setPrice(6000f);
                         items_1.setDetail("ThinkPad T430 聯想筆記本電腦!");                        Items items_2 = new Items();
                         items_2.setName("蘋果手機");
                         items_2.setPrice(5000f);
                         items_2.setDetail("iphone6蘋果手機!");                        itemsList.add(items_1);
                         itemsList.add(items_2);                        ModelAndView modelAndView = new ModelAndView();
                        // 相當于resquest的setAttribute,在jsp頁面中通過itemsList取資料
                         modelAndView.addObject("itemsList", itemsList);                        // 指定視圖
                         //通過springmvc配置檔案,可以省掉jsp路徑的字首和字尾
                         modelAndView.setViewName("items/itemsList");
                         
                         return modelAndView;
                     }
                     
                 }        5.3在spring容器中加載Handler
             <!-- 對于注解的handler配置可以單個配置
                 在實際開發中,建議使用元件掃描
              -->
             <bean id="itemsController3"
                 class="com.changemax.ssm.controller.ItemsController3"></bean>
                 <!-- 可以使用掃描controller、service、...
                     這裡讓掃描controller,指定controller的包路徑
                 -->
                 <context:component-scan base-package="com.changemax.ssm.controller"></context:component-scan>        5.4部署調試
             通路:http://localhost:8080/springmvc-day01/queryItems.action     6源碼分析(了解)
         通過前端控制器源碼分析springmvc的執行過程。
         第一步:前端控制器接收請求
             調用doDiapatch        第二步:前端控制器調用處理器映射器查找Handler
        第三步:調用處理器擴充卡執行Handler,得到執行結果ModelAndView
        第四步:視圖渲染,将model資料填充到request域。
             視圖解析,得到view。
             調用view的渲染方法,将model資料填充到request域    7.入門程式小結
         通過入門程式了解springmvc前端控制器、處理器映射器、處理器擴充卡、視圖解析器用法。
             前端控制器配置:
                 第一種:*.action,通路以.action結尾    由DispatcherServlet進行解析                第二種:/,是以通路的位址都由DispatcherServlet進行解析,對于靜态檔案的解析需要配置不讓DispatcherServlet進行解析
                     使用此種方式可以實作RESTful風格的url            處理器映射器:
                 非注解處理器映射器(了解)
                 注解的處理器映射器(掌握)
                     對标記@Controller類中辨別有@RequestMapping的方法進行映射。在@RequestMapping裡邊定義映射的url。
                     使用注解的映射器不用再xml中配置url和Handler的映射關系。            處理器擴充卡:
                 非注解的處理器擴充卡(了解)
                 注解的處理器擴充卡(掌握)
                     注解處理器擴充卡的注解的處理器映射器是配對使用。了解為不能使用非注解映射器進行映射。                    <mvc:annotation-driven></mvc:annotation-driven>可以替代下邊的配置:
                         <!--注解映射器 -->
                         <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
                         <!--注解擴充卡 -->
                         <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>                    實際開發使用:mvc:annotation-driven
            視圖解析器配置字首和字尾:
                 <!-- 視圖解析器 -->
                 <!-- 解析jsp視圖,預設使用jstl标簽,classpath下得有jstl的jar包 -->
                 <bean
                     class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                     <!-- 配置jsp路徑的字首 -->
                     <property name="prefix" value="/WEB-INF/jsp/" />
                     <!-- 配置jsp路徑的字尾 -->
                     <property name="suffix" value=".jsp" />
                 </bean>                如此配置之後,就可以在程式中不用指定字首和字尾
     8.springmvc和mybatis整合
         
         8.1需求
             使用springmvc和mybatis完成商品清單查詢。        8.2整合思路
             spring将各層進行整合
             通過spring管理持久層的mapper(相當于dao接口)
             通過spring管理業務層service,service中可以調用mapper接口。
             spring進行事物控制。            通過spirng管理表現層Handler,Handler中可以調用service接口。
            mapper、service、Handler都是javabean。
            第一步:整合dao層:
                 mybatis和spring整合,通過spring管理mapper接口。
                 使用mapper的掃描器自動掃描mapper接口在spring中進行注冊。            第二步:整合service層
                 通過spring管理service接口。
                 使用配置方式将service接口配置在spring配置檔案中。
                 實作事務控制。            第三步:整合springmvc
                 由于springmvc是spring的子產品,不需要整合。        8.3準備環境
             資料庫環境:mysql
             Java環境:
                 jdk1.8
                 eclipse indigo
                 springmvc4.3            所需要jar包;
                 資料庫驅動包:mysql
                 mybatis的jar包
                 mybatis和spring的整合包
                 log4j包
                 dbcp資料庫連接配接池包
                 spring3.2所有jar包
                 jstl包         8.4整合dao
             mybatis和spring進行整合            8.4.1sqlMapConfig.xml
                 mybatis自己的配置檔案:
                     <?xml version="1.0" encoding="UTF-8" ?>
                     <!DOCTYPE configuration
                     PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
                     "http://mybatis.org/dtd/mybatis-3-config.dtd">
                     <configuration>
                         <!-- 全局的setting配置,根據需要添加 -->                        <!-- 别名定義 -->
                         <typeAliases>
                             <package name="com.changemax.ssm.po" />
                         </typeAliases>                         <!-- 加載 映射檔案 由于使用spring和mybatis的整合包進行mapper掃描,這裡不需要配置了 -->
                         <mappers>
                             <!-- <mapper resource="sqlmap/User.xml" /> -->
                             <!-- 批量加載mapper 指定mapper接口的包名,mybatis自動掃描包下邊所有mapper接口進行加載 遵循一些規範: 需要将mapper接口類名和mapper.xml映射檔案名稱保持一緻,且在一個目錄 
                                 中 上邊規範的前提是:使用的是mapper代理方法 和spring整合後,使用mapper掃描器,這裡不需要配置了 -->
                             <!-- <package name="com.changemax.ssm.mapper" /> -->
                         </mappers>
                     </configuration>            8.4.2applicationContext-jdbc.xml
                 配置:
                     資料源
                     SqlSessionFactory
                     mapper掃描器                    <?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:mvc="http://www.springframework.org/schema/mvc"
                         xmlns:context="http://www.springframework.org/schema/context"
                         xmlns:aop="http://www.springframework.org/schema/aop"
                         xmlns:tx="http://www.springframework.org/schema/tx"
                         xsi:schemaLocation="http://www.springframework.org/schema/beans 
​​​                             http://www.springframework.org/schema/beans/spring-beans-3.2.xsd ​​​                             http://www.springframework.org/schema/mvc 
                             http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
                             http://www.springframework.org/schema/context 
                             http://www.springframework.org/schema/context/spring-context-3.2.xsd 
                             http://www.springframework.org/schema/aop 
                             http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
                             http://www.springframework.org/schema/tx 
                             http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">                        <!-- 和jdbc配置相關的 -->
                        <!-- 加載db.properties檔案中的内容,db.properties檔案中的key命名要具有一定的特殊規則 -->
                         <context:property-placeholder
                             location="classpath:db.properties" />                        <!-- 配置資料源,c3p0連接配接池 -->
                         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
                             destroy-method="close">
                             <property name="driverClassName" value="${jdbc.driver}" />
                             <property name="url" value="${jdbc.url}" />
                             <property name="username" value="${jdbc.username}" />
                             <property name="password" value="${jdbc.password}" />
                             <property name="maxActive" value="30" />
                             <property name="maxIdle" value="5" />
                         </bean>                        <!-- 配置sqlSessionFactory -->
                         <bean id="sqlSessionFactory"
                             class="org.mybatis.spring.SqlSessionFactoryBean">
                             <!-- 資料庫連接配接池 -->
                             <property name="dataSource" ref="dataSource" />
                             <!-- 加載mybatis的全局配置檔案 -->
                             <property name="configLocation"
                                 value="classpath:mybatis/sqlMapConfig.xml" />
                         </bean>                        <!-- mapper掃描器 -->
                         <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
                             <!-- 掃描包路徑,如果需要掃描多個包,中間使用半形逗號隔開 -->
                             <property name="basePackage" value="com.changemax.ssm.mapper"></property>
                             <property name="sqlSessionFactoryBeanName"
                                 value="sqlSessionFactory" />
                         </bean>                    </beans>
            8.4.3applicationContext-transaction.xml
                 
                         8.4.4逆向工程生成po類及mapper(單表增删改查)
                 将生成的檔案拷貝至工程中。            8.4.5手動定義商品查詢mapper
                 針對綜合查詢mapper,一般情況會有關聯查詢,建議自定義mapper                8.4.5.1ItemsMapperCustom.xml
                     <?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="com.changemax.ssm.mapper.ItemsMapperCustom" >                       <!-- 定義商品查詢的sql片段,就是商品查詢條件 -->
                        <sql id="query_items_where">
                            <!-- 使用動态sql,通過if判斷,滿足條件進行sql拼接 -->
                            <!-- 商品查詢條件通過ItemsQueryVo包裝對象 中itemsCustom屬性傳遞 -->
                                <if test="itemsCustom!=null">
                                    <if test="itemsCustom.name!=null and itemsCustom.name!=''">
                                        items.name LIKE '%${itemsCustom.name}%'
                                    </if>
                                </if>
                         
                        </sql>
                           
                           <!-- 商品清單查詢 -->
                           <!-- parameterType傳入包裝對象(包裝了查詢條件)
                               resultType建議使用擴充對象
                            -->
                           <select id="findItemsList" parameterType="com.changemax.ssm.po.ItemsQueryVo"
                                resultType="com.changemax.ssm.po.ItemsCustom">
                               SELECT items.* FROM items  
                               <where>
                                   <include refid="query_items_where"></include>
                               </where>
                           </select>
                           
                     </mapper>                8.4.5.2ItemsMapperCustom.java
                     package com.changemax.ssm.mapper;                    import java.util.List;
                    import com.changemax.ssm.po.ItemsCustom;
                     import com.changemax.ssm.po.ItemsQueryVo;                    public interface ItemsMapperCustom {
                         //商品查詢清單
                         public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;
                     }         8.5整合service
             讓spring關聯service接口。            8.5.1定義service接口
                 public interface ItemsService {
                     /**
                      * 商品查詢清單
                      * <p>
                      * Title: findItemsList
                      * </p>
                      * <p>
                      * Description:
                      * </p>
                      * 
                      * @param itemsQueryVo
                      * @return
                      * @throws Exception
                      */
                     public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;                    /**
                      * 根據id查詢商品資訊
                      * <p>
                      * Title: selectItemById
                      * </p>
                      * <p>
                      * Description:
                      * </p>
                      * 
                      * @param id
                      * @return
                      * @throws Exception
                      */
                     public ItemsCustom selectItemById(Integer id) throws Exception;                    /**
                      * 根據id更新商品資訊s
                      * <p>
                      * Title: updateItemsById
                      * </p>
                      * <p>
                      * Description:
                      * </p>
                      * 
                      * @param id
                      * @param itemsCustom
                      * @return
                      * @throws Exception
                      */
                     public Integer updateItemsById(Integer id, ItemsCustom itemsCustom) throws Exception;
                 }                 @Service("itemsService")
                 public class ItemsServiceImpl implements ItemsService {                    @Autowired
                     private ItemsMapperCustom itemsMapperCustom;                    @Autowired
                     ItemsMapper itemsMapper;                    @Override
                     public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception {
                         // 通過ItemsMappCustom查詢
                         return itemsMapperCustom.findItemsList(itemsQueryVo);
                     }                    @Override
                     public ItemsCustom selectItemById(Integer id) throws Exception {
                         Items items = itemsMapper.selectByPrimaryKey(id);
                         // 通過ItemsMappCustom查詢
                         ItemsCustom itemsCustom = new ItemsCustom();
                         BeanUtils.copyProperties(items, itemsCustom);
                         return itemsCustom;                    }
                    @Override
                     public Integer updateItemsById(Integer id, ItemsCustom itemsCustom) throws Exception {                        // 添加業務檢驗,通常在service接口對關鍵參數進行校驗
                         // 校驗id是否為空,如果為空則抛出異常                        // 更新商品資訊使用updateByParimaryKeyWithBLOBs根據id更新items表中的所有字段,包括大文本類型字段
                         // updateByPrimaryKeyWithBLOBs要求必須轉入id
                         itemsCustom.setId(id);
                         Integer i = itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom);
                         return i;
                     }                }
            8.5.2在spring容器配置service(applictionContext-service.xml)
                 略,通過注解配置            8.5.3事務控制(applicationContext-transaction.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:mvc="http://www.springframework.org/schema/mvc"
                     xmlns:context="http://www.springframework.org/schema/context"
                     xmlns:aop="http://www.springframework.org/schema/aop"
                     xmlns:tx="http://www.springframework.org/schema/tx"
                     xsi:schemaLocation="http://www.springframework.org/schema/beans 
​​​                         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd ​​​                         http://www.springframework.org/schema/mvc 
                         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
                         http://www.springframework.org/schema/context 
                         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
                         http://www.springframework.org/schema/aop 
                         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
                         http://www.springframework.org/schema/tx 
                         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
                         
                     <!-- 和事務相關的配置 -->
                     <!-- 配置事務管理器 -->
                     <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" read-only="false" />
                             <tx:method name="update*" propagation="REQUIRED" read-only="false" />
                             <tx:method name="delete*" propagation="REQUIRED" read-only="false" />
                             <tx:method name="insert*" propagation="REQUIRED" read-only="false" />
                             <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
                             <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
                             <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
                         </tx:attributes>
                     </tx:advice>
                     <!-- 配置aop -->
                     <aop:config>
                         <!-- 配置切入點表達式 -->
                         <aop:pointcut
                             expression="execution(* com.changemax.ssm.service.impl.*.*(..))" id="pt1" />
                         <!-- 建立切入點表達式和事務通知的關聯 -->
                         <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1" />
                     </aop:config>
                 </beans>         8.6整合springmvc
             8.6.1springmvc.xml
                 建立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:mvc="http://www.springframework.org/schema/mvc"
                         xmlns:context="http://www.springframework.org/schema/context"
                         xmlns:aop="http://www.springframework.org/schema/aop"
                         xmlns:tx="http://www.springframework.org/schema/tx"
                         xsi:schemaLocation="http://www.springframework.org/schema/beans 
​​​                             http://www.springframework.org/schema/beans/spring-beans-3.2.xsd ​​​                             http://www.springframework.org/schema/mvc 
                             http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
                             http://www.springframework.org/schema/context 
                             http://www.springframework.org/schema/context/spring-context-3.2.xsd 
                             http://www.springframework.org/schema/aop 
                             http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
                             http://www.springframework.org/schema/tx 
                             http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">                        <!-- 對于注解的handler配置可以單個配置 在實際開發中,建議使用元件掃描 -->
                         <!-- <bean id="itemsController3" class="com.changemax.ssm.controller.ItemsController3"></bean> -->
                         <!-- 可以使用掃描controller、service、... 這裡讓掃描controller,指定controller的包路徑 -->
                         <context:component-scan
                             base-package="com.changemax.ssm.controller"></context:component-scan>                         <!-- 注解的映射器 -->
                         <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->                        <!-- 注解的擴充卡 -->
                         <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->                        <!-- mvc:annotation-driven代替上邊注解映射器和注解擴充卡的配置 mvc:annotation-driven預設加載很多的參數綁定方法,比如json轉換解析器就預設加載了, 
                             如果使用mvc:annotation-driven不用配置上邊的RequestMappingHandlerMapping和RequsetMappingHandleAdaper 
                             實際開發中使用:mvc:annotation-driven -->
                         <mvc:annotation-driven
                             conversion-service="conversionService">
                         </mvc:annotation-driven>                        <!-- 視圖解析器 -->
                         <!-- 解析jsp視圖,預設使用jstl标簽,classpath下得有jstl的jar包 -->
                         <bean
                             class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                             <!-- 配置jsp路徑的字首 -->
                             <property name="prefix" value="/WEB-INF/jsp/" />
                             <!-- 配置jsp路徑的字尾 -->
                             <property name="suffix" value=".jsp" />
                         </bean>                         <!-- 自定義參數綁定 -->
                         <bean id="conversionService" class="">
                             <!-- 轉換器 -->
                             <property name="converters">
                                 <list>
                                     <!-- 日期類型轉換 -->
                                     <bean class="com.changemax.ssm.controller.CustomDateConverter" />
                                 </list>
                             </property>
                         </bean>
                     </beans>            8.6.2配置前端控制器
                 參考以上            8.6.3編寫Controller(就是Handler)
                 package com.changemax.ssm.controller;                import java.util.List;
                import javax.servlet.http.HttpServletRequest;
                import org.springframework.beans.factory.annotation.Autowired;
                 import org.springframework.stereotype.Controller;
                 import org.springframework.ui.Model;
                 import org.springframework.web.bind.annotation.RequestMapping;
                 import org.springframework.web.bind.annotation.RequestMethod;
                 import org.springframework.web.bind.annotation.RequestParam;
                 import org.springframework.web.servlet.ModelAndView;                import com.changemax.ssm.po.ItemsCustom;
                 import com.changemax.ssm.service.ItemsService;                /**
                  * <p>
                  * Title: ItemsController.java
                  * </p>
                  * <p>
                  * Description:
                  * </p>
                  * <p>
                  * Company: www.changemax.com
                  * </p>
                  * 
                  * @author WangJi
                  * @date 2018年11月22日
                  * @version 1.0
                  */
                 @Controller
                 //為了對url進行分類管理,可以在這裡定義根路徑,最終通路url是根路徑+子路徑
                 @RequestMapping("/items")
                 public class ItemsController {                    @Autowired
                     private ItemsService itemsService;                    // 商品查詢
                     @RequestMapping("/queryItems")
                     public ModelAndView queryItems(HttpServletRequest request) throws Exception {
                         // 調用service查找資料庫,查詢商品清單
                         List<ItemsCustom> itemsList = itemsService.findItemsList(null);                        ModelAndView modelAndView = new ModelAndView();
                        // 相當于resquest的setAttribute,在jsp頁面中通過itemsList取資料
                         modelAndView.addObject("itemsList", itemsList);                        // 指定視圖
                         // 通過springmvc配置檔案,可以省掉jsp路徑的字首和字尾
                         modelAndView.setViewName("items/itemsList");                        return modelAndView;
                     }
                 }
             
             8.6.4編寫jsp         8.7加載spring容器
             将mapper、service、controller加載到spring容器中。
             建議使用通配符加載上邊的配置檔案。
             在web.xml中,添加spring容器監聽器,加載spring容器。                <!-- 加載spring容器 -->
                 <context-param>
                     <param-name>contextConfigLocation</param-name>
                     <!-- <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value> -->
                     <param-value>/WEB-INF/classes/spring/applicationContext.xml</param-value>
                 </context-param>
                 <listener>
                     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
                 </listener>    9.商品修改功能開發
         9.1需求:
             操作流程:
                 1.進入商品查詢清單頁面
                 2.點選修改,進入商品修改頁面,頁面中顯示了要修改的商品(從資料庫查詢),要修改的商品從資料庫查詢,根據商品id(主鍵)查詢商品資訊。
                 3.在商品修改頁面,修改商品資訊,修改後,點選送出。        9.2開發mapper
             mapper:
                 根據id查詢商品新
                 根據id更新Items表的資料
             不用開發了,使用逆向工程生成的代碼。        9.3開發service
             接口功能:
                 根據id查詢商品新
                 修改商品資訊
                 /**
                  * 根據id查詢商品資訊
                  * <p>
                  * Title: selectItemById
                  * </p>
                  * <p>
                  * Description:
                  * </p>
                  * 
                  * @param id
                  * @return
                  * @throws Exception
                  */
                 public ItemsCustom selectItemById(Integer id) throws Exception;                /**
                  * 根據id更新商品資訊s
                  * <p>
                  * Title: updateItemsById
                  * </p>
                  * <p>
                  * Description:
                  * </p>
                  * 
                  * @param id
                  * @param itemsCustom
                  * @return
                  * @throws Exception
                  */
                 public Integer updateItemsById(Integer id, ItemsCustom itemsCustom) throws Exception;        9.4開發controller
             方法:
                 商品資訊修改頁面顯示
                 商品資訊修改送出     10@RequestMapping
         *url映射
             定義controller方法對應的url,進行處理器映射使用。        *窄化請求映射
             @Controller
             //為了對url進行分類管理,可以在這裡定義根路徑,最終通路url是根路徑+子路徑
             @RequestMapping("/items")
             public class ItemsController {        *限制http請求方法
             出于安全性考慮,對http的連結進行方法限制。
             如果限制請求為post方法,進行get請求,報錯,
                 @RequestMapping(value = "/editItems", method = { RequestMethod.GET, RequestMethod.POST })
             11.controller方法的傳回值
         *傳回ModelAndView
             需要方法結束時,定義ModelAndView,将model和view分别進行設定。        *傳回String
             如果controller方法傳回string            1.表示傳回邏輯視圖名。
                 真正視圖(jsp路徑) = 字首 + 邏輯視圖名 + 字尾                // 商品修改頁面顯示
                 //    @RequestMapping("/editItems")
                     // 限制http的請求方法
                     @RequestMapping(value = "/editItems", method = { RequestMethod.GET, RequestMethod.POST })
                     // @@RequestParam裡面指定request傳入參數名稱和形參進行形參綁定
                     // 通過required屬性指定參數是否傳入
                     // 通過defaultValue可以設定預設值,如果id參數沒有傳入,将預設值和形參綁定
                     public String editItems(Model model,
                             @RequestParam(value = "id", required = true/* , defaultValue="" */) Integer items_id) throws Exception {                        // 調用sevice根據商品id查詢商品資訊
                         ItemsCustom itemsCustom = itemsService.selectItemById(items_id);                //        ModelAndView modelAndView = new ModelAndView();
                 //
                 //        // 相當于resquest的setAttribute,在jsp頁面中通過itemsList取資料
                 //        modelAndView.addObject("itemsCustom", itemsCustom);
                 //
                 //        // 指定視圖
                 //        // 通過springmvc配置檔案,可以省掉jsp路徑的字首和字尾
                 //        modelAndView.setViewName("items/editItems");                        model.addAttribute("itemsCustom", itemsCustom);
                        return "items/editItems";
                     }            2.redirect重定向
                 商品修改送出後,重定向到商品查詢清單。
                 redirect重定向特點:浏覽器位址欄中的url會變化,修改送出的request資料無法傳到重定向的位址。因為重定向後重新進行request(request無法共享)
                     // 重定向
                     // return "redirect:queryItems.action";            3.forward頁面轉發
                 通過forward進行頁面轉發,浏覽器位址欄url不變,request可以共享。
                     // 請求轉發·
                     return "forword:queryItems.action";        *傳回void
             在controller方法形參上可以定義request和response,使用request或response指定響應結果:
                 1.使用request轉向頁面,如下:
                     request.getRequestDispatcher("頁面路徑").forward(request,response);                2.也可以通過response頁面重定向:
                     response.sendRedirect("url");                3.也可以通過response指定響應結果,例如響應json資料如下:
                     response.setCharacterEncoding("utf-8");
                     response.setContentType("application/json:charset=utf-8");
                     response.getWriter().write("json串");    12參數綁定
         12.1spring參數綁定過程
             從用戶端請求key/value資料,經過參數綁定,将key/value資料綁定到controller方法的形參上。            springmvc中,接收頁面送出的資料是通過方法形參來接收。而不是在controller類定義成員變更接收
            處理器擴充卡調用springmvc提供參數綁定元件将key/value資料轉成controller方法的形參
            參數綁定元件:在spirngmvc早期版本使用PropertyEditor(隻能将字元串傳成java對象)
             後期使用converter(進行任意類型的傳換)
             spirngmvc提供了很多converter(轉換器)
             在特殊情況下需要自定義converter
             對日期資料綁定需要自定義converter         12.2預設支援的類型
             直接在controller方法形參上定義下邊類型的對象,就可以使用這些對象。在參數綁定過程中,如果遇到下邊類型直接進行綁定。            12.2.1HttpServletRequest
                 通過request對象擷取請求資訊            12.2.2HttpServletResponse
                 通過response處理響應資訊            12.2.3HttpSession
                 通過session對象得到session中存放的對象            12.2.4Model/ModelMap
                 model是一個接口,modelMap是一個接口實作。
                 作用:将model資料填充到request域。        12.3簡單類型
             通過@RequestParam對簡單類型的參數進行綁定。
             如果不使用@RequestParam,要求request傳入參數名稱和controller方法的形參名稱一緻,方可綁定成功。            如果使用@RequestParam,不用限制request傳入參數名稱和controller方法的形參名稱一緻。
            通過required屬性指定參數是否必須要傳入,如果設定true,沒有傳入參數,報下邊錯誤:
                 @RequestParam(value = "id", required = true/* , defaultValue="" */) Integer items_id) throws Exception {        12.4pojo綁定
             頁面中的input的name和controller的pojo形參中的屬性名稱一緻,将頁面中資料綁定到pojo。            頁面定義:
                 <form id="itemForm"
                     action="${pageContext.request.contextPath }/items/editItems.action"
                     method="post" enctype="multipart/form-data">
                     <input type="hidden" name="id" value="${itemsCustom.id }" /> 修改商品資訊:
                     <table width="100%" border=1>
                         <tr>
                             <td>商品名稱</td>
                             <td><input type="text" name="name" value="${itemsCustom.name }" /></td>
                         </tr>
                         <tr>
                             <td>商品價格</td>
                             <td><input type="text" name="price"
                                 value="${itemsCustom.price }" /></td>
                         </tr>
                         <tr>
                             <td>商品生産日期</td>
                             <td><input type="text" name="createtime"
                                 value="<fmt:formatDate value="${itemsCustom.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>" /></td>
                         </tr>
                         <tr>
                             <td>商品圖檔</td>
                             <td><c:if test="${itemsCustom.pic !=null}">
                                     <img src="/pic/${itemsCustom.pic}" width=100 height=100 />
                                     <br />
                                 </c:if> <input type="file" name="pictureFile" /></td>
                         </tr>
                         <tr>
                             <td>商品簡介</td>
                             <td><textarea rows="3" cols="30" name="detail">${itemsCustom.detail }</textarea>
                             </td>
                         </tr>
                         <tr>
                             <td colspan="2" align="center"><input type="submit" value="送出" />
                             </td>
                         </tr>
                     </table>                </form>
            controller的pojo形參的定義:
                 public class Items {
                     private Integer id;                    private String name;
                    private Float price;
                    private String pic;
                    private Date createtime;
                    private String detail;
         12.5自定義參數綁定實作日期類型綁定
             對于controller形參中pojo對象,如果屬性中有日期類型,需要自定義參數綁定。
             是以自定義參數綁定将日期轉換成java.util.Date類型。
             需要向處理器擴充卡中注入自定義的參數綁定元件。            12.5.1自定義日期類型綁定:
                 public class CustomDateConverter implements Converter<String, Date> {                    @Override
                     public Date convert(String source) {
                         // 實作将日期串轉成util包下的日期類型(格式為:yyyy-MM-dd HH:mm:ss)
                         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");                        try {
                             // 轉成直接傳回
                             return simpleDateFormat.parse(source);
                         } catch (ParseException e) {
                             // TODO Auto-generated catch block
                             e.printStackTrace();
                         }
                         // 如果參數綁定失敗放回null
                         return null;
                     }                }
            12.5.2配置方式:
                 
                 <mvc:annotation-driven
                     conversion-service="conversionService">
                 </mvc:annotation-driven>                    <!-- 自定義參數綁定 -->
                 <bean id="conversionService" class="">
                     <!-- 轉換器 -->
                     <property name="converters">
                         <list>
                             <!-- 日期類型轉換 -->
                             <bean class="com.changemax.ssm.controller.converter.CustomDateConverter" />
                         </list>
                     </property>
                 </bean>    13.springmvc和struts2的差別
         1.springmvc基于方法開發的,struts2基于類開發的。
             springmvc将url和controller方法映射。映射成功後springmvc生成一個Handler對象,對象中隻包括了一個method。方法執行結束,形參資料銷毀。
             springmvc的controller開發類似service開發。        2.springmvc可以進行單例開發,并且建議使用單例開發,struts2通過類的成員變量接收參數,無法使用單例,隻能使用多例。
        3.經過實際測試,struts2速度慢,在于使用struts标簽,如果使用struts建議使用jstl。
     14.問題
         14.1post亂碼
             在web.xml添加post亂碼filter
             在web.xml中加入:
                 <!-- post亂碼過濾器 -->
                 <filter>
                     <filter-name>CharacterEncodingFilter</filter-name>
                     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
                     <init-param>
                         <param-name>encoding</param-name>
                         <param-value>utf-8</param-value>
                     </init-param>
                 </filter>
                 <filter-mapping>
                     <filter-name>CharacterEncodingFilter</filter-name>
                     <url-pattern>/*</url-pattern>
                 </filter-mapping>            以上就可以解決post請求亂碼問題。
             對于get請求中文參數亂碼問題解決方法有兩個:
                 1.修改tomcat配置檔案添加編碼與工程編碼一緻,如下:
                     <Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>                2.另外一種方法對參數進行重新編碼:
                     String userName = new String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8")