SpringMVC學習--springmvc和mybatis整合

来源:http://www.cnblogs.com/lcngu/archive/2016/05/26/5507181.html
-Advertisement-
Play Games

簡介 springMVC是表現層,service充當業務層,mybatis作為持久層,通過spring將這三層整合起來。如下圖: 第一步:整合dao層 mybatis和spring整合,通過spring管理mapper介面。使用mapper的掃描器自動掃描mapper介面在spring中進行註冊。 ...


  • 簡介

  springMVC是表現層,service充當業務層,mybatis作為持久層,通過spring將這三層整合起來。如下圖:

  第一步:整合dao

  mybatisspring整合,通過spring管理mapper介面。使用mapper的掃描器自動掃描mapper介面在spring中進行註冊。

  第二步:整合service

  通過spring管理 service介面,使用配置方式將service介面配置在spring配置文件中,實現事務控制。

  第三步:整合springmvc

  由於springmvcspring的模塊,不需要整合。

  • 整合Dao層

  也就是Spring與MyBatis的整合

  1、MyBatis的配置:SqlMapConfig.xml

 

1 <configuration>
2     <typeAliases>
3         <!-- 批量別名定義,掃描整個包下的類,別名為類名(首字母大寫或小寫都可以) -->
4         <package name="com.luchao.pojo" />
5     </typeAliases>    
6 </configuration>

 

  2、Dao的配置:applicationContext-dao.xml

  配置數據源、SqlSessionFactory、Mapper掃描器

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 3     xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
 7         http://www.springframework.org/schema/mvc 
 8         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
 9         http://www.springframework.org/schema/context 
10         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
11         http://www.springframework.org/schema/aop 
12         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
13         http://www.springframework.org/schema/tx 
14         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
15     <!-- 載入配置文件 -->
16     <context:property-placeholder location="classpath:db.properties" />
17     <!-- 數據源,使用dbcp -->
18     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
19         destroy-method="close">
20         <property name="driverClassName" value="${jdbc.driver}" />
21         <property name="url" value="${jdbc.url}" />
22         <property name="username" value="${jdbc.username}" />
23         <property name="password" value="${jdbc.password}" />
24         <property name="maxActive" value="10" />
25         <property name="maxIdle" value="5" />
26     </bean>
27     <!-- sqlSessinFactory -->
28     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
29         <!-- 載入mybatis的配置文件 -->
30         <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
31         <!-- 數據源 -->
32         <property name="dataSource" ref="dataSource" />
33     </bean>
34     <!-- 配置mapper掃描器 -->
35     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
36         <property name="basePackage" value="com.luchao.mapper"></property>
37         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
38     </bean>
39 </beans>

  3、定於Po、Mapper介面和Mapper映射文件。

  • 整合Service

  讓spring管理service

  定於service介面:

1 public interface ItemsService {
2     // 獲取商品列表
3     public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)
4             throws Exception;
5 }

  service實現類:

 1 public class ItemsServiceImpl implements ItemsService {
 2 
 3     @Autowired
 4     private ItemsMapperCustom itemsMapperCustom;
 5 
 6     @Override
 7     public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)
 8             throws Exception {
 9         // 通過itemsMapperCustom查詢資料庫
10         return itemsMapperCustom.findItemsList(itemsQueryVo);
11     }
12 }

  在spring容器中中配置service:applicationContext-service.xml

1 <bean id="itemsService" class="com.luchao.service.ItemsServiceImpl"></bean>

  事務控制:applicationContext-transaction.xml

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 3     xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
 7         http://www.springframework.org/schema/mvc 
 8         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
 9         http://www.springframework.org/schema/context 
10         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
11         http://www.springframework.org/schema/aop 
12         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
13         http://www.springframework.org/schema/tx 
14         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
15     <!-- 事務管理器 對mybatis操作資料庫事務控制,spring使用jdbc的事務控制類 -->
16     <bean id="transactionManager"
17         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
18         <!-- 數據源 dataSource在applicationContext-dao.xml中配置了 -->
19         <property name="dataSource" ref="dataSource" />
20     </bean>
21     
22     <!-- 通知 -->
23     <tx:advice id="txAdvice" transaction-manager="transactionManager">
24         <tx:attributes>
25             <tx:method name="save*" propagation="REQUIRED"/>
26             <tx:method name="delete*" propagation="REQUIRED"/>
27             <tx:method name="insert*" propagation="REQUIRED"/>
28             <tx:method name="update*" propagation="REQUIRED"/>
29             <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
30             <tx:method name="get*" propagation="REQUIRED" read-only="true"/>
31             <tx:method name="select*" propagation="REQUIRED" read-only="true"/>
32         </tx:attributes>
33     </tx:advice>
34     <!-- aop -->
35     <aop:config>
36         <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.luchao.service.*.*(..))"/>
37     </aop:config>
38 </beans>

  通過AOP將事務織入了符合一定條件的方法上面。

  • 整合springMVC

  配置處理器映射器、適配器、視圖解析器:springmvc.xml

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 3     xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
 7         http://www.springframework.org/schema/mvc 
 8         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
 9         http://www.springframework.org/schema/context 
10         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
11         http://www.springframework.org/schema/aop 
12         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
13         http://www.springframework.org/schema/tx 
14         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
15     <!-- 掃描controller註解,多個包中間使用半形逗號分隔 -->
16     <context:component-scan base-package="com.luchao.controller"/>
17     <mvc:annotation-driven/>
18     <!-- 視圖解析器 -->
19     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
20         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
21         <property name="prefix" value="/WEB-INF/jsp/"/>
22         <property name="suffix" value=".jsp"/>
23     </bean>
24 </beans>

  配置前端控制器:web.xml中加入:

 1 <servlet>
 2         <servlet-name>springmvc</servlet-name>
 3         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 4         <init-param>
 5             <param-name>contextConfigLocation</param-name>
 6             <param-value>classpath:spring/springmvc.xml</param-value>
 7         </init-param>
 8         <load-on-startup>1</load-on-startup>
 9     </servlet>
10     <servlet-mapping>
11         <servlet-name>springmvc</servlet-name>
12         <url-pattern>/</url-pattern>
13     </servlet-mapping>

  編寫控制器:

 1 @Controller
 2 @RequestMapping("/items")
 3 public class ItemController {
 4     @Autowired
 5     private ItemsService itemsService;
 6 
 7     // 商品查詢
 8     @RequestMapping("/queryItems")
 9     public ModelAndView queryItems() throws Exception {
10         // 調用service查找 資料庫,查詢商品列表
11         List<ItemsCustom> itemsList = itemsService.findItemsList(null);
12 
13         // 返回ModelAndView
14         ModelAndView modelAndView = new ModelAndView();
15         // 相當 於request的setAttribut,在jsp頁面中通過itemsList取數據
16         modelAndView.addObject("itemsList", itemsList);
17 
18         // 指定視圖
19         // 下邊的路徑,如果在視圖解析器中配置jsp路徑的首碼和jsp路徑的尾碼,修改為
20         // modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
21         // 上邊的路徑配置可以不在程式中指定jsp路徑的首碼和jsp路徑的尾碼
22         modelAndView.setViewName("items/itemsList");
23 
24         return modelAndView;
25     }
26 }

  載入spring容器:

  將mapperservicecontroller載入到spring容器中。web.xml中,添加spring容器監聽器,載入spring容器。

1 <!-- 載入spring容器 -->
2     <context-param>
3         <param-name>contextConfigLocation</param-name>
4         <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
5     </context-param>
6     <listener>
7         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
8     </listener>

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 上篇對python中的字元串進行了列舉和簡單說明,但這些方法太多,逐一背下效率實在太低,下麵我來對這些方法安裝其功能進行總結: 1.字母大小寫相關(中文無效) 1.1 S.upper() -> string 返回一個字母全部大寫的副本 1.2 S.lower() -> string 返回一個字母全是 ...
  • 當我們創建一個集合以後,可以直接使用system.out.println()來列印這個集合,但是,我們需要可以對每個元素進行操作,所以,這裡需要使用迭代器來遍歷集合 迭代器其實就是集合取出元素的方式 調用List對象的iterator()方法,得到Iterator對象,這個類是個介面類型,因此可以知 ...
  • 當今的技術領域,開發者人數最為之多的群體便是web領域,與之相關崗位的包括前端工程師,後臺工程師,移動端開發工程師等等。然而由於受時代浮躁氛圍的影響,許多開發者對最為基礎的HTTP協議都不甚瞭解,這也正是本篇文章的目的--簡單總結一下 瞭解HTTP協議之前你需要掌握的一些基礎知識,基本術語等等。 基 ...
  • 今天主要內容是線性回歸的介紹 原則:在進行任何正式分析之前,先要對數據進行可視化分析,看看直觀效果。 當沒有任何其他附加信息的情況下,對一個變數的最佳假設也是最基本的假設,就是其均值。(前提是使用平方誤差作為衡量準則時) 第二層信息就是可以被利用的二元或多元區分型的信息,這類信息可以輔助我們的預測。 ...
  • 本節大綱: 一:在執行list()函數或者dict()函數首先是調用list()函數或者dict()方法會自動調用該函數的__init__方法進行數據的初始化,list()函數通過一個可迭代的對象,用for迴圈進行遍歷插值。 上述的過程:首先list()函數調用本身的__init__方法進行數據的初 ...
  • 1.web.xml 配置: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <span style="font-size: 15px;"><servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org. ...
  • 題目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5698 Problem Description 有一個無限大的矩形,初始時你在左上角(即第一行第一列),每次你都可以選擇一個右下方格子,並瞬移過去(如從下圖中的紅色格子能直接瞬移到藍色格子),求到第nnn ...
  • PHP 算術運算符 以下實例演示了使用不同算術運算符得到的不同結果: 運行 相關閱讀: php strstr 判斷一個字元串是否存在於里一個字元串中 php stristr() 函數查找字元串在另一字元串中的第一次出現 php strchr() 函數查找字元串在另一字元串中的第一次出現 php 兩個 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...