Spring+MVC+Mybatis整合

本文是對慕課網上"搞定SSM開發"路徑的系列課程的總結,詳細的項目文檔和課程總結放在github上了.點擊查看前端

什麼是秒殺業務

網站售賣某產品時,規定在某個日期開始售賣限量的產品,最典型的好比小米的開售;這種狀況下,可能有不少用戶對同一產品在同一時間請求購買,併發數特別高,因此對數據庫和網絡的設計要求比較高.jquery

秒殺系統的業務分析

秒殺系統最關鍵的部分是對庫存的訪問與修改,可能存在同一時間對數據庫裏的同一字段大量的訪問,如何保證查詢的時間比較短,讓儘量多的用戶儘快訪問,是此類業務的關鍵.git

分層

  • Dao層: 數據層,主要是對數據的訪問;
  • Service層: 業務層,封裝主要的業務邏輯
    • exception層: 定義全部exception
  • Web層: 展現給用用戶,即前臺頁面

注意要多寫註釋
***github

Dao層:

  1. 使用sql建立相關的數據庫和表;
  2. 建立實體,通常對應數據庫的一張表;
  3. 建立Dao層接口,定義預先定義的方法;
  4. 使用Mybatis實現Dao接口,通常來講使用Mapper中的namespace對應到對應接口,Mybatis會自動建立其實現類;
    [mapper namespace]
    [resultMap]
    [seelct id]
    具體可參考系列文檔中關於mybatis的總結
  5. 寫好Mybatis的xml配置文件,啓用Mybatis的功能:
    mybatis-config.xml的基本配置:
<configuration>
    <!-- 配置全局屬性 -->  
    <settings>  
        <!-- 使用jdbc的getGeneratedKeys獲取數據庫自增主鍵   -->
        <setting name="useGeneratedKeys" value="true"/>
        <!-- 使用列別名替換列名 -->
        <setting name="useColumnLabel" value="true"/>
        <!-- 開啓駝峯命名轉換  下劃線命名到駝峯命名的轉換-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>     
    </settings>
    </configuration>

(如下序號應當從6開始)web

  1. Spring整合Mybatis:spring-dao.xml
    1. 引入property文件;
    2. 建立數據源-通常是c3p0
    3. SqlSessionFactory的配置(屬性:鏈接池,配置文件-全局文件指定*)
    4. 配置掃描接口,指定掃描哪些包下的Dao接口
  2. 單元測試

Service層:

1.Service層關注業務怎麼實現,一開始級應該設計好

從"使用者"的角度設計接口:redis

  • 方法定義粒度
  • 參數要好處理
  • 返回類型
  • 寫好註釋
2.業務代碼,愛咋寫咋寫,按層次便可
3.經過Spring IOC管理全部的service層代碼

使用註解的話,@Service,@Bean,@Autowired等註解實現bean的託管和依賴注入
如下爲Spring註解開啓的示例文件.spring

<?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:aop="http://www.springframework.org/shcema/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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
        <!-- 掃描service報下全部使用註解的類型 -->
        <context:component-scan base-package="com.ct.maven.SecKill.service"></context:component-scan>
       
        <!-- 配置事務管理器 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!-- 注入數據庫鏈接池 -->
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
4.使用集成測試

Web層:

[restful] [bootStrap+jquery] [SpringMVC]sql

1:restful接口設計,其實是uri的定義規範,資源的狀態或者資源狀態的轉移;Spring註解很方便地和resultful適配
2:編寫controller,詳情可參見SpringMVC的總結.
3:配置mvc
  • 開啓spring mvc註解 mvc:annotion-driven
  • 靜態資源處理
  • viewResolver配置
  • 掃描web相關的bean;實際上,關於bean的定義能夠在統一的文件包中,而沒必要要分在三個層中進行配置.

Dao+Web+Service整合

  • 配置dispatcher(servlet)
  • 配置contextConfiglocation(這裏就是全部的層的整合,當spring讀取到全部的xml文件後,即可以互相調用和整合,由於實在一個完整的spring容器中)
  • 配置servlet-mapping

秒殺業務的深刻,高併發的優化

三種途徑

1:前端cdn訪問
2:redis緩存處理;
redis有windows版本可使用
3:併發訪問,經過存儲過程將一系列的操做一塊兒進行,下降由於網絡延遲致使的行級鎖數據庫

相關文章
相關標籤/搜索