SpringMVC学習--springmvcとmybatisの統合


  • 概要
  • SpringMVCは表現層であり、サービスは業務層として機能し、mybatisは持続層としてspringを通じてこの3層を統合する.次の図を示します.
    ステップ1:daoレイヤの統合
    mybatisとspringを統合し、springでmapperインタフェースを管理します.mapperのスキャナを使用してmapperインタフェースを自動的にスキャンしてspringに登録します.
    ステップ2:サービス層の統合
    スプリング管理による サービスインタフェースは、構成方式を使用してspringプロファイルにサービスインタフェースを構成し、トランザクション制御を実現します.
    ステップ3:springmvcの統合
    springmvcはspringのモジュールであるため、統合は必要ありません.
  • Dao層
  • を統合
    つまりSpringとMyBatisの統合【×××アドレス  
    1、MyBatisの配置:SqlMapConfig.xml
    
    2     
    3         
    4         
    5         
    6 

    2、Daoの構成:アプリケーションContext-dao.xml
    データソース、SqlSessionFactory、Mapperスキャナの構成
     1 
    15     
    16     
    17     
    18     
    20         
    21         
    22         
    23         
    24         
    25         
    26     
    27     
    28     
    29         
    30         
    31         
    32         
    33     
    34     
    35     
    36         
    37         
    38     
    39 

    3.Po、Mapperインタフェース、Mapperマッピングファイルに決めます.
  • 統合サービス
  • springにサービスを管理させる
    サービスインタフェースに設定:
    1 public interface ItemsService {2     //       
    3     public List findItemsList(ItemsQueryVo itemsQueryVo)4             throws Exception;5 }

    サービス実装クラス:
     1 public class ItemsServiceImpl implements ItemsService { 2 
     3     @Autowired 4     private ItemsMapperCustom itemsMapperCustom; 5 
     6     @Override 7     public List findItemsList(ItemsQueryVo itemsQueryVo) 8             throws Exception { 9         //   itemsMapperCustom     
    10         return itemsMapperCustom.findItemsList(itemsQueryVo);11     }12 }

    springコンテナでserviceを構成する:アプリケーションContext-service.xml

    トランザクション制御:アプリケーションContext-transaction.xml
     1 
    15     
    16     
    18         
    19         
    20     
    21     
    22     
    23     
    24         
    25             
    26             
    27             
    28             
    29             
    30             
    31             
    32         
    33     
    34     
    35     
    36         
    37     
    38 

    AOPによって事務を一定の条件を満たす方法に織り込んだ.
  • 統合springMVC
  • プロセッサマッパ、アダプタ、ビュー解析器の構成:springmvc.xml
     1 
    15     
    16     
    17     
    18     
    19     
    20         
    21         
    22         
    23     
    24 

    フロントエンドコントローラの構成:web.xmlに追加:
     1 
     2         springmvc
     3         org.springframework.web.servlet.DispatcherServlet
     4         
     5             contextConfigLocation
     6             classpath:spring/springmvc.xml
     7         
     8         1
     9     
    10     
    11         springmvc
    12         /
    13     

    作成コントローラ:
     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 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 }

    スプリングコンテナをロードするには:
    mapper、service、controllerをspringコンテナにロードします.Web.xmlでspringコンテナリスナーを追加し、springコンテナをロードします.
    1 
    2     
    3         contextConfigLocation
    4         /WEB-INF/classes/spring/applicationContext-*.xml
    5     
    6     
    7         org.springframework.web.context.ContextLoaderListener
    8