spring mvc與Mybatis整合——(四)mvc與mybatis整合

 前面的增刪改查還沒有融入到一個web項目中,這裏在前面的基礎上,集成spring管理相關的bean,並在web層集成springmvc。同樣會有源碼的下載。

一、目錄結構:



二、集成spring:

    因爲是一個web項目,且使用了spring作爲粘合劑,相關的jar包就不多解釋了,詳見源碼。項目上篇文章中的結構,這裏把映射文件單獨放到一個目錄,而且添加了Controller的包,映射文件內容不變。注意到這裏講其他配置文件整合到了config包中。因爲使用spring,所以Configuration.xml中的數據連接就移動到了applicationContext.xml文件中,當然可以獨立到database.properties文件中。

    spring管理的事務、Mybatis的sqlSessionFactoryBean,以及項目要掃描的Mybatis的映射文件包路徑屬性,數據源等都在applicationContext.xml文件中:

[html] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <beans xmlns="http://www.springframework.org/schema/beans"   
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:aop="http://www.springframework.org/schema/aop"   
  5.     xmlns:tx="http://www.springframework.org/schema/tx"   
  6.     xmlns:context="http://www.springframework.org/schema/context"   
  7.     xmlns:p="http://www.springframework.org/schema/p"   
  8.     xsi:schemaLocation="    
  9.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  10.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd    
  11.             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd    
  12.             http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd    
  13.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"    
  14.             default-autowire="byName" default-lazy-init="false">   
  15.       
  16.  <!--本示例採用DBCP連接池,應預先把DBCP的jar包複製到工程的lib目錄下。 -->     
  17.     <context:property-placeholder    location="classpath:/config/database.properties" />  
  18.           
  19. <!--     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  20.         destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver"  
  21.         p:url="jdbc:mysql://127.0.0.1:3306/mybatis?characterEncoding=utf8"   
  22.         p:username="root"   
  23.         p:password="123"  
  24.         p:maxActive="10"  
  25.         p:maxIdle="10">  
  26.     </bean> -->  
  27.       
  28.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  29.       <property name="dataSource" ref="dataSource" />  
  30.     </bean>  
  31.       
  32.        
  33.   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">   
  34.      <!--dataSource屬性指定要用到的連接池-->   
  35.      <property name="dataSource" ref="dataSource"/>   
  36.      <!--configLocation屬性指定mybatis的核心配置文件-->   
  37.      <property name="configLocation" value="classpath:config/Configuration.xml" />   
  38.      <!-- 所有配置的mapper文件 -->  
  39.      <property name="mapperLocations" value="classpath*:com/tgb/mybatis/mapper/*.xml" />  
  40.   </bean>   
  41.     
  42.   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  43.      <property name="basePackage" value="com.tgb.mybatis.inter" />       
  44.   </bean>  
  45. </beans>   

三、集成springMVC:

    相關的jar包參見源碼,springMVC充當了web端的框架,mvc-dispatcher-servlet.xml和web.xml中是相關的配置。

mvc-dispatcher-servlet.xml:

[html] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:context="http://www.springframework.org/schema/context"  
  3.     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="  
  5.         http://www.springframework.org/schema/beans       
  6.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.         http://www.springframework.org/schema/context   
  8.         http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  9.         http://www.springframework.org/schema/mvc  
  10.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  11.   
  12.     <context:component-scan base-package="com.tgb.controller" />  
  13.     <mvc:annotation-driven />  
  14.       
  15.     <mvc:resources mapping="/static/**" location="/WEB-INF/static/"/>    
  16.     <mvc:default-servlet-handler/>    
  17.        
  18.     <bean  
  19.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  20.         <property name="prefix">  
  21.             <value>/WEB-INF/pages/</value>  
  22.         </property>  
  23.         <property name="suffix">  
  24.             <value>.jsp</value>  
  25.         </property>  
  26.     </bean>  
  27.   
  28. </beans>  

web.xml:
[html] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  3.   <display-name>MyBatis_Spring_MVC</display-name>  
  4.   <welcome-file-list>  
  5.     <welcome-file>index.jsp</welcome-file>  
  6.   </welcome-file-list>  
  7.     
  8.     
  9.   <filter>  
  10.     <filter-name>encodingFilter</filter-name>  
  11.     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  12.     <init-param>  
  13.       <param-name>encoding</param-name>  
  14.       <param-value>UTF-8</param-value>  
  15.     </init-param>  
  16.     <init-param>  
  17.       <param-name>forceEncoding</param-name>  
  18.       <param-value>true</param-value>  
  19.     </init-param>  
  20.   </filter>  
  21.   <filter-mapping>  
  22.     <filter-name>encodingFilter</filter-name>  
  23.     <url-pattern>/*</url-pattern>  
  24.   </filter-mapping>  
  25.     
  26.   <context-param>  
  27.     <param-name>contextConfigLocation</param-name>  
  28.     <param-value>classpath*:config/applicationContext.xml</param-value>  
  29.   </context-param>  
  30.   <listener>  
  31.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  32.   </listener>  
  33.   <listener>  
  34.     <listener-class>  
  35.             org.springframework.web.context.ContextCleanupListener</listener-class>  
  36.   </listener>  
  37.     
  38.   <!-- mvc配置 -->  
  39.   <servlet>  
  40.     <servlet-name>mvc-dispatcher</servlet-name>  
  41.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  42.     <load-on-startup>1</load-on-startup>  
  43.   </servlet>  
  44.   <servlet-mapping>  
  45.     <servlet-name>mvc-dispatcher</servlet-name>  
  46.     <url-pattern>/</url-pattern>  
  47.   </servlet-mapping>  
  48.    
  49. </web-app>  

web.xml中的啓動順序是:context-param—》listener—》filter—》servlet,如果servlet配置了load-on-startup參數(非負整數,含0,且越小越優先啓動)纔會在服務器啓動的時候加載到內存。


控制層:

 UserController,相當於struts中的action,因爲使用註解,省去了像struts中的一堆的配置,個人感覺這樣對開發和維護都很方便:

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. @Controller  
  2. @RequestMapping("/article")  
  3. public class UserController {  
  4.     @Autowired  
  5.     IUserOperation userMapper;  
  6.   
  7.     @RequestMapping("/list")  
  8.     public ModelAndView listall(HttpServletRequest request,HttpServletResponse response){  
  9.         List<Article> articles=userMapper.getUserArticles(1);   
  10.         ModelAndView mav=new ModelAndView("list");  
  11.         mav.addObject("articles",articles);  
  12.         return mav;  
  13.     }  
  14. }  

展示層:

界面使用el表達式,解析返回的數據:

[html] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. <body>  
  2.     <c:forEach items="${articles}" var="item">    
  3.         ${item.id }--${item.title }--${item.content }<br />  
  4.     </c:forEach>  
  5. </body>  


四、測試:

     這裏使用的是tomcat7,部署之後,訪問:http://localhost:8080/MyBaits_Spring_MVC//article/list可以看到下面的效果:


注意這裏的url,端口後的地址是在controller中配置的,跟struts中稍微有些區別。


五、總結:

    到這裏纔算是一個比較簡單的web項目,有了前面文章的鋪墊,這裏只需要添加spring和mvc的配置,並添加一個controller即可。

    Mybatis還有其他很多的功能,比如,動態語句,自動生成代碼,相關的一些插件,如分頁插件等,有了這些基礎,會在後續的文章中,逐步貼出相關的源碼。


版權聲明:本文爲博主原創文章,未經博主允許不得轉載。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章